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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值