MS Exchange java 发邮件配置
求助大神!~
我现在用java 写了个发邮件功能,大致思路是运行main方法,读取配置文件里的配置信息,发邮件给CCC@126.com
代码如下:
public static void main(String[] args)
throws RowsExceededException, WriteException, IOException
{
MailUtil mu = new MailUtil();
ResourceBundle bundle = ResourceBundle.getBundle("config");
String receiveEmailAddress = "CCC@126.com";
String title = "ceshi";
String receiveContent = "邮件测试";
mu.sendEmail(receiveEmailAddress, title, receiveContent);
}
public int sendEmail(String sendEmailHost, String sendEmailUser, String sendEmailPassword,
String receiveEmailAddress, String title, String receiveContent) {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", sendEmailHost);
props.put("mail.user", sendEmailUser);
props.put("mail.password", sendEmailPassword);
Authenticator authenticator = new Authenticator() {
@Override
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);
;
try {
// 设置发件人地址
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人地址
InternetAddress to = new InternetAddress(receiveEmailAddress);
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject(title);
// 设置邮件内容
message.setContent(receiveContent,"text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
return -1;
} catch (MessagingException e) {
e.printStackTrace();
return 0;
}
return 1;
}
如果用下面的配置
SEND_EMAIL_HOST=mail.yst.com.cn
SEND_EMAIL_USER=AAA@mail.yst.com.cn
SEND_EMAIL_PASSWORD=BBB
如果用这个配置,发送失败,抛出异常 :javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.dhc.purchase.common.MailUtil.sendEmail(MailUtil.java:76)
at com.dhc.purchase.common.MailUtil.sendEmail(MailUtil.java:30)
at com.dhc.demo.ui3.notif.web.Ui3InterNotifAction.main(Ui3InterNotifAction.java:206)
如果配置文件改成下面这样,可以发送成功
#SEND_EMAIL_HOST=smtp.126.com
#SEND_EMAIL_USER=DDD@126.com
#SEND_EMAIL_PASSWORD=XXX
以前没写过发邮件功能,也不太懂.
客户说他们邮箱使用的是MS exchange 服务器,还需要如何修改,求教大神!!
解决问题100分全给