Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2485086
  • 博文数量: 357
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 4864
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

下死功夫,动活脑筋;读好书,交益友

文章分类

全部博文(357)

文章存档

2019年(12)

2018年(23)

2017年(67)

2016年(45)

2015年(51)

2014年(58)

2013年(53)

2012年(42)

2011年(6)

分类: Java

2014-04-02 20:19:55

mark一下



点击(此处)折叠或打开

  1. package test;

  2. /**
  3.  * Created by xinghongrui on 2014/4/2.
  4.  */
  5. import javax.mail.*;
  6. import javax.mail.internet.*;
  7. import java.util.*;

  8. public class Main
  9. {
  10.     String d_email = "xxxx@gmail.com",
  11.             d_password = "xxxx",
  12.             d_host = "smtp.gmail.com",
  13.             d_port = "465",
  14.             m_to = "mfc42d@sohu.com",
  15.             m_subject = "Testing",
  16.             m_text = "testing email.";

  17.     public Main()
  18.     {
  19.         Properties props = new Properties();
  20.         props.put("mail.smtp.user", d_email);
  21.         props.put("mail.smtp.host", d_host);
  22.         props.put("mail.smtp.port", d_port);
  23.         props.put("mail.smtp.starttls.enable","true");
  24.         props.put("mail.smtp.auth", "true");
  25.         props.put("mail.smtp.debug", "true");
  26.         props.put("mail.smtp.socketFactory.port", d_port);
  27.         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  28.         props.put("mail.smtp.socketFactory.fallback", "false");

  29.         SecurityManager security = System.getSecurityManager();

  30.         try
  31.         {
  32.             Authenticator auth = new SMTPAuthenticator();
  33.             Session session = Session.getInstance(props, auth);
  34.             session.setDebug(true);
  35.             MimeMessage msg = new CustomMimeMessage(session);
  36.             msg.setText(m_text);
  37.             msg.setSubject(m_subject);
  38.             msg.setSender(new InternetAddress("joe@acme.com", "Joe Smith"));//制定sender,用户名和邮件地址分开
  39.             msg.setFrom(new InternetAddress(d_email));
  40.             msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
  41.             Transport.send(msg);
  42.         }
  43.         catch (Exception mex)
  44.         {
  45.             mex.printStackTrace();
  46.         }
  47.     }

  48.     public static void main(String[] args)
  49.     {
  50.         Main blah = new Main();
  51.     }

  52.     private class SMTPAuthenticator extends javax.mail.Authenticator
  53.     {
  54.         public PasswordAuthentication getPasswordAuthentication()
  55.         {
  56.             return new PasswordAuthentication(d_email, d_password);
  57.         }
  58.     }
  59. }



自定义Message-ID

点击(此处)折叠或打开

  1. package test;

  2. import javax.mail.MessagingException;
  3. import javax.mail.Session;
  4. import javax.mail.internet.MimeMessage;

  5. /**
  6.  * Created by xinghongrui on 2014/4/2.
  7.  */
  8. public class CustomMimeMessage extends MimeMessage {

  9.     public CustomMimeMessage(Session session) {

  10.         super(session);

  11.     }

  12.     @Override

  13.     protected void updateMessageID() throws MessagingException {

  14.         setHeader("Message-ID", "objects-message-id");

  15.     }

  16. }



阅读(2208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册