简体   繁体   English

连接SMTP服务器失败

[英]Failed to connect smtp server

It is showing following error :: 它显示以下错误::

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587, response: 421 at SendMail.main(SendMail.java:54) Caused by: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587, response: 421 at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2088) 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 javax.mail.Transport.send0(Transport.java:254) at javax.mail.Transport.send(Transport.java:124) at SendMail.main(SendMail.java:49) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds) 线程“主”中的异常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秒)

and my code is: 我的代码是:

`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);
        }
    }
}`

Below is the working code. 以下是工作代码。 You are not setting an SSL socket factory in the non-TLS case. 在非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();
    }
    }
   } 

To connect to google SMTP server, make sure you are using TLS/SSL. 要连接到Google SMTP服务器,请确保您使用的是TLS / SSL。 It is required. 它是必需的。 Go to gmail account Click on the Forwarding/IMAP tab and scroll down to the IMAP Access section: IMAP must be enabled in order for emails to be properly copied to your sent folder. 转到gmail帐户单击“转发/ IMAP”选项卡,然后向下滚动到“ IMAP访问”部分:必须启用IMAP才能将电子邮件正确复制到您的已发送文件夹中。

And Try Change This 并尝试更改此

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");
        }

To This 对此

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