java邮箱exchange_Exchange服务器之java mail 使用:exchange邮箱

();

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值