簡體   English   中英

無法連接到SMTP主機:smtp.gmail.com,port:465,響應:-1

[英]Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

發送郵件時,我收到此錯誤

java.lang.RuntimeException:javax.mail.SendFailedException:發送失敗; 嵌套異常是:class javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,port:465,響應:-1

我的代碼是:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("email","password");
                }
        });

try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("email"));
        message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(this.to));
        message.setSubject("Testing");
        message.setText("Hey, this is the testing email.");



        Transport.send(message);

任何幫助,將不勝感激。

提前致謝。

您需要告訴它您正在使用SSL:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

萬一你錯過了什么,這里是工作代碼:

String  d_email = "address@gmail.com",
            d_uname = "Name",
            d_password = "urpassword",
            d_host = "smtp.gmail.com",
            d_port  = "465",
            m_to = "toAddress@gmail.com",
            m_subject = "Indoors Readable File: " + params[0].getName(),
            m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    SMTPAuthenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);
    try {
        msg.setSubject(m_subject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

Transport transport = session.getTransport("smtps");
            transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        } catch (AddressException e) {
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }

我在使用NetBeans進行調試時遇到過這個問題,甚至執行實際的jar文件。 防病毒軟件會阻止發送電子郵件。 您應該在調試期間暫時禁用防病毒軟件,或者排除NetBeans並掃描實際的jar文件。 就我而言,我正在使用Avast。

請參閱此鏈接,了解如何排除: 如何將文件/網站例外添加到avast中! 防病毒2014

這個對我有用。

我做的是我評論了

props.put("mail.smtp.starttls.enable","true"); 

因為顯然對於G-mail你不需要它。 然后,如果您還沒有這樣做,則需要在G-mail中為您的程序創建一個應用程序密碼。 我做到了,效果很好。 此鏈接將向您顯示以下內容: https//support.google.com/accounts/answer/185833

就我而言,Avast Antivirus干擾了連接。 禁用此功能的操作:Avast - >設置 - >組件 - > Mail Shield(自定義) - > SSL掃描 - >取消選中“掃描SSL連接”。

端口465用於“smtp over SSL”。

http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

[...] For example, use
    props.put("mail.smtp.port", "888");
to set the mail.smtp.port property, which is of type int.

Note that if you're using the "smtps" protocol to access SMTP over SSL, 
all the properties would be named "mail.smtps.*"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM