javaMail实现以html格式发送邮件

package mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class PageDemo {
 public static void main(String[] args) {
  try {
   Properties props = new Properties();
   props.put("mail.smtp.host", "smtp.sina.com");
   props.put("mail.smtp.auth", "true");
   props.put("mail.bebug", "true");

   // URLName urlName=new URLName(server);
   // PasswordAuthentication pa=new
   // PasswordAuthentication(user,password);
   Session sendMailSession = Session.getDefaultInstance(props, null);
   sendMailSession.setDebug(true);

   // sendMailSession.setPasswordAuthentication (urlName,pa);
   // SMTPTransport transport=new SMTPTransport(sendMailSession,null);
   Transport transport = sendMailSession.getTransport("smtp");
   Message msg = new MimeMessage(sendMailSession);
   msg.setFrom(new InternetAddress("kingsever@sina.com"));
   InternetAddress[] address = { new InternetAddress(
     "wtc455615369@126.com") };
   msg.setRecipients(Message.RecipientType.TO, address);
   msg.setSubject("您好,请审批");
   // msg.setHeader("X-Mailer", "msgsend");
   msg.setSentDate(new Date());
   Multipart mp = new MimeMultipart();
   MimeBodyPart mbp = new MimeBodyPart();

   // 设定邮件内容的类型为 text/plain 或 text/html
   mbp
     .setContent(
       "<a href='http://test.com/sdfj?sfsfjeijflskjdflsjdf' target='blank'>审批</a>",
       "text/html;charset=GB2312");
   mp.addBodyPart(mbp);
   msg.setContent(mp);
   // msg.setText ("<a href='fsdf'>asfsaf</a>");
   transport.connect("smtp.sina.com", "kingsever", "6557043");
   // transport.connect ();
   transport.sendMessage(msg, msg
     .getRecipients(Message.RecipientType.TO));
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值