JAVA MAIL邮件发送

package Util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import Joyin.Idc.Core.Beans.Util.GlobalConfig;

public class SendEmail {

private  String emailSubjectTxt = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime()) + "理财兑付数据";


public void postMail(String[] recipients, String message,
        String files){
    try {

        boolean debug = true;
         Properties props = new Properties();   
         String SMTP_HOST_NAME = GlobalConfig.getProperty("/file.properties",
                    "SMTP_HOST_NAME");
        String SMTP_PORT = GlobalConfig.getProperty("/file.properties",
                    "SMTP_PORT");
        String SMTP_AUTH_USER = GlobalConfig.getProperty("/file.properties",
                    "SMTP_AUTH_USER");
        String SMTP_AUTH_PWD = GlobalConfig.getProperty("/file.properties",
                "SMTP_AUTH_PWD");
         props.setProperty("mail.host", SMTP_HOST_NAME);
         props.setProperty("mail.transport.protocol", "smtp");
         props.setProperty("mail.smtp.auth", "true");
         //props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         props.setProperty("mail.smtp.port",SMTP_PORT);
         props.setProperty("mail.smtp.socketFactory.port",SMTP_PORT);
         props.setProperty("mail.smtp.starttls.enable", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(debug);

         Transport transport = session.getTransport("smtp");//"smtps"
         transport.connect(SMTP_HOST_NAME,SMTP_AUTH_USER,SMTP_AUTH_PWD);


         MimeMessage msg = new MimeMessage(session);
         msg.setSubject(emailSubjectTxt);
         System.out.print("发送邮箱:");
        for(int i = 0 ;i< recipients.length; i ++){
            System.out.print(recipients[i]+" ");
        }


          //--------发送地址------
        msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
            msg.addRecipient(Message.RecipientType.TO, addressTo[i]);
        }

        MimeMultipart mp = new MimeMultipart("mixed");
        msg.setContent(mp);
        msg.saveChanges();

        //------文件发送--------

        if (files != null && files.length() > 0) {  //邮件附件
                 System.out.println("需要发送附件!");               
                  MimeBodyPart mbp = new MimeBodyPart();
                  FileDataSource fds = new FileDataSource(new File(files));
                  mbp.setDataHandler(new DataHandler(fds));
                  try {
                    mbp.setFileName(java.net.URLDecoder.decode(MimeUtility.encodeWord(files.substring(files.lastIndexOf("/")+1)), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                  mp.addBodyPart(mbp);
              }
        MimeBodyPart ms = new MimeBodyPart();
        ms.setContent(message, "text/plain;charset=UTF-8");
        mp.addBodyPart(ms);  

//      msg.setContent(mp);  
        System.out.println("开始发送邮件~");
        Transport.send(msg);
        System.out.println("发送邮件成功~");
        transport.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private class SMTPAuthenticator extends javax.mail.Authenticator {
    String SMTP_AUTH_USER = GlobalConfig.getProperty("/file.properties",
                "SMTP_AUTH_USER");
    String SMTP_AUTH_PWD = GlobalConfig.getProperty("/file.properties",
            "SMTP_AUTH_PWD");
    public PasswordAuthentication getPasswordAuthentication() {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值