java自带的扩展包javax实现邮件发送
public class EmailEntity {
/**
* 邮箱服务器ip
*/
private String mailServerHost;
/**
* 邮箱服务器端口
*/
private String mailServerPort;
/**
* 邮件发送者邮箱地址
*/
private String fromAddress;
/**
* 邮件发送者邮箱用户名
*/
private String username;
/**
* 邮件发送者邮箱密码
*/
private String passwd;
/**
* 收件人
*/
private String[] toAddress;
/**
* 抄送人
*/
private String[] cc;
/**
* 密送人
*/
private String[] bcc;
/**
* 是否认证邮件发送者身份
*/
private boolean validate;
/**
* 邮件主题
*/
private String subject;
/**
* 邮件内容
*/
private String content;
/**
* 邮件附件
*/
private File[] files;
public Properties getProperties(){
Properties properties = new Properties();
properties.put("mail.smtp.host", this.mailServerHost);
properties.put("mail.smtp.port", this.mailServerPort);
properties.put("mail.smtp.auth", this.validate ? "true":"false");
properties.put("mail.smtp.sendpartial", "true");
/**
* 开启tls
properties.put("mail.smtp.ssl.checkserveridentity", "false");
//添加信任的服务器地址,多个地址之间用空格分开
properties.put("mail.smtp.ssl.trust","0.0.0.0");
properties.put("mail.smtp.starttls.enable", tlsEnabled);
*/
return properties;
}
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 String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String[] getToAddress() {
return toAddress;
}
public void setToAddress(String[] toAddress) {
this.toAddress = toAddress;
}
public String[] getCc() {
return cc;
}
public void setCc(String[] cc) {
this.cc = cc;
}
public String[] getBcc() {
return bcc;
}
public void setBcc(String[] bcc) {
this.bcc = bcc;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public File[] getFiles() {
return files;
}
public void setFiles(File[] files) {
this.files = files;
}
}
public class EmailSender {
private static Logger logger = LoggerFactory.getLogger(uyun.bird.notify.core.sender.email.MailSender.class);
public boolean sendMail(EmailEntity emailEntity){
IdentityAuthenticator authenticator = null;
if (emailEntity.isValidate()) {
authenticator = new IdentityAuthenticator(emailEntity.getUsername(), emailEntity.getPasswd());
}
Session sendMailSession = Session.getInstance(emailEntity.getProperties(), authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
mailMessage.setFrom(new InternetAddress(emailEntity.getFromAddress()));
// 收件人
if (emailEntity.getToAddress() != null && emailEntity.getToAddress().length > 0) {
for (String to : emailEntity.getToAddress()) {
if (!StringUtils.isBlank(to)) {
mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
}
}
// 抄送人
if (emailEntity.getCc() != null && emailEntity.getCc().length > 0) {
for (String cc : emailEntity.getCc()) {
if (!StringUtils.isBlank(cc)) {
mailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
}
}
// 密送人
if (emailEntity.getBcc() != null && emailEntity.getBcc().length > 0) {
for (String bcc : emailEntity.getBcc()) {
if (!StringUtils.isBlank(bcc)) {
mailMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
}
}
}
Multipart mainPart = new MimeMultipart();
File[] files = emailEntity.getFiles();
if (files != null) {
MimeBodyPart filePart;
FileDataSource filedatasource;
for (File file : files) {
filePart = new MimeBodyPart();
filedatasource = new FileDataSource(file);
filePart.setDataHandler(new DataHandler(filedatasource));
try {
filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));
} catch (Exception e) {
logger.error(String.format("file not exist", file.getName()), e);
}
mainPart.addBodyPart(filePart);
}
}
mailMessage.setSubject(emailEntity.getSubject());
mailMessage.setSentDate(new Date());
// 设置邮件内容
BodyPart html = new MimeBodyPart();
html.setContent(emailEntity.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException e) {
logger.error("mail sending fail:", e);
throw new Exception("500", e.getMessage());
}
}
}