|
@@ -7,15 +7,7 @@ import java.io.UnsupportedEncodingException;
|
|
|
import java.util.Date;
|
|
|
import java.util.Properties;
|
|
|
import java.util.UUID;
|
|
|
-//import java.util.HashMap;
|
|
|
-//import java.util.Base64;
|
|
|
-//import com.google.gson.GsonBuilder;
|
|
|
-//import javax.activation.DataHandler;
|
|
|
-//import javax.activation.FileDataSource;
|
|
|
-//import java.net.URL;
|
|
|
-//import java.net.URLEncoder;
|
|
|
-//import javax.activation.DataHandler;
|
|
|
-//import javax.activation.URLDataSource;
|
|
|
+
|
|
|
|
|
|
public class SampleMail {
|
|
|
protected static String genMessageID(String mailFrom) {
|
|
@@ -103,6 +95,80 @@ public class SampleMail {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public void sendAuthCodePortalmcc(String toEmail, String content) {
|
|
|
+ // 配置发送邮件的环境属性
|
|
|
+ final Properties props = new Properties();
|
|
|
+
|
|
|
+ // 表示SMTP发送邮件,需要进行身份验证
|
|
|
+ props.put("mail.smtp.auth", "true");
|
|
|
+ props.put("mail.smtp.host", "smtpdm.aliyun.com"); //华东1:smtpdm.aliyun.com,悉尼:smtpdm-ap-southeast-2.aliyun.com,(美国(原悉尼)):smtpdm-us-east-1.aliyuncs.com
|
|
|
+ //设置端口:
|
|
|
+ props.put("mail.smtp.port", "80"); //或"25"
|
|
|
+ props.put("mail.smtp.from", "support@portalmcc.com.cn"); //mailfrom 参数
|
|
|
+ props.put("mail.user", "support@portalmcc.com.cn"); // 发件人的账号(在控制台创建的发信地址)
|
|
|
+ props.put("mail.password", "DirectMail321"); // 发信地址的smtp密码(在控制台选择发信地址进行设置)
|
|
|
+ System.setProperty("mail.mime.splitlongparameters", "false"); //用于解决附件名过长导致的显示异常
|
|
|
+
|
|
|
+ // 构建授权信息,用于进行SMTP进行身份验证
|
|
|
+ Authenticator authenticator = new Authenticator() {
|
|
|
+ @Override
|
|
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ // 用户名、密码
|
|
|
+ String userName = props.getProperty("mail.user");
|
|
|
+ String password = props.getProperty("mail.password");
|
|
|
+ return new PasswordAuthentication(userName, password);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ //使用环境属性和授权信息,创建邮件会话
|
|
|
+ Session mailSession = Session.getInstance(props, authenticator);
|
|
|
+ //mailSession.setDebug(true); //开启debug模式
|
|
|
+
|
|
|
+
|
|
|
+ final String messageIDValue = genMessageID(props.getProperty("mail.user"));
|
|
|
+ //创建邮件消息
|
|
|
+ MimeMessage message = new MimeMessage(mailSession) {
|
|
|
+ @Override
|
|
|
+ protected void updateMessageID() throws MessagingException {
|
|
|
+ //设置自定义Message-ID值
|
|
|
+ setHeader("Message-ID", messageIDValue); //创建Message-ID
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 设置发件人邮件地址和名称。填写控制台配置的发信地址。和上面的mail.user保持一致。名称用户可以自定义填写。
|
|
|
+ InternetAddress from = new InternetAddress("support@portalmcc.com.cn", "Cotton Candy Robot"); //from 参数,可实现代发,注意:代发容易被收信方拒信或进入垃圾箱。
|
|
|
+ message.setFrom(from);
|
|
|
+
|
|
|
+ //可选。设置回信地址
|
|
|
+ Address[] a = new Address[1];
|
|
|
+ a[0] = new InternetAddress(toEmail);
|
|
|
+ message.setReplyTo(a);
|
|
|
+
|
|
|
+ // 设置收件人邮件地址
|
|
|
+ InternetAddress to = new InternetAddress(toEmail);
|
|
|
+ message.setRecipient(MimeMessage.RecipientType.TO, to);
|
|
|
+
|
|
|
+
|
|
|
+ message.setSentDate(new Date()); //设置时间
|
|
|
+
|
|
|
+ //设置邮件标题
|
|
|
+ message.setSubject("Verification Code");
|
|
|
+ message.setContent(content, "text/html;charset=UTF-8"); //html超文本;// "text/plain;charset=UTF-8" //纯文本。
|
|
|
+ // 发送邮件
|
|
|
+ Transport.send(message);
|
|
|
+
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ String err = e.getMessage();
|
|
|
+ // 在这里处理message内容, 格式是固定的
|
|
|
+ System.out.println(err);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
// public static void main(String[] args) {
|
|
|
// // 配置发送邮件的环境属性
|
|
|
// final Properties props = new Properties();
|