();
map.put("state", "success");
String message = "邮件发送成功!";
Session session = null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
if(isExchange){
if(domain == null || domain.equals("")){
throw new RuntimeException("domain is null");
}
props.setProperty("mail.smtp.auth.ntlm.domain", domain);
}
if (ifAuth) { // 服务器需要身份认证
props.put("mail.smtp.auth", "true");
MailAuthenticator smtpAuth = new MailAuthenticator(username, password);
session = Session.getDefaultInstance(props, smtpAuth);
} else {
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
}
session.setDebug(true);
Transport trans = null;
try {
Message msg = new MimeMessage(session);
try {
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), "text/html;charset=gb2312");
mp.addBodyPart(mbp);
if (!file.isEmpty()) {// 有附件
Enumerationefile = file.elements();
while (efile.hasMoreElements()) {
mbp = new MimeBodyPart();
filename = efile.nextElement().toString(); // 选择出每一个附件名
FileDataSource fds = new FileDataSource(filename); // 得到数据源
mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); // 得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
file.removeAllElements();
}
msg.setContent(mp); // Multipart加入到信件
msg.setSentDate(new Date()); // 设置信件头的发送日期
// 发送信件
msg.saveChanges();
trans = session.getTransport("smtp");
trans.connect(smtpServer, username, password);
trans.sendMessage(msg, msg.getAllRecipients());
trans.close();
} catch (AuthenticationFailedException e) {
map.put("state", "failed");
message = "邮件发送失败!错误原因:\n" + "身份验证错误!";
e.printStackTrace();
} catch (MessagingException e) {
message = "邮件发送失败!错误原因:\n" + e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
}
// System.out.println("\n提示信息:"+message);
map.put("message", message);
return map;
}
public static void main(String[] args) {
String smtpServer = "exchange.be***.com.cn";
String from = "*******@be***.com.cn";
String userName = "*******";
String password = "*******";
String to = "*******@qq.com";
String subject = "你好";
String content = "你好啊";
MailTool mail = new MailTool(smtpServer, from, "BES**YDJD", userName, password,
to, subject, content);
mail.send();
}
}
package tool.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
*
* 服务器邮箱登录验证
*
*/
public class MailAuthenticator extends Authenticator {
/**
*
* 用户名(登录邮箱)
*/
private String username;
/**
* 密码
*/
private String password;
/**
*
* 初始化邮箱和密码
*
* @param username
* 邮箱
*
* @param password
* 密码
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}