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);
    }
}
%>

 
上一篇:Facebook发布 Wedge 100 ToR 交换机及光模块规范


下一篇:关于Sweep and Prune 算法