java實現(xiàn)發(fā)送郵件嘱丢,可以帶附件發(fā)送薪介。。屿讽。昭灵。。伐谈。
pom.xml:
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
java代碼:
package com.jd.blackdragon.storm.service.impl;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.Properties;
/**
* Created by dell on 2018-04-15.
*/
public class SendEmail {
//發(fā)件郵箱的地址
private final static String MAIL_NAME = "username";
private final static String MAIL_ADDR = "username@xxx.com";
//發(fā)件郵箱的密碼
private final static String MAILPWD = "password";
//設(shè)置發(fā)送郵件的協(xié)議,下面是163郵箱的SMTP服務(wù)器
private final static String SMTP_HOST = "smtp.163.local";
//主程序測試
public static void main(String[] args) {
//郵件內(nèi)容
String content = "明細數(shù)據(jù)詳見附件:";
//郵件主題
String topic = "數(shù)據(jù)";
//接收郵箱地址
String address = "123@xxx.com";
//發(fā)送附件路徑
String filename = "F:/home/test/data.txt";
//帶附件發(fā)送
boolean tag = sendMail(topic, content, address, filename);
System.out.println("帶附件發(fā)送"+tag);
//不帶附件發(fā)送
boolean tag1 = sendMail(topic, content, address);
System.out.println("不帶附件發(fā)送" + tag1);
}
/**
* @author yanawang
* @Title: sendMail
* @Description: 發(fā)送郵件(可以進行帶附件發(fā)送, 參數(shù)最后為附件地址)
* @date 2018-2-24 下午 02:28
* 參數(shù)順序說明:String topic, String content, String address,String file(文件地址選填)
* 參數(shù)至少為3個(標(biāo)題烂完、內(nèi)容、接收人地址)
* 接收人地址可以設(shè)置多個诵棵,使用','進行分割抠蚣。如:123@163.com,456@163.com
*/
// public static boolean sendMail(String topic, String content, String address) {
public static boolean sendMail(String... strArray) {
boolean bool = false;
System.out.println(strArray.length);
if (strArray.length < 3) {
return bool;
}
try {
String topic = strArray[0];
String content = strArray[1];
String address = strArray[2];
Address[] addressTO = setAddressTo(address);
MimeMessage message = setMessage(addressTO, topic);
/*添加正文內(nèi)容*/
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
// contentPart.setText(content);
contentPart.setContent(content, "text/html;charset=UTF-8");
contentPart.setHeader("Content-Type", "text/html; charset=utf-8");
multipart.addBodyPart(contentPart);
/*添加附件*/
if (strArray.length > 3) {
String file = strArray[3];
File usFile = new File(file);
MimeBodyPart fileBody = new MimeBodyPart();
DataSource source = new FileDataSource(file);
fileBody.setDataHandler(new DataHandler(source));
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
fileBody.setFileName("=?GBK?B?"
+ enc.encode(usFile.getName().getBytes()) + "?=");
multipart.addBodyPart(fileBody);
}
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport.send(message);
bool = true;
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* @author yanawang
* @Title: setMessage
* @Description: 設(shè)置郵箱信息
* @date 2018-2-5 上午 11:20
*/
private static MimeMessage setMessage(Address[] addressTO, String topic)
throws MessagingException {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.user", MAIL_NAME);
props.put("mail.addr", MAIL_ADDR);
props.put("mail.password", MAILPWD);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props
.getProperty("mail.password");
return new PasswordAuthentication(userName,
password);
}
};
Session mailSession = Session.getInstance(props,
authenticator);
MimeMessage message = new MimeMessage(mailSession);
InternetAddress form = new InternetAddress(
props.getProperty("mail.addr"));
message.setFrom(form);
message.setRecipients(Message.RecipientType.TO, addressTO);
message.setSubject(topic);
message.addHeader("charset", "UTF-8");
return message;
}
/**
* @author yanawang
* @Title: setAddressTo
* @Description: 設(shè)置郵箱地址
* @date 2018-2-5 上午 11:20
*/
private static Address[] setAddressTo(String address) throws AddressException {
String[] tmpAddress = address.split(",");
Address[] addressTO = new InternetAddress[tmpAddress.length];
for (int i = 0; i < tmpAddress.length; i++) {
addressTO[i] = new InternetAddress(tmpAddress[i]);
}
return addressTO;
}
}