簡體   English   中英

連接SMTP服務器失敗

[英]Failed to connect smtp server

它顯示以下錯誤::

線程“主”中的異常java.lang.RuntimeException:javax.mail.MessagingException:無法連接到SMTP主機:localhost,端口:587,響應:421在SendMail.main(SendMail.java:54)上的原因:javax。 mail.MessagingException:無法連接到SMTP主機:localhost,端口:587,響應:421,位於com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2088),位於com.sun.mail.smtp.SMTPTransport。在javax.mail.Service.connect(Service.java:246)在javax.mail.Service.connect(Service.java:246)處的protocolConnect(SMTPTransport.java:699)在javax.mail.Service.connect(Service.java)處:195),位於javax.mail.Transport.send0(Transport.java:254),位於javax.mail.Transport.send(Transport.java:124),位於SendMail.main(SendMail.java:49),Java結果:1構建成功(總時間:2秒)

我的代碼是:

`public class SendMail {

    public static void main(String[] args) throws Exception{

        final String smtp_host = "smtp.gmail.com";
        final String smtp_username = "xxx@gmail.com";
        final String smtp_password = "xxxxxx";
        final String smtp_connection = "TLS"; 

        final String toEmail="xxx@gmail.com";
        final String fromEmail="yyy@example.com";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");

       if (smtp_connection.equals("TLS")) {
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.port", "587");
        } else{
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.port", "465");
        }

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(smtp_username, smtp_password);
                }
          });

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromEmail, "NoReply"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(toEmail, "Mr. Recipient"));
            msg.setSubject("Welcome To JavaMail API");
            msg.setText("JavaMail API Test - Sending email example through remote smtp server");
            Transport.send(msg);
            System.out.println("Email sent successfully...");
        } catch (AddressException e) {
            throw new RuntimeException(e);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}`

以下是工作代碼。 在非TLS情況下,您沒有設置SSL套接字工廠。

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Email {

private static String USER_NAME = "username";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "password"; // GMail password

private static String RECIPIENT = "xxxxx@gmail.com";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "hi ....,!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
  String host = "smtp.gmail.com";

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

    props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");


    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {


        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }



        message.setSubject(subject);
        message.setText(body);


        Transport transport = session.getTransport("smtp");


        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
    }
   } 

要連接到Google SMTP服務器,請確保您使用的是TLS / SSL。 它是必需的。 轉到gmail帳戶單擊“轉發/ IMAP”選項卡,然后向下滾動到“ IMAP訪問”部分:必須啟用IMAP才能將電子郵件正確復制到您的已發送文件夾中。

並嘗試更改此

if (smtp_connection.equals("TLS")) {
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.port", "587");
        } else{
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.port", "465");
        }

對此

properties.put("mail.smtp.starttls.enable", "true"); 
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.user", "username"); // User name
properties.put("mail.smtp.password", "password"); // password
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");

暫無
暫無

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

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