package com.company.email;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Email {
private static final String TYPE = "text/html;charset=UTF-8";
private static final String MAIL_SMTP_AUTH = "true";
private static final String MAIL_SMTP_HOST = "smtp.163.com";
private String username;
private String password;
public Email(String username, String password) {
this.username = username;
this.password = password;
}
public static void main(String[] args) throws Exception {
Email email = new Email("zhangsan@163.com", "123456");
email.sendTo("lisi@sina.com", "wangwu@qq.com", "hello", "How are you?");
}
public void sendTo(String receiver, String ccReceiver, String subject,
String content) throws Exception {
MimeMessage message = createMimeMessage(username, password);
message.setFrom(new InternetAddress(username));
message.setRecipient(RecipientType.TO, new InternetAddress(receiver));
message.setRecipient(RecipientType.CC, new InternetAddress(ccReceiver));
message.setSubject(subject);
message.setContent(content, TYPE);
Transport.send(message);
}
public static MimeMessage createMimeMessage(String username, String password) {
Properties properties = createProperties(username, password);
Authenticator authenticator = createAuthenticator(username, password);
return new MimeMessage(Session.getInstance(properties, authenticator));
}
public static Properties createProperties(String username, String password) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", MAIL_SMTP_AUTH);
properties.put("mail.smtp.host", MAIL_SMTP_HOST);
return properties;
}
public static Authenticator createAuthenticator(final String username,
final String password) {
return new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
pom.xml 中依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
linux环境
public class EmailUtils {
private static final String TYPE = "text/html;charset=UTF-8";
private static final String TRUE = "true";
private static final String MAIL_SMTP_HOST = "smtp.163.com";
private static final String MAIL_SMTP_PORT = "465";
private static String username = "xxxxxx@163.com";
private static String password = "xxxxx"; //授权码,非密码
public static void sendTo(String subject, String content, String receiver, String... ccReceivers) throws Exception {
if (StringUtils.isEmpty(content)) {
return;
}
System.out.println("邮件内容:" + content);
MimeMessage message = createMimeMessage(username, password);
message.setFrom(new InternetAddress(username));
message.setRecipient(RecipientType.TO, new InternetAddress(receiver));
for (String ccReceiver : ccReceivers) {
message.addRecipients(RecipientType.CC, ccReceiver);
}
message.setSubject(subject);
message.setContent(content, TYPE);
Transport.send(message);
}
public static MimeMessage createMimeMessage(String username, String password) {
return new MimeMessage(Session.getInstance(createProperties(), createAuthenticator(username, password)));
}
public static Properties createProperties() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", TRUE);
properties.put("mail.smtp.host", MAIL_SMTP_HOST);
properties.put("mail.smtp.port", MAIL_SMTP_PORT); //更改端口号25为465,然后设置SSL协议
properties.put("mail.smtp.ssl.enable", TRUE); //打开SSL协议
// properties.put("mail.debug", TRUE);
return properties;
}
public static Authenticator createAuthenticator(final String username, final String password) {
return new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
}
}