简体   繁体   English

无法连接到SMTP主机:Outlook.office365.com,端口:587; 嵌套的异常是:java.net.SocketException:权限被拒绝:connect

[英]Could not connect to SMTP host: outlook.office365.com, port: 587; nested exception is: java.net.SocketException: Permission denied: connect

I have written code that sends email using outlook.office365.com. 我已经编写了使用Outlook.office365.com发送电子邮件的代码。 When I am running the program getting the following error. 当我运行程序时,出现以下错误。

javax.mail.MessagingException: Could not connect to SMTP host: outlook.office365.com, port: 587; javax.mail.MessagingException:无法连接到SMTP主机:Outlook.office365.com,端口:587; nested exception is: 嵌套的异常是:

java.net.SocketException: Permission denied: connect java.net.SocketException:权限被拒绝: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();
}

I think, you don't have permission for access your email. 我认为,您没有访问电子邮件的权限。 For example while i try in my gmail account, if i don't open the setting that show at below, i get a authorization failed but if i open the setting, i can send e-mail with your code. 例如,当我尝试使用gmail帐户时,如果我没有打开下面显示的设置,则授权失败,但是如果我打开设置,则可以使用您的代码发送电子邮件。

Please check your e-mail permission and settings. 请检查您的电子邮件权限和设置。

settings picture 设置图片

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接到SMTP主机:smtp.gmail.com,port:587; 嵌套异常是:java.net.ConnectException:连接超时:连接 - Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect MailConnectException:嵌套异常:java.net.SocketException:权限被拒绝:connect - MailConnectException : nested exception: java.net.SocketException: Permission denied: connect Java.net.SocketException:权限被拒绝:connect - Java.net.SocketException: Permission denied: connect javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:主机没有路由 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; java.net.NoRouteToHostException: No route to host 未知的SMTP主机:outlook.office365.com; - Unknown SMTP host: outlook.office365.com; 在JDeveloper中获取“ java.net.SocketException:权限被拒绝:connect” - Getting “java.net.SocketException: Permission denied: connect” in JDeveloper JavaMail API到iMail - java.net.SocketException:权限被拒绝:连接 - JavaMail API to iMail — java.net.SocketException: Permission denied: connect (java.net.SocketException)java.net.SocketException:权限被拒绝:连接 - (java.net.SocketException) java.net.SocketException: Permission denied: connect Java.net.SocketException:Permission denied:在Pentaho Data Integration中连接 - Java.net.SocketException: Permission denied: connect in Pentaho Data Integration java.net.SocketException:权限被拒绝:连接启动Tomcat 7 - java.net.SocketException: Permission denied: connect Starting Tomcat 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM