JavaMail---我的邮件我做主---发送邮件

用JavaMail发邮件十分简单。

先设properties--->session--->message创建--->transport去连接和发送。

主要麻烦的还是昨天讲的如何创建

 

  1. public class ComplexMessage {
  2.     String protocal = "smtp";
  3.     public Session createSession() {
  4.         Properties props = new Properties();
  5.         props.setProperty("mail.transport.protocol", protocal);
  6.         props.setProperty("mail.smtp.auth""true");
  7.         Session session = Session.getInstance(props);
  8.         session.setDebug(true);
  9.         return session;
  10.     }
  11.     public static MimeMessage createMessage(Session session) throws Exception {
  12.         String from = "abc@sina.com";
  13.         String to = "123@hotmail.com";
  14.         String subject = "hello world";
  15.         String body = "<a href=http://blog.csdn.net/terryzero/>"
  16.                 + "欢迎大家光临我的blog</a></br>" + "<img src=/"cid:google/">";
  17.         MimeMessage message = new MimeMessage(session);
  18.         message.setFrom(new InternetAddress(from));
  19.         message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  20.         message.setSubject(subject);
  21.         MimeBodyPart contentPart = createContent(body, "E://MyTemplate//resourse//google.jpg");
  22.         MimeBodyPart attachPart1 = createAttachmet("E://MyTemplate//resourse//google.jpg");
  23.         MimeBodyPart attachPart2 = createAttachmet("E://MyTemplate//resourse//google.jpg");
  24.         MimeMultipart allMultipart = new MimeMultipart("mixed");
  25.         allMultipart.addBodyPart(contentPart);
  26.         allMultipart.addBodyPart(attachPart1);
  27.         allMultipart.addBodyPart(attachPart2);
  28.         message.setContent(allMultipart);
  29.         message.saveChanges();
  30.         return message;
  31.     }
  32.     public static MimeBodyPart createContent(String body, String filename) throws Exception {
  33.         MimeBodyPart contentPart = new MimeBodyPart();
  34.         MimeMultipart contentMultipart = new MimeMultipart("related");
  35.         MimeBodyPart htmlBodyPart = new MimeBodyPart();//文本
  36.         htmlBodyPart.setContent(body, "text/html;charset=gb2312");
  37.         contentMultipart.addBodyPart(htmlBodyPart);
  38.         MimeBodyPart gifBodyPart = new MimeBodyPart(); //图片
  39.         FileDataSource fds = new FileDataSource(filename);
  40.         gifBodyPart.setDataHandler(new DataHandler(fds));
  41.         gifBodyPart.setContentID("google");
  42.         contentMultipart.addBodyPart(gifBodyPart);
  43.         contentPart.setContent(contentMultipart);
  44.         return contentPart;
  45.     }
  46.     public static MimeBodyPart createAttachmet(String filename) throws Exception {
  47.         MimeBodyPart attachPart = new MimeBodyPart();//附件
  48.         FileDataSource fds = new FileDataSource(filename);
  49.         attachPart.setDataHandler(new DataHandler(fds));
  50.         attachPart.setFileName(fds.getName());
  51.         return attachPart;
  52.     }
  53.     public static void main(String[] args) {
  54.         String server = "smtp.sina.com.cn";
  55.         String user = "abc";
  56.         String pass = "123456";
  57.         try {
  58.             ComplexMessage cm = new ComplexMessage();
  59.             Session session = cm.createSession();
  60.             MimeMessage message = createMessage(session);
  61.             //message.writeTo(new FileOutputStream("E://MyTemplate//resourse//ComplexMessage.eml"));
  62.             Transport transport = session.getTransport();
  63.             transport.connect(server, user, pass);
  64.             transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
  65.             transport.close();
  66.         } catch (Exception e) {
  67.             e.printStackTrace();
  68.         }
  69.     }
  70. }

 

这代码能说明一切,我就不多少说废话了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值