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

读好书,交益友

文章分类

全部博文(447)

文章存档

2024年(1)

2023年(5)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: 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. }



阅读(4438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~