gpt4 book ai didi

java - 无法连接到 SMTP 主机 : outlook. office365.com,端口 : 587; nested exception is: java.net.SocketException:权限被拒绝:连接

转载 作者:行者123 更新时间:2023-12-02 05:02:53 25 4
gpt4 key购买 nike

我编写了使用 Outlook.office365.com 发送电子邮件的代码。当我运行程序时出现以下错误。

javax.mail.MessagingException: Could not connect to SMTP host: outlook.office365.com, port: 587; nested exception is:

java.net.SocketException: Permission denied: connect

private static final String SERVIDOR_SMTP = "outlook.office365.com";
private static final int PORTA_SERVIDOR_SMTP = 587;
private static final String CONTA_PADRAO = "xxxx@xxx.com"; //Cofig Mail Id
private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password

private final String from = "xxxx@xxx.com";
private final String to = "xxxx@xxx.com";

private final String subject = "Teste";
private final String messageContent = "Teste de Mensagem";

public void sendEmail() {
final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
}

});

try {
final Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(messageContent);
message.setSentDate(new Date());
Transport.send(message);
} catch (final MessagingException ex) {
System.out.println(" "+ex);
}
}

public Properties getEmailProperties() {
final Properties config = new Properties();
config.put("mail.smtp.auth", "true");
config.put("mail.smtp.starttls.enable", "true");
config.put("mail.smtp.host", SERVIDOR_SMTP);
config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
return config;
}

public static void main(final String[] args) {
new SendAttachmentInEmail().sendEmail();
}

最佳答案

我认为您没有访问电子邮件的权限。例如,当我尝试使用我的 Gmail 帐户时,如果我不打开下面显示的设置,我会收到授权失败,但如果我打开该设置,我可以使用您的代码发送电子邮件。

请检查您的电子邮件权限和设置。

settings picture

关于java - 无法连接到 SMTP 主机 : outlook. office365.com,端口 : 587; nested exception is: java.net.SocketException:权限被拒绝:连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com