简体   繁体   English

无法使用Javax.mail发送邮件

[英]Failed to send Mail with Javax.mail

I'm trying to send an email on Windows 8.1 with java 1.8 using javax.mail API but I keep getting an error. 我正在尝试使用javax.mail API在带有Java 1.8的Windows 8.1上发送电子邮件,但我一直收到错误消息。

Here is the method I wrote: 这是我写的方法:

public void sendAlertMail(String from, String to, String header, String msgBody){
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", smptHost);
        props.put("mail.smtp.port", smptPort);
        props.put("mail.smtp.starttls","true");
        Session mailSession;
        if (authenticate) {
            props.put("mail.smtp.auth", "true");

            Authenticator auth = new SMTPAuthenticator();
            mailSession = Session.getInstance(props, auth);
        }
        else{
            mailSession = Session.getInstance(props);
        }
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);
        Transport transport = null;
        try {
            transport = mailSession.getTransport();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }

        MimeMessage message = new MimeMessage(mailSession);
        try {
            message.addHeaderLine(header);
            message.setContent(msgBody, "text/plain");
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
        } catch (MessagingException e) {
        }
        try {
            transport.connect();
            transport.sendMessage(message,
                    message.getRecipients(Message.RecipientType.TO));
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

And the error I receive is: 我收到的错误是:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:64)
    at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:74)
    at tapperSolution.mail.MailSender.main(MailSender.java:88)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.SocketException: Permission denied: connect\at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
... 12 more

I turned off my firewall and anti virus and I tried to set the VM option in my code using: 我关闭了防火墙和防病毒软件,并尝试使用以下命令在代码中设置VM选项:

System.setProperty("java.net.preferIPv4Stack", "true");

Also tried to set it in the run command with: 还尝试使用以下命令在run命令中进行设置:

-Djava.net.preferIPv4stack=true -Djava.net.preferIPv4stack = true

I was able to send mail using Python, but I want to do it using Java. 我可以使用Python发送邮件,但我想使用Java发送邮件。

Tried to capture traffic using Wireshark, but there is no even a TCP handshake with the server. 试图使用Wireshark捕获流量,但是服务器之间甚至没有TCP握手。

Any other ideas? 还有其他想法吗?

Thanks 谢谢

It looks like your SMTP server may be rejecting your connection, perhaps due to authentication issues. 看来您的SMTP服务器可能由于身份验证问题而拒绝了您的连接。 To test this, try a manual SMTP session from the same machine, and see what the result is. 要对此进行测试,请尝试从同一台计算机进行手动SMTP会话 ,然后查看结果。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM