【问题标题】:Error in sending mail - ConnectException发送邮件时出错 - ConnectException
【发布时间】:2012-12-14 21:37:06
【问题描述】:

我使用以下 Java 代码发送电子邮件。

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

public class SendEmail
{
   public static void main(String [] args)
   {    

      String to = "abcd@gmail.com";
      String from = "web@gmail.com";
      String host = "localhost";
      Properties properties = System.getProperties();
      properties.setProperty("smtp.gmail.com", host);
      Session session = Session.getDefaultInstance(properties);
      try{
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to))
         message.setSubject("This is the Subject Line!");
         message.setText("This is actual message");
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

当我运行该文件时,我收到以下错误:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)

Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)

如果有人可以帮助我,我将不胜感激。

ConnectException如何解决?

【问题讨论】:

  • 您可以尝试在配置的 SMTP 端口(默认值:25)上远程登录到您的本地主机。 telnet localhost 25 并检查?
  • 那么,您的机器上运行着 SMTP 服务器吗?
  • 我试过 telnet localhost 25 bt 它错误显示 Connecting To localhost...Could not open connection to the host, on port 25: Connect failed 我在 Windows 功能中启用了 telnet 客户端和 telnet 服务器功能,我使用的是 Windows 7
  • 这里可能有更多关于调试 telnet 超时的信息:stackoverflow.com/questions/5179807/…

标签: java email jakarta-mail connectexception


【解决方案1】:

我可以看到您正在尝试使用Gmail 作为您的 SMTP 服务器。此行不正确:

properties.setProperty("smtp.gmail.com", host);

您使用主机名作为属性名,这是不正确的。因为您没有设置mail.smtp.host 属性JavaMail 尝试连接到'localhost'。请改为设置以下属性:

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

【讨论】:

  • 什么是用户名?是gmail id吗?还是别的什么??
  • 非常感谢,这对我很有帮助,我会把完整的代码添加下来
  • 这是用户 ID 假设我的邮件是 liferayasif@gmail.com 然后用户 ID:liferayasif
【解决方案2】:

将主机名更改为:smtp.gmail.com

String host = "localhost";

将其更改为:

String host = "smtp.gmail.com"

【讨论】:

    【解决方案3】:

    你必须尝试这样的事情

    String host = "smtp.gmail.com";
    String from = "user name";
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", "asdfgh");
    props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
    props.put("mail.smtp.auth", "true");
    
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    
    InternetAddress[] to_address = new InternetAddress[to.length];
    int i = 0;
    // To get the array of addresses
    while (to[i] != null) {
        to_address[i] = new InternetAddress(to[i]);
        i++;
    }
    System.out.println(Message.RecipientType.TO);
    i = 0;
    while (to_address[i] != null) {
    
        message.addRecipient(Message.RecipientType.TO, to_address[i]);
        i++;
    }
    message.setSubject("sending in a group");
    message.setText("Welcome to JavaMail");
    // alternately, to send HTML mail:
    // message.setContent("<p>Welcome to JavaMail</p>", "text/html");
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    

    现在您正在尝试连接到 localhost

    礼貌this answer

    【讨论】:

      【解决方案4】:

      尝试将其添加到您的属性中:

      private static final String SMTP_HOST = "smtp.gmail.com"; //Use Gmail's SMTP Host.
      private static final String SMTP_PORT = ""; //Use Gmail's Port Number for SMTP.
      properties.setProperty("mail.smtp.host", SMTP_HOST);
      properties.setProperty("mail.smtp.port", SMTP_PORT);
      

      【讨论】:

        【解决方案5】:

        假设我的

        电子邮件:liferayasif@gmail.com

        密码:密码

        public Session getSession() {
                
                Properties props = new Properties();
            
                props.put("mail.smtp.starttls.enable", "true"); 
                
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.user", "liferayasif"); // User name
                props.put("mail.smtp.password", "password"); // password
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.auth", "true");
                
                props.put("mail.session.mail.pop3.host", "pop.gmail.com");
        
                props.put("mail.session.mail.pop3.password", "password");
        
                props.put("mail.session.mail.pop3.port", "110");
        
                props.put(" mail.session.mail.pop3.user", "USER");
        
                props.put("mail.session.mail.imap.host", "imap.gmail.com");
        
                props.put("mail.session.mail.imap.port", "993");
        
                props.put("mail.session.mail.store.protocol", "imap");
        
                props.put("mail.session.mail.transport.protocol", "smtp");
        
                props.put("mail.session.mail.smtp.host", "smtp.gmail.com");
        
                props.put("mail.session.mail.smtp.password", "password");
        
                props.put("mail.session.mail.smtp.user", "liferayasif@gmail.com");
        
                props.put("mail.session.mail.smtp.port", "465");
        
                props.put("mail.session.mail.smtp.auth", "true");
        
                props.put("mail.session.mail.smtp.starttls.enable", "true");
        
                props.put("mail.session.mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                      
                Session session = Session.getInstance(props,
                 new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication("liferayasif", "password");
                    }
                 });
              
              return session;
            }
            
        

        【讨论】:

          猜你喜欢
          • 2014-07-11
          • 2015-03-05
          • 2016-08-25
          • 1970-01-01
          • 1970-01-01
          • 2013-07-14
          • 2014-09-26
          相关资源
          最近更新 更多