【问题标题】:Not able to connect to smtp.gmail.com无法连接到 smtp.gmail.com
【发布时间】:2013-09-05 06:25:24
【问题描述】:

我使用以下 java 程序从 gmail 帐户发送邮件

final String username = "user@gmail.com";
final String password = "password";

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

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

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("user@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
                          InternetAddress.parse("user@live.in"));
    message.setSubject("Testing Subject");
    message.setText("Dear Bhavik Patel," +
                    "\n\n This is just a mail!");

    Transport.send(message);

    System.out.println("Done");

} catch (Exception e) {
    throw new RuntimeException(e);
}

我也试过 587 端口,但它不工作

Transport.send(message); 

在这个执行过程中尝试连接和发送

我不知道它有什么问题。我也尝试过 telnet,从那里我可以连接

例外:

java.lang.RuntimeException:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1

【问题讨论】:

  • 您如何确定“它不起作用”?错误信息,超时,异常,什么?
  • SSL 属性(用于端口 465)略有不同。查看this example了解更多详情(注意,我没有尝试过)
  • 超时 6 分钟后抛出
  • @MadProgrammer 让我试试你链接中的第二个例子
  • 双 ping,没有问题(SSL 和 TLS)。你用的是什么版本的javamail?我使用的是 1.4.7 和 1.5.0

标签: java jakarta-mail


【解决方案1】:

我正在使用端口587,我可以发送/接收邮件..或者你可以设置属性

ma​​il.smtp.timeout25000 并尝试是否是超时异常..

【讨论】:

  • 如果我使用 587 则显示“无法向 SMTP 发送命令”...防火墙或任何阻止系统会影响程序吗?
  • 主要是因为防火墙或防病毒程序可以阻止您发送电子邮件。如果您已安装或启用它们,请尝试禁用这些程序..并尝试发送它..
  • 还有其他方法吗?
  • Refer this link 用于发送简单邮件、带有附件的邮件和 html 电子邮件。
【解决方案2】:

试试这些 JavaMail 常见问题解答条目:

你需要设置“mail.smtp.ssl.enable”,而不是starttls。

【讨论】:

    【解决方案3】:

    我的send_mail 代码与您的代码之间存在细微差别,但我无法找出您的问题是由什么引起的。这是我的代码:

     public static int sendMail(String SMTPServer,
                        String Sender,
                        String Recipient,
                        String Subject,
                        String Body,
                        String ErrorMessage,
                        String Attachments) {
    // Error status;
    int ErrorStatus = 0;
    
    // Create some properties and get the default Session;
    final String username = Sender;
    final String password = "passwd";
    
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    
    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });
    
    try {
       // Create a message.
       MimeMessage msg = new MimeMessage(session);
    
       // extracts the senders and adds them to the message.
       // Sender is a comma-separated list of e-mail addresses as per RFC822.
       {
          InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
          msg.addFrom(TheAddresses);
       }
    
       // Extract the recipients and assign them to the message.
       // Recipient is a comma-separated list of e-mail addresses as per RFC822.
       {
          InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
          msg.addRecipients(Message.RecipientType.TO,TheAddresses);
       }
    
       // Subject field
       msg.setSubject(Subject);
    
       // Create the Multipart to be added the parts to
       Multipart mp = new MimeMultipart();
    
       // Create and fill the first message part
       {
          MimeBodyPart mbp = new MimeBodyPart();
          mbp.setText(Body);
    
          // Attach the part to the multipart;
          mp.addBodyPart(mbp);
       }
    
       // Add the Multipart to the message
       msg.setContent(mp);
    
       // Set the Date: header
       msg.setSentDate(new Date());
    
       // Send the message;
       Transport.send(msg);
    } catch (MessagingException MsgException) {
        System.out.println("blows here");
        ErrorMessage = MsgException.toString();
        Exception TheException = null;
        if ((TheException = MsgException.getNextException()) != null)
         ErrorMessage += "\n" + TheException.toString();
         ErrorStatus = 1;
    }
    System.out.println(ErrorMessage);
    return ErrorStatus;
    } 
    

    我一直在使用此代码,没有任何问题。

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2021-11-21
      • 1970-01-01
      • 2016-11-07
      • 2020-06-19
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多