package javaduke.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class MailService extends HttpServlet {
int index=0;
String err=null;
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//Service the request
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mailto=null;
String messageText="\n以下为表单内容";
mailto=request.getParameter("Email");
Enumeration keys;
String key,value;
keys=request.getParameterNames ();
while(keys.hasMoreElements ()){
key=(String)keys.nextElement ();
value=request.getParameter (key);
messageText+="\n"+key+":"+value;
}
messageText+="\n转发邮件处理程序来自\nwww.dukejava.com";
Properties pros=new Properties();
pros.put("mail.smtp.host","mycgiserver.com");
Session session=Session.getDefaultInstance(pros,null);
try{
Message message=new MimeMessage(session);
message.setSubject("DUKEJAVA.COM欢迎您光临");
message.setFrom(new InternetAddress("发信@dukejava.com"));
InternetAddress address=new InternetAddress(mailto);
message.setRecipient(Message.RecipientType.TO,address);
message.setText(messageText);
Transport.send(message);
}catch(MessagingException exa){int index=1;err=exa.toString();}
if(index==0){
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("");
out.println("");
out.println("");
out.println ("
信息成功发送");
out.println("\n
www.dukejava.com提供程序");
out.println("\n
关闭窗口");
out.println("");
out.close();
}
if(index==1){
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("");
out.println("");
out.println("");
out.println ("
信息发送失败");
out.println ("\n
错误原因:"+err);
out.println("\n
www.dukejava.com提供程序");
out.println("\n
关闭窗口");
out.println("");
out.close();
}
}
//Get Servlet information
public String getServletInfo() {
return "javaduke.MailService Information";
}
}