无法连接到Office365 for JavaEmailer

时间:2018-10-05 22:48:53

标签: office365 javamail

我正在使用VPN,但我不认为这是问题所在。我将防火墙设置为允许传出连接,而我的McAfee则忽略了Eclipse和Java。

我知道这看起来像重复的。我保证不是。我已经尝试了过去三天在Stack Overflow上发现的所有内容,但这仍然给了我

javax.mail.MessagingException: Could not connect to SMTP host: 
smtp.live.com, port: 587;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:275)
    at edu.montana.fs.MailAttachment.<init>(MailAttachment.java:67)
    at edu.montana.fs.MainCall.main(MainCall.java:90)
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
    ... 4 more

这是我的邮递区号(已删除电子邮件和密码):

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
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 MailAttachment {
    public MailAttachment(String filepath, int errorCount, int recordCount)    {

        final String SMTP_HOST = "smtp.live.com";
        final String SMTP_PORT = "587";
        final String SMTP_USER = "user@outlook.com";
        final String SMTP_PWD = "password";

        final String from = "server@institute.edu";
        final String to = "user@institute.edu";

        final String subject = "Test";
        final String messageContent = "Testing Message";

        final Properties config = new Properties();
        config.put("mail.smtp.user", SMTP_USER);
        config.put("mail.smtp.pwd", SMTP_PWD);
        config.put("mail.smtp.ssl.trust", SMTP_HOST);
        config.put("mail.smtp.port", SMTP_PORT);
        config.put("mail.smtp.starttls.enable", "true");
        config.put("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SMTP_USER, SMTP_PWD);
            }
        };

        Session session = Session.getInstance(config, auth);

        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 transport = session.getTransport("smtp");
            transport.connect(SMTP_HOST, 587, SMTP_USER, SMTP_PWD);
            transport.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail successfully sent: " + to);
            transport.close();
            //Transport.send(message);
        } catch (final MessagingException ex) {
            ex.printStackTrace();
        }
    }

}

是否有明显的事情我做错了?我已经至少完成了该课程的5次完整改版,并欢迎提出建议。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

其他人都知道,问题出在网络防火墙/安全性上。显然,即使我将网络管理员级别的访问权限绑定到我的Active Directory帐户上(通过VPN访问),通过VPN连接显然也没有给Java程序足够的特权来运行电子邮件程序。我能够在具有营业场所的物理位置并且与网络进行有线连接的服务器上对此进行测试。