添加依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
EmailSender.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;
public class EmailSender {
public static void main(String[] args) {
String fromAddr = "*****@163.com";
String toAddr = "****@163.com";
String title = "【测试标题】Testing Subject-myself-TEXT";
String content = "【测试内容】Hello, this is sample for to check send email using JavaMailAPI ";
String port = "25";
String host = "smtp.163.com";
String userName = "****@163.com";
String password = "****";
EmailSendInfo mailInfo = new EmailSendInfo();
mailInfo.setMailServerHost(host);
mailInfo.setMailServerPort(port);
mailInfo.setValidate(true);
mailInfo.setUserName(userName);
mailInfo.setPassword(password);
mailInfo.setFromAddress(fromAddr);
mailInfo.setToAddress(toAddr);
mailInfo.setSubject(title);
mailInfo.setContent(content);
EmailSender.sendTextMail(mailInfo);
}
public static boolean sendTextMail(EmailSendInfo mailInfo) {
boolean sendStatus = false;
EmailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new EmailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
MimeMessage mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject(), "UTF-8");
mailMessage.setSentDate(new Date());
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent, "UTF-8");
createEmailFile("file/EML_myself-TEXT.eml", mailMessage);
Transport.send(mailMessage);
sendStatus = true;
} catch (MessagingException ex) {
return sendStatus;
}
return sendStatus;
}
public static boolean sendHtmlMail(EmailSendInfo mailInfo) {
boolean sendStatus = false;
EmailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new EmailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
mailMessage.setContent(mailInfo.getContent(), "text/html;charset=utf-8");
createEmailFile("file/EML_myself-HTML.eml", mailMessage);
Transport.send(mailMessage);
sendStatus = true;
} catch (MessagingException ex) {
return sendStatus;
}
return sendStatus;
}
public static boolean sendMail(EmailSendInfo mailInfo, File emailFile) {
boolean sendStatus = false;
EmailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new EmailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
InputStream source = new FileInputStream(emailFile);
Message mailMessage = new MimeMessage(sendMailSession, source);
Transport.send(mailMessage);
source.close();
sendStatus = true;
} catch (MessagingException e) {
return sendStatus;
} catch (FileNotFoundException e) {
return sendStatus;
} catch (Exception e) {
return sendStatus;
}
return sendStatus;
}
public static boolean sendAttachmentMail(EmailSendInfo mailInfo) {
boolean sendStatus = false;
EmailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new EmailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
MimeMessage mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject(), "UTF-8");
mailMessage.setSentDate(new Date());
String mailContent = mailInfo.getContent();
Multipart mainPart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(bodyPart);
String[] filenames = mailInfo.getAttachFileNames();
for (int i = 0; i < filenames.length; i++) {
bodyPart = new MimeBodyPart();
String filename = filenames[i];
FileDataSource source = new FileDataSource(filename);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(source.getName());
mainPart.addBodyPart(bodyPart);
}
mailMessage.setContent(mainPart);
createEmailFile("file/EML_reademl-eml文件_含多个附件.eml", mailMessage);
Transport.send(mailMessage);
sendStatus = true;
} catch (MessagingException ex) {
return sendStatus;
}
return sendStatus;
}
public static void createEmailFile(String fileName, Message mailMessage)
throws MessagingException {
File f = new File(fileName);
try {
mailMessage.writeTo(new FileOutputStream(f));
} catch (IOException e) {
}
}
}
EmailSendInfo.java
import java.util.Properties;
public class EmailSendInfo {
private String mailServerHost;
private String mailServerPort = "25";
private String fromAddress;
private String toAddress;
private String userName;
private String password;
private boolean validate = false;
private String subject;
private String content;
private String[] attachFileNames;
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
EmailAuthenticator
import javax.mail.*;
public class EmailAuthenticator extends Authenticator{
private String userName;
private String password;
public EmailAuthenticator(){}
public EmailAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
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;
}
}