java - 如何使用我的 outlook 帐户发送电子邮件?

标签 java email outlook

我正在尝试这段代码:

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):

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 邮件。

最佳答案

尝试编写自己的 Authenticator

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
    private String userName;
    private String password;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public SMTPAuthenticator(String userName,String password){
        this.userName=userName;
        this.password=password;
    }

    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(userName, password);
    }
}

然后用它来创建 session :

SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", "outlook.office365.com");
properties.setProperty("mail.smtp.port", "587");

Session session = Session.getInstance(properties, authenticator);

如果您没有 TSL 保护连接,也请删除此行

props.put("mail.smtp.starttls.enable", "true");

关于java - 如何使用我的 outlook 帐户发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30046131/

相关文章:

java - 我已经从 h2o 下载了一个 pojo,编译了它,但是我该如何使用它呢?

java - 如何为一个按钮添加多个操作

PHPList 对于带有前导、尾随或多个相邻点的电子邮件地址失败

email - Lotus Notes 电子邮件作为另一封电子邮件的附件

ruby - 在 Ruby sinatra 中接收电子邮件

c# - VSTO - C# 中的 Outlook 事件处理程序

html - 无法在 Outlook 2013 HTML 电子邮件中正确隐藏图像

java - PST 时间换算

vba - 在 Outlook 中使用 VBA 收集收到的电子邮件的统计信息

java - 将元素添加到数组时,对象数组未初始化新对象