Commit c7900db5 authored by Quxl's avatar Quxl
parents 9659c747 eb909335
......@@ -78,6 +78,62 @@ public class MailUtil {
}
public static void sendBySmtps(String host, Integer port, final String username, final String password, final String from, String subject, String content, File[] attachments, String... emailTo) {
sendBySmtps(host, port, username, password, from, null, subject, content, attachments, emailTo);
/*boolean is_auth = StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password);
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtps");
properties.setProperty("mail.smtp.auth", is_auth ? "true" : "false");
properties.setProperty("mail.smtp.host", String.valueOf(host));
properties.setProperty("mail.smtp.port", String.valueOf(port));
properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
try {
Session session = is_auth ? Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}) : Session.getDefaultInstance(properties);
Message message = new MimeMessage(session);
Address fromAddress = new InternetAddress(from, "上海国际电影节报名 SIFF PROJECT SUBMISSION", "UTF-8");
message.setFrom(fromAddress);
List<Address> addresses = new ArrayList<Address>();
for(String email : emailTo) {
addresses.add(new InternetAddress(email));
}
message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
message.setSubject(subject);
message.setSentDate(new Date());
Multipart m = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=gb2312");
m.addBodyPart(bp);
message.setContent(m);
if(attachments != null) {
for(File file : attachments) {
MimeBodyPart mbpFile = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(file);
mbpFile.setDataHandler(new DataHandler(fileDataSource));
String filename = fileDataSource.getName();
String name = filename.substring(0, filename.lastIndexOf("."));
String exname = filename.substring(filename.lastIndexOf("."));
String encodeName = MimeUtility.encodeText(name) + exname;
mbpFile.setFileName(encodeName);
m.addBodyPart(mbpFile);
}
}
Transport.send(message);
} catch (Exception e) {
throw new PluginException(e);
}*/
}
public static void sendBySmtps(String host, Integer port, final String username, final String password, final String from, final String fromName, String subject, String content, File[] attachments, String... emailTo) {
System.out.println(fromName);
boolean is_auth = StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password);
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtps");
......@@ -94,7 +150,12 @@ public class MailUtil {
}
}) : Session.getDefaultInstance(properties);
Message message = new MimeMessage(session);
Address fromAddress = new InternetAddress(from);
Address fromAddress;
if(fromName!=null) {
fromAddress = new InternetAddress(from, fromName, "UTF-8");
}else {
fromAddress = new InternetAddress(from);
}
message.setFrom(fromAddress);
List<Address> addresses = new ArrayList<Address>();
for(String email : emailTo) {
......@@ -132,6 +193,6 @@ public class MailUtil {
}
public static void main(String[] args) {
MailUtil.sendBySmtps("smtphz.qiye.163.com", 994, "filmpassword@siff.com", "siffstvf2018", "filmpassword@siff.com", "测试", "测试", null, "747539993@qq.com");
MailUtil.sendBySmtps("smtphz.qiye.163.com", 994, "filmpassword@siff.com", "siffstvf2018", "filmpassword@siff.com","上海国际电影节", "测试", "测试", null, "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