使用servlet和javamail发送邮件
2008-01-05 08:56:10 来源:WEB开发网核心提示:import java.util.PRoperties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import java
import java.util.PRoperties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.rmi.transport.Transport;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
public class EmailServlet extends HttpServlet {
//default value for mail server address, in case the user
//doesn't provide one
private final static String DEFAULT_SERVER = "mail.attbi.com";
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String smtpServ = DEFAULT_SERVER;
String from = "java2s@yourserver.com";
String to = "yourname@yourserver.com";
String subject = "subject line";
String emailContent = "emailContent";
response.setContentType("text/Html");
java.io.PrintWriter out = response.getWriter();
out
.println("<html><head><title>Email message sender</title></head><body>");
try {
sendMessage(smtpServ, to, from, subject, emailContent);
} catch (Exception e) {
throw new ServletException(e.getMessage());
}
out.println("<h2>The message was sent sUCcessfully</h2>");
out.println("</body></html>");
} //doPost
private void sendMessage(String smtpServer, String to, String from,
String subject, String emailContent) throws Exception {
Properties properties = System.getProperties();
//populate the 'Properties' object with the mail
//server address, so that the default 'session'
//instance can use it.
properties.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(properties);
Message mailMsg = new MimeMessage(session);//a new email message
InternetAddress[] addresses = null;
try {
if (to != null) {
//throws 'AddressException' if the 'to' email address
//violates RFC822 syntax
addresses = InternetAddress.parse(to, false);
mailMsg.setRecipients(Message.RecipientType.TO, addresses);
} else {
throw new MessagingException(
"The mail message requires a 'To' address.");
}
if (from != null) {
mailMsg.setFrom(new InternetAddress(from));
} else {
throw new MessagingException(
"The mail message requires a valid 'From' address.");
}
if (subject != null)
mailMsg.setSubject(subject);
if (emailContent != null)
mailMsg.setText(emailContent);
//Finally, send the meail message; throws a 'SendFailedException'
//if any of the message's recipients have an invalid adress
Transport.send(mailMsg);
} catch (Exception exc) {
throw exc;
}
}//sendMessage
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}//EmailServlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.rmi.transport.Transport;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
public class EmailServlet extends HttpServlet {
//default value for mail server address, in case the user
//doesn't provide one
private final static String DEFAULT_SERVER = "mail.attbi.com";
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String smtpServ = DEFAULT_SERVER;
String from = "java2s@yourserver.com";
String to = "yourname@yourserver.com";
String subject = "subject line";
String emailContent = "emailContent";
response.setContentType("text/Html");
java.io.PrintWriter out = response.getWriter();
out
.println("<html><head><title>Email message sender</title></head><body>");
try {
sendMessage(smtpServ, to, from, subject, emailContent);
} catch (Exception e) {
throw new ServletException(e.getMessage());
}
out.println("<h2>The message was sent sUCcessfully</h2>");
out.println("</body></html>");
} //doPost
private void sendMessage(String smtpServer, String to, String from,
String subject, String emailContent) throws Exception {
Properties properties = System.getProperties();
//populate the 'Properties' object with the mail
//server address, so that the default 'session'
//instance can use it.
properties.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(properties);
Message mailMsg = new MimeMessage(session);//a new email message
InternetAddress[] addresses = null;
try {
if (to != null) {
//throws 'AddressException' if the 'to' email address
//violates RFC822 syntax
addresses = InternetAddress.parse(to, false);
mailMsg.setRecipients(Message.RecipientType.TO, addresses);
} else {
throw new MessagingException(
"The mail message requires a 'To' address.");
}
if (from != null) {
mailMsg.setFrom(new InternetAddress(from));
} else {
throw new MessagingException(
"The mail message requires a valid 'From' address.");
}
if (subject != null)
mailMsg.setSubject(subject);
if (emailContent != null)
mailMsg.setText(emailContent);
//Finally, send the meail message; throws a 'SendFailedException'
//if any of the message's recipients have an invalid adress
Transport.send(mailMsg);
} catch (Exception exc) {
throw exc;
}
}//sendMessage
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}//EmailServlet
[]
- ››使用脚本恢复WinXP系统的用户登录密码
- ››使用phpMyadmin创建数据库及独立数据库帐号
- ››使用Zend Framework框架中的Zend_Mail模块发送邮件...
- ››使用cout标准输出如何控制小数点后位数
- ››使用nofollow标签做SEO的技巧
- ››使用 WebSphere Message Broker 的 WebSphere Tra...
- ››使用SQL Server事件探查器做应用程序的性能分析
- ››使用SQL Server事件探查器分析死锁原因
- ››使用纯文本文件打造WCF服务
- ››使用 Dojo 开发定制 Business Space 小部件,第 4...
- ››使用 ADDRESS 与 INDIRECT函数查询信息
- ››使用 COLUMN函数编制单元信息
更多精彩
赞助商链接