简体   繁体   English

通过GMail使用SMTP时,如何修复javax.mail.MessagingException?

[英]How do I fix a javax.mail.MessagingException when using SMTP through GMail?

I cannot execute code that is publicly available for sending email through Gmail. 我无法执行公开用于通过Gmail发送电子邮件的代码。 I think it may be a network problem because I am at work although I am able to ping Gmail through the cmd prompt: ping smtp.gmail.com . 我认为这可能是网络问题,因为尽管我可以在cmd提示符下ping Gmail: ping smtp.gmail.com但我ping smtp.gmail.com

Here is the code I am using (from this page ): 这是我正在使用的代码(来自此页面 ):

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


class tester {
    public static void main(String args[]) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.stmp.user", "username of the sender");          
        //If you want you use TLS 
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "password of the sender");
        // If you want to use SSL
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String username = "username";
                    String password = "password";
                    return new PasswordAuthentication("username","password"); 
                }
            });
        String to = "me@gmail.com";
        String from = "from@gmail.com";
        String subject = "Testing...";
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(MimeMessage.RecipientType.TO, new    InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("JAVA is the BEST");
            Transport transport = session.getTransport("smtp");
            transport.send(msg);
            System.out.println("E-mail sent !");
        } catch(Exception exc) {
            System.out.println(exc);
        }
    }
}

I have changed the username and password fields to be correct. 我已更改用户名和密码字段为正确的。 What are some things that could cause this? 有哪些可能导致此问题的原因? I have used my own code and many other publicly available code for this, but I get the same error. 我为此使用了自己的代码和许多其他公共可用的代码,但是遇到了相同的错误。 I did try running through port 587 also and received the same error. 我也尝试通过端口587运行,并收到了相同的错误。

Here is the error: 这是错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is: java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at GoogleMail.sendMail(GoogleMail.java:45)

I used the telnet smtp.gmail.com 465 and telnet smtp.gmail.com 587 command in the cmd prompt and both could not connect. 我在cmd提示符下使用了telnet smtp.gmail.com 465telnet smtp.gmail.com 587命令,但都无法连接。 I assume it is associated with the network I am connected to. 我认为它与我连接的网络相关。 I cannot provide any info beyond this and will update if I find out more. 除此以外,我无法提供任何信息,如果我发现更多信息,将会更新。

暂无
暂无

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

相关问题 尝试连接到 gmail IMAP 服务器时出现 javax.mail.MessagingException - javax.mail.MessagingException when attempting to connect to gmail IMAP server javax.mail.MessagingException:未知的SMTP主机 - javax.mail.MessagingException: Unknown SMTP host 如何解决以下问题:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465;? - How to solve the issue with: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;? 当我尝试通过IMAP连接到Gmail时抛出javax.mail.MessagingException - javax.mail.MessagingException Is throwing when I try to connect via IMAP to Gmail javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465; - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 如何解决javax.mail.MessagingException:220 - How to solve javax.mail.MessagingException: 220 通过Java发送电子邮件-javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:587; - Sending emails through Java - javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587; javax.mail.MessagingException:无法将命令发送到SMTP主机 - javax.mail.MessagingException: Can't send command to SMTP host javax.mail.MessagingException:无法连接到SMTP主机? - javax.mail.MessagingException: Could not connect to SMTP host?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM