Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6270169
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: Java

2013-10-24 11:50:38

1、第一个文件 MailSenderInfo.java

点击(此处)折叠或打开

  1. /**
  2. * 发送邮件需要使用的基本信息
  3. */
  4. import java.util.Properties;
  5. public class MailSenderInfo {
  6.     // 发送邮件的服务器的IP和端口

  7.     private String mailServerHost;
  8.     private String mailServerPort = "25";
  9.     // 邮件发送者的地址

  10.     private String fromAddress;
  11.     // 邮件接收者的地址

  12.     private String toAddress;
  13.     // 登陆邮件发送服务器的用户名和密码

  14.     private String userName;
  15.     private String password;
  16.     // 是否需要身份验证

  17.     private boolean validate = false;
  18.     // 邮件主题

  19.     private String subject;
  20.     // 邮件的文本内容

  21.     private String content;
  22.     // 邮件附件的文件名

  23.     private String[] attachFileNames;     
  24.     /**
  25.      * 获得邮件会话属性
  26.      */
  27.     public Properties getProperties(){
  28.      Properties p = new Properties();
  29.      p.put("mail.smtp.host", this.mailServerHost);
  30.      p.put("mail.smtp.port", this.mailServerPort);
  31.      p.put("mail.smtp.auth", validate ? "true" : "false");
  32.      return p;
  33.     }
  34.     public String getMailServerHost() {
  35.      return mailServerHost;
  36.     }
  37.     public void setMailServerHost(String mailServerHost) {
  38.      this.mailServerHost = mailServerHost;
  39.     }
  40.     public String getMailServerPort() {
  41.      return mailServerPort;
  42.     }
  43.     public void setMailServerPort(String mailServerPort) {
  44.      this.mailServerPort = mailServerPort;
  45.     }
  46.     public boolean isValidate() {
  47.      return validate;
  48.     }
  49.     public void setValidate(boolean validate) {
  50.      this.validate = validate;
  51.     }
  52.     public String[] getAttachFileNames() {
  53.      return attachFileNames;
  54.     }
  55.     public void setAttachFileNames(String[] fileNames) {
  56.      this.attachFileNames = fileNames;
  57.     }
  58.     public String getFromAddress() {
  59.      return fromAddress;
  60.     }
  61.     public void setFromAddress(String fromAddress) {
  62.      this.fromAddress = fromAddress;
  63.     }
  64.     public String getPassword() {
  65.      return password;
  66.     }
  67.     public void setPassword(String password) {
  68.      this.password = password;
  69.     }
  70.     public String getToAddress() {
  71.      return toAddress;
  72.     }
  73.     public void setToAddress(String toAddress) {
  74.      this.toAddress = toAddress;
  75.     }
  76.     public String getUserName() {
  77.      return userName;
  78.     }
  79.     public void setUserName(String userName) {
  80.      this.userName = userName;
  81.     }
  82.     public String getSubject() {
  83.      return subject;
  84.     }
  85.     public void setSubject(String subject) {
  86.      this.subject = subject;
  87.     }
  88.     public String getContent() {
  89.      return content;
  90.     }
  91.     public void setContent(String textContent) {
  92.      this.content = textContent;
  93.     }
  94. }

2、第二个文件 MyAuthenticator.java

 

点击(此处)折叠或打开

  1. import javax.mail.*;
  2.   
  3. public class MyAuthenticator extends Authenticator{
  4.     String userName=null;
  5.     String password=null;
  6.     
  7.     public MyAuthenticator(){
  8.     }
  9.     public MyAuthenticator(String username, String password) {
  10.         this.userName = username;
  11.         this.password = password;
  12.     }
  13.     protected PasswordAuthentication getPasswordAuthentication(){
  14.         return new PasswordAuthentication(userName, password);
  15.     }
  16. }

3、第三个文件 SimpleMailSender.java

