java发送邮件基础方法(另附部分主流邮箱服务器地址、端口及设置方法)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java发送邮件基础方法(另附部分主流邮箱服务器地址、端口及设置方法)

ggwudivs   2019-12-17 我要评论

java发送邮件基础方法,可通过重载简化参数

  1 import java.io.File;
  2 import java.io.UnsupportedEncodingException;
  3 import java.util.Properties;
  4 
  5 import javax.activation.DataHandler;
  6 import javax.activation.FileDataSource;
  7 import javax.mail.Authenticator;
  8 import javax.mail.BodyPart;
  9 import javax.mail.Message.RecipientType;
 10 import javax.mail.MessagingException;
 11 import javax.mail.Multipart;
 12 import javax.mail.PasswordAuthentication;
 13 import javax.mail.Session;
 14 import javax.mail.Transport;
 15 import javax.mail.internet.InternetAddress;
 16 import javax.mail.internet.MimeBodyPart;
 17 import javax.mail.internet.MimeMessage;
 18 import javax.mail.internet.MimeMultipart;
 19 import javax.mail.internet.MimeUtility;
 20 
 21 public class MailUtil {
 22 
 23     /**
 24      * @Description: 使用QQ邮箱发送不带附件的邮件,收件人邮箱类型不限。
 25      * @date: 2019年12月17日 下午4:51:01
 26      * @author: ggwudivs
 27      * @param subject        邮件标题
 28      * @param content        邮件内容
 29      * @param fromUser        发件人邮箱
 30      * @param fromPass        发件人邮箱密码,应为16位SMTP口令
 31      * @param tO_Recipients    收件人邮箱,多个收件人用","分隔
 32      * @param openSSL        是否开启SSL
 33      * @throws UnsupportedEncodingException
 34      * @throws MessagingException:
 35      * @return: void
 36      */
 37     public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, boolean openSSL) throws UnsupportedEncodingException, MessagingException{
 38         sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", null, openSSL);
 39     }
 40     
 41     /**
 42      * @Description: 使用QQ邮箱发送带附件的邮件,收件人邮箱类型不限。
 43      * @date: 2019年12月17日 下午5:25:14
 44      * @author: ggwudivs
 45      * @param subject                邮件标题
 46      * @param content                邮件内容
 47      * @param fromUser                发件人邮箱
 48      * @param fromPass                发件人邮箱密码,应为16位SMTP口令
 49      * @param tO_Recipients            收件人邮箱,多个收件人用","分隔
 50      * @param attachmentFilesPath    邮件附件路径,多个附件用","分隔
 51      * @param openSSL                是否开启SSL
 52      * @throws UnsupportedEncodingException
 53      * @throws MessagingException:
 54      * @return: void
 55      */
 56     public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, String attachmentFilesPath, boolean openSSL) throws UnsupportedEncodingException, MessagingException{
 57         sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", attachmentFilesPath, openSSL);
 58     }
 59     
 60     /**
 61      * @Description: smtp发送邮件
 62      * @date: 2019年12月17日 下午3:22:35
 63      * @author: ggwudivs
 64      * @param subject                邮件标题
 65      * @param content                邮件文本内容
 66      * @param fromUser                发件人邮箱
 67      * @param fromPass                发件人邮箱密码,QQ邮箱应为16位SMTP口令
 68      * @param nickname                发件人昵称
 69      * @param tO_Recipients            收件人邮箱,多个收件人用","分隔
 70      * @param cC_Recipients            抄送人邮箱,多个抄送人用","分隔
 71      * @param bCC_Recipients        密送人邮箱,多个密送人用","分隔
 72      * @param smtpHost                smtp服务器地址
 73      * @param smtpPort                smtp服务器端口
 74      * @param attachmentFilesPath    邮件附件路径,多个附件用","分隔
 75      * @throws MessagingException
 76      * @throws UnsupportedEncodingException:
 77      * @return: void
 78      */
 79     public static void sendMessage(String subject, String content, String fromUser, String fromPass, String nickname, String tO_Recipients, String cC_Recipients, String bCC_Recipients, String smtpHost, String smtpPort, String attachmentFilesPath, boolean openSSL) throws MessagingException, UnsupportedEncodingException {  
 80         //创建Properties类,用于记录邮箱的一些属性
 81         Properties props = new Properties();
 82         //表示SMTP发送邮件,必须进行身份验证
 83         props.put("mail.smtp.auth", "true");
 84         //SMTP服务器地址
 85         props.put("mail.smtp.host", smtpHost);
 86         //是否开启SSL
 87         if(openSSL){
 88             //SMTP服务器端口号
 89             props.put("mail.smtp.port", smtpPort);
 90         }else{
 91             //SMTP服务器ssl端口号
 92             props.setProperty("mail.smtp.socketFactory.port", smtpPort);
 93             props.setProperty("mail.smtp.socketFactory.fallback", "false");
 94             props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
 95         }
 96 
 97         //构建授权信息,用于进行SMTP进行身份验证
 98         Authenticator authenticator = new Authenticator() {
 99             protected PasswordAuthentication getPasswordAuthentication() {
100                 //发件人账号,发件人密码(QQ邮箱应为16位SMTP口令)
101                 return new PasswordAuthentication(fromUser, fromPass);
102             }
103         };
104         
105         //使用环境属性和授权信息,创建邮件会话
106         Session mailSession = Session.getInstance(props, authenticator);
107         
108         //创建邮件消息
109         MimeMessage message = new MimeMessage(mailSession);
110         
111         //设置发件人,有昵称时同时设置昵称
112         try {
113             message.setFrom((nickname==null||"".equals(nickname))?new InternetAddress(fromUser):new InternetAddress(fromUser, nickname, "UTF-8"));
114         } catch (UnsupportedEncodingException e) {
115             e.printStackTrace();
116         }
117 
118         //设置一个或多个收件人
119         message.setRecipients(RecipientType.TO, tO_Recipients);
120         
121         //设置一个或多个抄送人
122         message.setRecipients(RecipientType.CC, cC_Recipients);
123         
124         //设置一个或多个密送人
125         message.setRecipients(RecipientType.BCC, bCC_Recipients);
126 
127         //设置邮件标题
128         message.setSubject(subject);
129 
130         //设置邮件的内容
131         if(attachmentFilesPath == null || "".equals(attachmentFilesPath)){
132             //设置邮件的正文文本
133             message.setContent(content, "text/html;charset=UTF-8");
134         }else{
135             //向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
136             Multipart multipart = new MimeMultipart();
137             
138             //添加邮件文本内容
139             BodyPart contentBodyPart = new MimeBodyPart();
140             contentBodyPart.setContent(content, "text/html;charset=utf-8");
141             multipart.addBodyPart(contentBodyPart);
142             
143             //添加邮件附件内容
144             BodyPart attachmentBodyPart = new MimeBodyPart();
145             String[] attachmentFiles = attachmentFilesPath.split(",");
146             for (String attachmentFile : attachmentFiles) {
147                 if (attachmentFile != null && !"".equals(attachmentFile)) {
148                     attachmentBodyPart = new MimeBodyPart(); 
149                     
150                     //根据附件路径获取文件,
151                     FileDataSource dataSource = new FileDataSource(new File(attachmentFile));
152                     attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
153                     
154                     //MimeUtility.encodeWord可以避免文件名乱码
155                     String strFileName=dataSource.getFile().getName();
156                     attachmentBodyPart.setFileName(MimeUtility.encodeText(strFileName));  
157                     
158                     multipart.addBodyPart(attachmentBodyPart);
159                 }
160             }
161             
162             //设置邮件的正文内容
163             message.setContent(multipart);
164         }
165         
166         //发送邮件
167         Transport.send(message);
168     }  
169 }
邮箱类型 SMTP服务器地址 普通端口 SSL端口 服务器配置参考地址
QQ邮箱 smtp.qq.com 587 465 https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=167
阿里企业邮箱 smtp.qiye.aliyun.com 25 465 http://mailhelp.mxhichina.com/smartmailhttps://img.qb5200.com/download-x/detail.vm?knoId=5871700
网易163免费邮箱 smtp.163.com 25 465/994 https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac22dc0e9af8168582a
网易企业邮箱 smtp.qiye.163.com 25 994 https://qiye.163.com/help/client-profile.html
网易免费企业邮箱 smtp.ym.163.com 25 994 http://app.ym.163.com/ym/help/help.html

 

 

 

 

 

tips:

QQ邮箱需先设置独立密码才能使用smtp功能(https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001220&&id=28)。

QQ邮箱设置方法:设置--账户--账户安全--独立密码。

网易163免费邮箱同QQ邮箱一样,需先设置授权码才能使用smtp功能(https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac24a2130dd2fad05b1)

网易163免费邮箱开启授权码方法:https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516

网易企业邮箱在开启客户端授权密码的功能时才需要设置客户端授权码:https://qiye.163.com/help/3f85a9.html

网易企业邮箱开启授权码方法:https://qiye.163.com/help/af988e.html

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们