最近在用javamail通过exchange发邮件时遇到了问题.大家有没有遇到过呢? 帮忙看看.非常感谢
我代码如下:
public static void main(String[] args) {
try {
String STMP="mail.df.com.cn";
final String username = "[email protected]";//该用户是exchange上面的用户
final String password = "000000";
String port="465";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host",STMP);
//props.setProperty("mail.smtp.port", port);
//props.setProperty("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", true);
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}}); Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress());
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]",false));
msg.setSubject("Hello");
msg.setText("Hello");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent success");
} catch (Exception e) {
e.printStackTrace();
}
}
但是运行的时候总是报错.错误如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 5.7.1 Unable to relay for [email protected]
我代码如下:
public static void main(String[] args) {
try {
String STMP="mail.df.com.cn";
final String username = "[email protected]";//该用户是exchange上面的用户
final String password = "000000";
String port="465";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host",STMP);
//props.setProperty("mail.smtp.port", port);
//props.setProperty("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", true);
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}}); Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress());
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]",false));
msg.setSubject("Hello");
msg.setText("Hello");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent success");
} catch (Exception e) {
e.printStackTrace();
}
}
但是运行的时候总是报错.错误如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 5.7.1 Unable to relay for [email protected]
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]",false));
改成
msg.setRecipients(Message.RecipientType.TO,new InternetAddress("[email protected]"));试试。