zoukankan      html  css  js  c++  java
  • java工具类-邮件发送

    mail-1.4.jar

    package com.huawei.it.citools.mail;

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

    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    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 SimpleMailSender {

     /**
      * 以文本格式发送邮件
      *
      * @param mailInfo
      *            待发送的邮件的信息
      * @throws MessagingException
      */
     public boolean sendTextMail(MailSenderInfo mailInfo) throws MessagingException {
      // 判断是否需要身份认证
      MyAuthenticator authenticator = null;
      Properties pro = mailInfo.getProperties();
      if (mailInfo.isValidate()) {
       // 如果需要身份认证,则创建一个密码验证器
       authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
      }
      // 根据邮件会话属性和密码验证器构造一个发送邮件的session
      Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
      try {
       // 根据session创建一个邮件消息
       Message mailMessage = new MimeMessage(sendMailSession);
       // 创建邮件发送者地址
       Address from = new InternetAddress(mailInfo.getFromAddress());
       // 设置邮件消息的发送者
       mailMessage.setFrom(from);
       // 创建邮件的接收者地址,并设置到邮件消息中
       List<String> toAddressList = mailInfo.getToAddressList();
       for (String toAddress : toAddressList) {
        Address to = new InternetAddress(toAddress);
        mailMessage.addRecipient(Message.RecipientType.TO, to);
       }
       // 设置邮件消息的主题
       mailMessage.setSubject(mailInfo.getSubject());
       // 设置邮件消息发送的时间
       mailMessage.setSentDate(new Date());
       // 设置邮件消息的主要内容
       String mailContent = mailInfo.getContent();
       mailMessage.setText(mailContent);
       // 发送邮件
       Transport.send(mailMessage);
       return true;
      } catch (MessagingException ex) {
       throw ex;
      }
     }

     /**
      * 以HTML格式发送邮件
      *
      * @param mailInfo
      *            待发送的邮件信息
      * @throws MessagingException
      */
     public static boolean sendHtmlMail(MailSenderInfo mailInfo) throws MessagingException {
      // 判断是否需要身份认证
      MyAuthenticator authenticator = null;
      Properties pro = mailInfo.getProperties();
      // 如果需要身份认证,则创建一个密码验证器
      if (mailInfo.isValidate()) {
       authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
      }
      // 根据邮件会话属性和密码验证器构造一个发送邮件的session
      Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
      try {
       // 根据session创建一个邮件消息
       Message mailMessage = new MimeMessage(sendMailSession);
       // 创建邮件发送者地址
       Address from = new InternetAddress(mailInfo.getFromAddress());
       // 设置邮件消息的发送者
       mailMessage.setFrom(from);
       // 创建邮件的接收者地址,并设置到邮件消息中
       List<String> toAddressList = mailInfo.getToAddressList();
       for (String toAddress : toAddressList) {
        Address to = new InternetAddress(toAddress);
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.addRecipient(Message.RecipientType.TO, to);
       }
       // 设置邮件消息的主题
       mailMessage.setSubject(mailInfo.getSubject());
       // 设置邮件消息发送的时间
       mailMessage.setSentDate(new Date());
       // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
       Multipart mainPart = new MimeMultipart();
       // 创建一个包含HTML内容的MimeBodyPart
       BodyPart html = new MimeBodyPart();
       // 设置HTML内容
       html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
       mainPart.addBodyPart(html);
       // 将MiniMultipart对象设置为邮件内容
       mailMessage.setContent(mainPart);
       // 发送邮件
       Transport.send(mailMessage);
       return true;
      } catch (MessagingException ex) {
       throw ex;
      }
     }

    }

    //发送使用

    package com.huawei.it.citools.mail;

    import java.util.ArrayList;
    import java.util.List;

    public class TestMail {
     public static void main(String[] args) throws Exception {
      // 这个类主要是设置邮件
      MailSenderInfo mailInfo = new MailSenderInfo();
      mailInfo.setMailServerHost("smtp.huawei.com");
      mailInfo.setMailServerPort("25");
      mailInfo.setValidate(true);
      mailInfo.setUserName("swx117518");
      mailInfo.setPassword("teamsun^123");// 您的邮箱密码
      mailInfo.setFromAddress("kaifa.songsong@huawei.com");
      List<String> toAddressList = new ArrayList<String>();
      toAddressList.add("swx117518@notesmail.huawei.com.cn");
      toAddressList.add("swx117518@notesmail.huawei.com.cn");
      mailInfo.setToAddressList(toAddressList);
      mailInfo.setSubject("测试邮件");
      mailInfo.setContent("测试邮件");
      // 这个类主要来发送邮件
      SimpleMailSender sms = new SimpleMailSender();
      sms.sendTextMail(mailInfo);// 发送文体格式
      sms.sendHtmlMail(mailInfo);// 发送html格式
     }

    }

  • 相关阅读:
    codesmith 连接mysql
    数据库 价格字段 设置 decimal(8,2),价格为100W,只显示999999.99
    AOP和IOC
    Android Studio 每次运行都会再下载一遍,修改
    gradle 的jar下载到哪里了
    遇到的坑
    Error:Failed to resolve: :Base:
    re-download dependencies and 无法下载jar 的解决
    DI是实现面向切面和面向抽象的前提
    基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美
  • 原文地址:https://www.cnblogs.com/heling/p/3874710.html
Copyright © 2011-2022 走看看