无法连接SMTP服务器

问题描述:

它显示以下错误::无法连接SMTP服务器

在线程异常 “主” 了java.lang.RuntimeException: javax.mail.MessagingException的:无法连接到SMTP主机: 本地主机,端口:587,响应:421在 SendMail.main(SendMail.java:54)导致: javax.mail.MessagingException:无法连接到SMTP主机: localhost,端口:587,响应:421在 com。 sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2088) at com.sun.mail.smtp.SMTPTransport.protocolConn ect(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)留在在 SendMail.main javax.mail.Transport.send(Transport.java:124) javax.mail.Transport.send0(Transport.java:254)(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 = "[email protected]"; 
     final String smtp_password = "xxxxxx"; 
     final String smtp_connection = "TLS"; 

     final String toEmail="[email protected]"; 
     final String fromEmail="[email protected]"; 

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

检查的SMTP 421响应意味着什么。通常如果是指服务器临时拒绝你的连接(通常是因为你尝试了太多的连接) – Brody

+0

要连接到谷歌smtp服务器,请确保你使用的是TLS/SSL。这是必需的 – mubeen

贝尔

ow是工作代码。您没有在非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 = "[email protected]"; 

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