java 发送邮件

package com.ss.services.request;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gep.core.util.InputChecker;
import com.ss.common.constants.CommonConstants;


@Service
public class MailServices {
	private static Logger log = LoggerFactory.getLogger(MailServices.class);

    /** 
     *  
     * 方法名: sendMail  
     * 描述: 发送邮件 
     * 创建人: 徐小骥 
     * 创建时间: 2018年5月2日 下午2:51:32 
     * 版本号: v1.0    
     * 抛出异常: 
     * 参数: @param tatil 	主题
     * 参数: @param toMail  	收件人,号隔开
     * 参数: @param ccMail 	抄送人,号隔开
     * 参数: @param content  	内容
     * 参数: @param List fileUrlList 附件路径(支持多附件)
     * 参数: @return 
     * 返回类型: boolean 
     */  
    public static boolean sendMail(String tatil, String toMail,String ccMail, String content,List fileUrlList) {    
      log.info("MailServices  sendMail tatil:{},toMail:{},ccMail:{}",tatil,toMail,ccMail);

    	boolean isFlag = false;  
        String mailSwitch="Y";//是否发送邮件开关
        if(mailSwitch.equals(CommonConstants.N)){
        	return true;
        }else{
	        try {    
	            String smtpFromMail = "XXXXXXXXXX";  //账号    
	        	toMail="XXXXXXXXXX,[email protected]";
	        	ccMail="XXXXXXXXXX";
	        	InternetAddress[] internetAddressTo = new InternetAddress().parse(toMail); 
	        	String pwd="XXXXXXXXXX";//QQ填写邮箱授权码 阿里邮箱填写登录密码
	            int port = 465; //端口    
	            String host = "smtp.公司域名.com";    
	                     
	            Properties props = new Properties();  
	            /*  QQ
	            String host = "smtp.qq.com";      
	           	props.put("mail.smtp.host", host);
	           	props.put("mail.smtp.port", port);     
	            props.put("mail.smtp.auth", "true");  
	            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	            */
	            //阿里邮箱
	            props.put("mail.smtp.host", host);  
	            props.put("mail.transport.protocol", "smtp");  
	            props.put("mail.smtp.auth", "true");  
	            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 使用JSSE的SSL  
	            props.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理  
	            props.put("mail.smtp.port", port);  
	            props.put("mail.smtp.socketFactory.port",port);  
	            Session session = Session.getDefaultInstance(props);    
	            session.setDebug(false);    
	    
	            MimeMessage message = new MimeMessage(session);    
	            try {    
	                message.setFrom(new InternetAddress(smtpFromMail, "测试发送邮件"));   //发件人显示名 
	                message.addRecipients(Message.RecipientType.TO, internetAddressTo);  
	                if(!InputChecker.isEmpty(ccMail)){
	    	        	InternetAddress[] internetAddressCc = new InternetAddress().parse(ccMail); 
	                	message.addRecipients(Message.RecipientType.CC, internetAddressCc);   
	                }
	                message.setSubject(tatil);    
	                message.addHeader("charset", "UTF-8");    
	                    
	                /*添加正文内容*/    
	                Multipart multipart = new MimeMultipart();    
	                BodyPart contentPart = new MimeBodyPart();    
	                contentPart.setText(content);    	    
	                contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");    
	                multipart.addBodyPart(contentPart);    
	
	                /*添加多附件*/    
	                if(!InputChecker.isEmpty(fileUrlList)){
	                	for(String fileUrl:fileUrlList){
		        		String fileName =fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.length());//文件名
		        		BufferedInputStream   bis = new BufferedInputStream (new FileInputStream(fileUrl));  
		
		                MimeBodyPart fileBody = new MimeBodyPart();    
		                DataSource source = new ByteArrayDataSource(bis, "application/msexcel");   
		                fileBody.setDataHandler(new DataHandler(source));    
		                // 中文乱码问题  
		                fileBody.setFileName(MimeUtility.encodeText(fileName));    
		                multipart.addBodyPart(fileBody); 
		                bis.close();
	                	}
	                }
	    
	                message.setContent(multipart);    
	                message.setSentDate(new Date());    
	                message.saveChanges();    
	                Transport transport = session.getTransport("smtp");    	    
	                transport.connect(host, port, smtpFromMail, pwd);    
	                transport.sendMessage(message, message.getAllRecipients());    
	                transport.close();    
	                isFlag = true;    
	            } catch (Exception e) {    
	                e.printStackTrace();  
	                isFlag = false;    
	            }    
	        } catch (Exception e) {    
	            e.printStackTrace();    
	        }    
	        return isFlag;    
	    }
    }
}


你可能感兴趣的:(其他,java,邮件)