• MailJobUtils 发送邮件


     1 import java.util.List;
     2 import java.util.Properties;
     3 
     4 import javax.annotation.Resource;
     5 import javax.mail.Authenticator;
     6 import javax.mail.Message.RecipientType;
     7 import javax.mail.PasswordAuthentication;
     8 import javax.mail.Session;
     9 import javax.mail.Transport;
    10 import javax.mail.internet.InternetAddress;
    11 import javax.mail.internet.MimeMessage;
    12 
    13 import cn.itcast.bos.dao.IWorkbillDao;
    14 import cn.itcast.bos.domain.Workbill;
    15 
    16 /**
    17  * 发送邮件
    18  *
    19  */
    20 public class MailJob {
    21     @Resource
    22     private IWorkbillDao workbillDao;
    23 
    24     private String username;
    25     private String password;
    26     private String smtpServer;
    27 
    28     public String getUsername() {
    29         return username;
    30     }
    31 
    32     public void setUsername(String username) {
    33         this.username = username;
    34     }
    35 
    36     public String getPassword() {
    37         return password;
    38     }
    39 
    40     public void setPassword(String password) {
    41         this.password = password;
    42     }
    43 
    44     public void execute() {
    45         System.out.println("要发邮件了。。。");
    46         try {
    47             //查询工单类型为新单的所有工单
    48             List<Workbill> list = workbillDao.findNewWorkbills();
    49             if(null != list && list.size() > 0){
    50                 final Properties mailProps = new Properties();
    51                 mailProps.put("mail.smtp.host", this.getSmtpServer());
    52                 mailProps.put("mail.smtp.auth", "true");
    53                 mailProps.put("mail.username", this.getUsername());
    54                 mailProps.put("mail.password", this.getPassword());
    55 
    56                 // 构建授权信息,用于进行SMTP进行身份验证
    57                 Authenticator authenticator = new Authenticator() {
    58                     protected PasswordAuthentication getPasswordAuthentication() {
    59                         // 用户名、密码
    60                         String userName = mailProps.getProperty("mail.username");
    61                         String password = mailProps.getProperty("mail.password");
    62                         return new PasswordAuthentication(userName, password);
    63                     }
    64                 };
    65                 // 使用环境属性和授权信息,创建邮件会话
    66                 Session mailSession = Session.getInstance(mailProps, authenticator);
    67                 for(Workbill workbill : list){
    68                     // 创建邮件消息
    69                     MimeMessage message = new MimeMessage(mailSession);
    70                     // 设置发件人
    71                     InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));
    72                     message.setFrom(from);
    73                     // 设置收件人
    74                     InternetAddress to = new InternetAddress("test@itcast.cn");
    75                     message.setRecipient(RecipientType.TO, to);
    76                     // 设置邮件标题
    77                     message.setSubject("系统邮件:新单通知");
    78                     // 设置邮件的内容体
    79                     message.setContent(workbill.toString(), "text/html;charset=UTF-8");
    80                     // 发送邮件
    81                     Transport.send(message);
    82                 }
    83             }
    84         } catch (Exception ex) {
    85             ex.printStackTrace();
    86         }
    87     }
    88 
    89     public String getSmtpServer() {
    90         return smtpServer;
    91     }
    92 
    93     public void setSmtpServer(String smtpServer) {
    94         this.smtpServer = smtpServer;
    95     }
    96 }
  • 相关阅读:
    php数组操作
    DedeCMS栏目页调用当前栏目名和上级栏目名
    ThinkPHP递归删除栏目
    WebUploader
    js中的事件委托或是事件代理
    thinkphp框架if标签条件表达式
    sublime插件安装
    thinkphp分页
    织梦自定义变量使用
    php foreach用法和实例
  • 原文地址:https://www.cnblogs.com/zlw-xf/p/8030255.html
Copyright © 2020-2023  润新知