点击(此处)折叠或打开

  1. import java.util.Date;
  2. import java.util.Properties;
  3. import javax.mail.Address;
  4. import javax.mail.BodyPart;
  5. import javax.mail.Message;
  6. import javax.mail.Multipart;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeBodyPart;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeMultipart;

  13. /**
  14. * 简单邮件(不带附件的邮件)发送器
  15. */
  16. public class SimpleMailSender {
  17. /**
  18.   * 以文本格式发送邮件
  19.   * @param mailInfo 待发送的邮件的信息
  20.   */
  21.     public void sendTextMail(MailSenderInfo mailInfo) throws Exception{
  22.      MyAuthenticator authenticator = null;
  23.      Properties pro = mailInfo.getProperties();
  24.      if (mailInfo.isValidate()) {
  25.         authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
  26.      }
  27.      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
  28.      Message mailMessage = new MimeMessage(sendMailSession);
  29.      Address from = new InternetAddress(mailInfo.getFromAddress());
  30.      mailMessage.setFrom(from);
  31.      Address to = new InternetAddress(mailInfo.getToAddress());
  32.      mailMessage.setRecipient(Message.RecipientType.TO,to);
  33.      mailMessage.setSubject(mailInfo.getSubject());
  34.      mailMessage.setSentDate(new Date());
  35.      String mailContent = mailInfo.getContent();
  36.      mailMessage.setText(mailContent);
  37.      Transport.send(mailMessage);
  38.     }
  39.     
  40.     /**
  41.      * 以HTML格式发送邮件
  42.      * @param mailInfo 待发送的邮件信息
  43.      */
  44.     public static void sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
  45.      MyAuthenticator authenticator = null;
  46.      Properties pro = mailInfo.getProperties();
  47.      if (mailInfo.isValidate()) {
  48.         authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
  49.      }
  50.      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
  51.      Message mailMessage = new MimeMessage(sendMailSession);
  52.      Address from = new InternetAddress(mailInfo.getFromAddress());
  53.      mailMessage.setFrom(from);
  54.      Address to = new InternetAddress(mailInfo.getToAddress());
  55.      mailMessage.setRecipient(Message.RecipientType.TO,to);
  56.      mailMessage.setSubject(mailInfo.getSubject());
  57.      mailMessage.setSentDate(new Date());
  58.      Multipart mainPart = new MimeMultipart();
  59.      BodyPart html = new MimeBodyPart();
  60.      html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
  61.      mainPart.addBodyPart(html);
  62.      mailMessage.setContent(mainPart);
  63.      Transport.send(mailMessage);
  64.      }
  65. }

4、测试Main Test.java

点击(此处)折叠或打开

  1. import org.apache.commons.logging.Log;
  2. import org.apache.commons.logging.LogFactory;

  3. public class Test {
  4.     
  5.     /**
  6.      * 日志输出对象
  7.      */
  8.     private static final Log log = LogFactory.getLog(Test.class);

  9.     /**
  10.      * 测试Main方法
  11.      * @param args
  12.      */
  13.     public static void main(String [] args){
  14.         try {
  15.             sendEmail("test1@126.com","test2@126.com","邮件标题","邮件内容");
  16.         } catch (Exception e) {
  17.             e.printStackTrace();
  18.         }
  19.     }
  20.     
  21.     /**
  22.      * 邮件发送工具方法
  23.      * @param fromAddress
  24.      * @param toAddress
  25.      * @param content
  26.      * @throws Exception
  27.      */
  28.     public static void sendEmail(String fromAddress, String toAddress,String title ,String content) throws Exception{
  29.         log.info("执行邮件发送(发送地址:"+fromAddress+ " 接收地址:" + toAddress +")");
  30.         log.debug("发送邮件标题:" + title + " 邮件内容:" + content);
  31.         SimpleMailSender sms = new SimpleMailSender();
  32.         MailSenderInfo mailInfo = new MailSenderInfo();
  33.         mailInfo.setMailServerHost("smtp.domain.com"); //邮件服务器地址

  34.         mailInfo.setMailServerPort("25"); //邮件服务器端口

  35.         mailInfo.setValidate(true); //是否进行用户名密码验证

  36.      mailInfo.setUserName("username"); //用户名

  37.      mailInfo.setPassword("passwd"); //密码

  38.      mailInfo.setFromAddress(fromAddress); //发送地址

  39.      mailInfo.setToAddress(toAddress); //接收地址

  40.      mailInfo.setSubject(title);
  41.      mailInfo.setContent(content);
  42.         sms.sendHtmlMail(mailInfo);
  43.     }
  44. }

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