Commit 799bbae7 authored by Quxl's avatar Quxl

优化发送邮件工具

parent 0b206a96
...@@ -25,21 +25,8 @@ import javax.mail.internet.MimeUtility; ...@@ -25,21 +25,8 @@ import javax.mail.internet.MimeUtility;
import com.egolm.common.exception.PluginException; import com.egolm.common.exception.PluginException;
public class MailUtil { public class MailUtil {
/** public static void sendBySmtp(String host, Integer port, boolean is_auth, final String username, final String password, final String from, String subject, String content, File[] attachments, String... emailTo) {
* 发送邮件
*
* @param host 邮件服务器地址
* @param port 邮件服务器端口
* @param is_auth 是否要求身份验证
* @param from 发送邮件地址
* @param subject 邮件主题
* @param content 邮件内容
* @param attachments 附件
* @param emailTo 接收邮件地址
* @return 是否成功
*/
public static void sendBySmtp(String host, Integer port, boolean is_auth, final String from, final String passwd, String subject, String content, File[] attachments, String... emailTo) {
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("mail.smtp.host", host); properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", is_auth ? "true" : "false"); properties.put("mail.smtp.auth", is_auth ? "true" : "false");
...@@ -49,7 +36,7 @@ public class MailUtil { ...@@ -49,7 +36,7 @@ public class MailUtil {
try { try {
Session session = is_auth ? Session.getDefaultInstance(properties, new Authenticator() { Session session = is_auth ? Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, passwd); return new PasswordAuthentication(username, password);
} }
}) : Session.getDefaultInstance(properties); }) : Session.getDefaultInstance(properties);
Message message = new MimeMessage(session); Message message = new MimeMessage(session);
...@@ -89,17 +76,4 @@ public class MailUtil { ...@@ -89,17 +76,4 @@ public class MailUtil {
throw new PluginException(e); throw new PluginException(e);
} }
} }
public static void main(String[] args) throws Exception {
// EmailSender sender = new EmailSender();
// File[] files = new File[]{new File("C:/opt/modules/dev/boot/file/excel/40001-收银员资料维护-20180813.xlsx"), new File("C:/opt/modules/dev/boot/file/2018-08-15/admin_客单价对比图表.html"), new File("C:/opt/modules/dev/boot/file/2018-08-15/admin_销售对比图表.html")};
// MailMessage mailMessage = new MailMessage("测试");
// mailMessage.setFiles(files);
// sender.send(mailMessage, "13384825689@163.com");
//String json = "{\"password\": \"hong804334\",\"port\": 25,\"protocol\": \"smtp\",\"smtpAuth\": \"true\",\"smtpHost\": \"smtp.163.com\",\"sslEnable\": \"false\",\"username\": \"13384825689@163.com\"}";
File[] files = new File[]{new File("C:/opt/modules/dev/boot/file/excel/40001-收银员资料维护-20180813.xlsx"), new File("C:/opt/modules/dev/boot/file/2018-08-15/admin_客单价对比图表.html"), new File("C:/opt/modules/dev/boot/file/2018-08-15/admin_销售对比图表.html")};
MailUtil.sendBySmtp("smtp.163.com", 25, true, "13384825689@163.com", "hong804334", "测试aaa", "测hi", files, "13384825689@163.com");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment