package com.stylefeng.guns.modular.system.utils;
|
|
import javax.activation.DataHandler;
|
import javax.activation.FileDataSource;
|
import javax.mail.*;
|
import javax.mail.internet.*;
|
import java.io.UnsupportedEncodingException;
|
import java.util.Date;
|
import java.util.Properties;
|
|
public class EmailUtil {
|
|
|
private static String from = "andy.wang@cca19.com";//发送者的谷歌邮箱
|
private static String password = "Zhang20140616_";//谷歌邮箱密码
|
public static boolean sendMailGMail(String to, String content) {
|
return gmailSender(from,password,to,"标题", content);
|
}
|
|
public static void main(String[] args) {
|
sendMailGMail("1793218484@qq.com","a");
|
}
|
|
private static void gmailSsl(Properties props) {
|
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
props.put("mail.debug", "true");
|
props.put("mail.smtp.host", "smtp.gmail.com");
|
props.put("mail.smtp.ssl.enable", "true");
|
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
|
props.put("mail.smtp.port", "465");
|
//props.put("mail.smtp.port", "587");
|
props.put("mail.smtp.socketFactory.port", "465");
|
//props.put("mail.smtp.socketFactory.port", "587");
|
props.put("mail.smtp.auth", "true");
|
}
|
|
|
public static boolean gmailSender(String from,String password,String email,String subject,String content) {
|
|
Properties props = new Properties();
|
gmailSsl(props);
|
Session session = Session.getDefaultInstance(props,
|
new Authenticator() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(from, password);
|
}
|
});
|
Message msg = new MimeMessage(session);
|
|
try {
|
msg.setFrom(new InternetAddress(from));
|
msg.setRecipients(Message.RecipientType.TO,
|
InternetAddress.parse(email));
|
msg.setSubject(subject);
|
msg.setText(content);
|
msg.setSentDate(new Date());
|
Transport.send(msg);
|
} catch (AddressException e) {
|
e.printStackTrace();
|
} catch (MessagingException e) {
|
e.printStackTrace();
|
}
|
return true;
|
}
|
|
public static void sendMail(String mailHost, String fromMail, String fromName, String fromMailPwd,
|
String toMails, String mailTitle, String mailContent) throws Exception {
|
final String username = "andy.wang@cca19.com";
|
final String password = "Zhang20140616_";
|
|
Properties props = new Properties();
|
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.starttls.enable", "true");
|
props.put("mail.smtp.host", "smtp.gmail.com");
|
props.put("mail.smtp.port", "587");
|
|
Session session = Session.getInstance(props,
|
new javax.mail.Authenticator() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(username, password);
|
}
|
});
|
|
try {
|
Message message = new MimeMessage(session);
|
message.setFrom(new InternetAddress("1793218484@qq.com"));
|
message.setRecipients(Message.RecipientType.TO,
|
InternetAddress.parse("1793218484@qq.com"));
|
message.setSubject("Testing Subject");
|
message.setText("Dear Mail Crawler,"
|
+ "\n\n No spam to my email, please!");
|
|
Transport.send(message);
|
|
System.out.println("Done");
|
|
} catch (MessagingException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
}
|