• jar包:mail.jar & activation.jar
  • 方法类
import java.util.Date;
import java.util.Properties;

import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import javax.mail.internet.MimeUtility;

public class Mail extends Authenticator {

private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _subject;
private String _body;
private Multipart _multipart;

private Mail() {
this._multipart = new MimeMultipart();
MailcapCommandMap commandMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
commandMap.addMailcap("text/html;;x-java-content-handler=com.sun.mail.handlers.text_html");
commandMap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
commandMap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
commandMap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
commandMap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(commandMap);
}

public Mail(String user, String pass) {
this();
this._user = user;
this._pass = pass;
}

public void set_from(String _from) {
this._from = _from;
}

public void set_to(String[] _to) {
this._to = _to;
}

public void set_subject(String _subject) {
this._subject = _subject;
}

public void set_body(String _body) {
this._body = _body;
}


/* 添加附件 */
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
DataHandler dataHandler = new DataHandler(source);
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setFileName(MimeUtility.encodeText(dataHandler.getName())); // 解决中文附件名乱码
_multipart.addBodyPart(messageBodyPart);
}

public boolean send() throws Exception {
Properties props = _setProperties();
if (!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Authentication authentication = new Authentication(_user, _pass);
Session session = Session.getDefaultInstance(props, authentication);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
msg.setContent(_multipart);
Transport.send(msg);
return true;
} else {
return false;
}
}


private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", "---");
props.put("mail.smtp.port", "---");
props.put("mail.protocol", "smtp");
props.put("mail.useSSL", "false");
props.put("mail.smtp.auth", "true");
return props;
}


@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}


/**
* 公共方法:发送邮件
*/
public static void send(String username, String password, String[] receivers, String title, String content, String... filePaths) {
new Thread(() -> {
Mail mail = new Mail(username, password);
mail.set_to(receivers);
mail.set_from(username);
mail.set_subject(title);
mail.set_body(content);
try {
for (String path : filePaths) {
mail.addAttachment(path);
}
if (mail.send()) {

} else {

}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
  • 调用示例
public class MailTest {
public static void main(String[] args) {
String username = "---@gmail.com";
String password = "******";
String[] recievers = {
"---1@gmail.com",
"---2@gmail.com",
};
String title = "title";
String content = "content";

Mail.send(username, password, recievers, title, content);
}
}