【问题标题】:How to send email using my outlook account?如何使用我的 Outlook 帐户发送电子邮件?
【发布时间】: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 邮件。

【问题讨论】:

标签: java email outlook


【解决方案1】:

尝试编写自己的 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);
    }
}

然后用它来创建会话:

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");

【讨论】:

  • 能否请您在编辑后回复整个代码。我无法理解。
  • @AK2 只需为 Authenticator 创建一个新类,然后用这段代码替换您的代码(带有属性和会话的部分)
  • 当我写你的 SMTPAuthenticator 时它会在这一行出现错误return new PasswordAuthentication(userName, password); 它说 PasswordAuthentication 不能应用于给定的类型。
  • @AK2 确保你的类扩展了 Authenticator 类; SMTPAuthenticator extends Authenticator 并且您已经包含了正确的导入。检查编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2022-08-10
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2012-05-25
  • 2012-03-30
相关资源
最近更新 更多