【发布时间】:2015-05-05 23:19:17
【问题描述】:
我正在尝试这段代码:
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class NewClass {
public static void main(String[]args)throws IOException {
final String username = "prashantsood_90";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("prashantsood_90@outlook.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("prashantsood_90@outlook.com"));
message.setSubject("Test");
message.setText("HI");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
它给出了这个例外:
Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
at NewClass.main(NewClass.java:47)
Caused by: javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at NewClass.main(NewClass.java:42)
Java Result: 1
请帮助我如何将电子邮件从我的 Outlook 帐户发送到 gmail 或任何其他帐户。我无法使用 Outlook 帐户发送,但我可以将 gmail 发送到 gmail 邮件。
【问题讨论】:
-
您可以自动化 Outlook,请参阅如何在 Outlook 中创建电子邮件并使其对用户可见以获取更多信息,请访问stackoverflow.com/questions/18057759/…。