SendMailText.java 5.55 KB
package com.huaheng.common.mail;


import com.huaheng.common.mail.alarmmail.domain.Sender;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;
import java.util.TreeMap;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;

/**
 * @Description:邮件发送类
 * @Anthor:ZengBo
 */
public class SendMailText {
    //发件人地址
    private static String senderAddress = "wms@huahengweld.com";
    //收件人地址
    private static String recipientAddress = "wms@huahengweld.com";
    //发件人账户名
    private static String senderAccount = "wms";
    //发件人账户密码
    private static String senderPassword = "nkfwhfi986+/214G2";

    public void aaa(String[] args) throws Exception {
        //1、连接邮件服务器的参数配置
        Properties props = new Properties();
        //设置用户的认证方式
        props.setProperty("mail.smtp.auth", "true");
        //设置传输协议
        props.setProperty("mail.transport.protocol", "smtp");
        //设置发件人的SMTP服务器地址,使用前PING一下mail.huahengweld.com
        props.setProperty("mail.smtp.host", "172.16.0.2");
        //2、创建定义整个应用程序所需的环境信息的 Session 对象
        Session session = Session.getInstance(props);
        //设置调试信息在控制台打印出来
        session.setDebug(true);
        //3、创建邮件的实例对象
        Message msg = createAttachMail(session);
        //4、根据session对象获取邮件传输对象Transport
        Transport transport = session.getTransport();
        //设置发件人的账户名和密码
        transport.connect(senderAccount, senderPassword);
        //发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(msg,msg.getAllRecipients());

        //如果只想发送给指定的人,可以如下写法
        //transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});

        //5、关闭邮件连接
        transport.close();
    }

    /**
     * @Description: 仅文字的邮件
     * @param session
     * @return
     * @throws MessagingException
     * @throws AddressException
     */
    public  MimeMessage getMimeMessage(Session session, Sender sender,String[] addresseeemaillist) throws Exception{
        //创建一封邮件的实例对象
        MimeMessage msg = new MimeMessage(session);
        //设置发件人地址
        msg.setFrom(new InternetAddress(sender.getSenderAddress()));
        /**
         * 设置收件人地址(可以增加多个收件人、抄送、密送),即下面这一行代码书写多行
         * MimeMessage.RecipientType.TO:发送
         * MimeMessage.RecipientType.CC:抄送
         * MimeMessage.RecipientType.BCC:密送
         */
        InternetAddress[] address = new InternetAddress[addresseeemaillist.length];
        for (int i = 0; i < addresseeemaillist.length ; i++) {
            address[i] = new InternetAddress(addresseeemaillist[i]);
        }
        msg.setRecipients(Message.RecipientType.TO,address);

        //设置邮件主题
        msg.setSubject(sender.getTitle(),"UTF-8");
        //设置邮件正文
        msg.setContent(sender.getBody(), "text/html;charset=UTF-8");
        //将创建的Email写入到D盘存储
        msg.writeTo(new FileOutputStream("D:\\huahengLog\\huahengEmail"));
        //设置邮件的发送时间,默认立即发送
        msg.setSentDate(new Date());
        return msg;
    }

  /**
   * @Method: createAttachMail
   * @Description: 带附件的邮件
   * @Anthor:ZengBo
   * @param session
   * @throws Exception
   */
   public static MimeMessage createAttachMail(Session session) throws Exception {
       MimeMessage message = new MimeMessage(session);

       //设置邮件的基本信息
       //发件人
       message.setFrom(new InternetAddress(senderAddress));
       //收件人
       message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));
       //邮件标题
       message.setSubject("JavaMail邮件发送测试");

       //创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
       MimeBodyPart text = new MimeBodyPart();
       text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");

       //这个方法用于获取当前文件夹下的最新的文件
       File file = new File("D:\\huahengLog\\sys-error");
       TreeMap<Long, File> treemap = new TreeMap();;
       for (File f : file.listFiles()) {
           long time = f.lastModified();
           treemap.put(time, f);
       }
       File rs = treemap.lastEntry().getValue();

       //创建邮件附件
       MimeBodyPart attach = new MimeBodyPart();
       DataHandler dh = new DataHandler(new FileDataSource(rs));
       attach.setDataHandler(dh);
       attach.setFileName(dh.getName());

       //创建容器描述数据关系
       MimeMultipart mp = new MimeMultipart();
       mp.addBodyPart(text);
       mp.addBodyPart(attach);
       mp.setSubType("mixed");

       message.setContent(mp);
       message.saveChanges();
       //将创建的Email写入到D盘存储
       message.writeTo(new FileOutputStream("D:\\huahengLog\\huahengEmail"));
       //返回生成的邮件
       return message;
   }
}