Commit 91c1da32 authored by Quxl's avatar Quxl

x

parent 1da43d04
package com.egolm.common;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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 com.egolm.common.exception.PluginException;
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) {
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", is_auth ? "true" : "false");
if(port != null) {
properties.put("mail.smtp.port", port);
}
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);
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, 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");
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;
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) {
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 main(String[] args) {
MailUtil.sendBySmtps("smtphz.qiye.163.com", 994, "filmpassword@siff.com", "siffstvf2018", "filmpassword@siff.com","上海国际电影节", "测试", "测试", null, "13384825689@163.com");
}
}
package com.egolm.common;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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 com.egolm.common.exception.PluginException;
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) {
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", is_auth ? "true" : "false");
if(port != null) {
properties.put("mail.smtp.port", port);
}
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);
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, 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) {
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;
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) {
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 main(String[] args) {
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