jsp发送html邮件
Posted 54powerman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp发送html邮件相关的知识,希望对你有一定的参考价值。
<%@ page import= " javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*" %>
<%
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
Email_Autherticatorbean auth=null;
props.put("mail.smtp.host", "smtp.sohu.com");
//如果需要验证
props.put("mail.smtp.auth", "true");
auth = new Email_Autherticatorbean("54powerman","xxxx");
props.put("mail.smpt.port", "25");
//session认证
sendMailSession = Session.getInstance(props,auth);
//这个是跟踪后台消息。打印在控制台
sendMailSession.setDebug(true);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setSubject("Mail Test Subject");
newMessage.setContent("<b>Hell!</b>","text/html;charset=gb2312");
MimeMultipart multipart = new MimeMultipart("related");
BodyPart body = new MimeBodyPart();
body.setContent("<b>Hello!</b><br><img src=/"cid:1/">","text/html;charset=gb2312");
multipart.addBodyPart(body);
body = new MimeBodyPart();
DataSource fds = new FileDataSource("d:/0.jpg");
body.setDataHandler(new DataHandler(fds));
body.setHeader("Content-ID","<1>");
multipart.addBodyPart(body);
newMessage.setContent(multipart);
newMessage.setFrom(new InternetAddress("54powerman@sohu.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("54powerman@sohu.com"));
newMessage.setSentDate(new Date());
//newMessage.setText("<b>Mail Test Content</b>");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<%!
public class Email_Autherticatorbean extends Authenticator
{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
%>
以上是关于jsp发送html邮件的主要内容,如果未能解决你的问题,请参考以下文章