【问题标题】:Sending email using Java, connecting to a gmail host hangs使用 Java 发送电子邮件,连接到 gmail 主机挂起
【发布时间】:2014-06-10 05:22:35
【问题描述】:

我想通过 Java 代码发送电子邮件。我在我的库中添加了以下 .JAR:log4j.jar、smtp.jar、mailapi.jar、ctivation.jar。我的 Java 类看起来像这样:

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

public class SendEmail
{
public static void main(String [] args)
{    
   String to = "abcd@gmail.com";
   String from = "web@gmail.com";
   String host = "smtp.gmail.com";
   Properties properties = System.getProperties();


   properties.setProperty("mail.smtp.host", host);
   properties.setProperty("mail.smtp.starttls.enable", "true");
   properties.setProperty("mail.smtp.auth", "true");

   SmtpAuthenticator authentication = new SmtpAuthenticator();
   javax.mail.Message msg = new MimeMessage(Session
                       .getInstance(properties, authentication));

   try {
       msg.setFrom(new InternetAddress(from));
       msg.setRecipient(Message.RecipientType.TO, 
           new InternetAddress(to));
       msg.setSubject("Subject");
       msg.setText("Working fine..!");
       System.out.println("fine1    !!");
       Transport transport = Session.getDefaultInstance( properties , null).getTransport("smtp");
       System.out.println("fine2    !!");
       transport.connect("smtp.gmail.com" , 465 , "username", "password");
       System.out.println("fine3    !!");
       Transport.send(msg);
       System.out.println("fine!!");
   }
   catch(Exception exc) {
       System.out.println(exc);
   }
}
}

我的 SmtpAuthenticator 类:

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SmtpAuthenticator extends Authenticator {
    public SmtpAuthenticator() {

        super();
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "user";
        String password = "password";
        if ((username != null) && (username.length() > 0) && (password != null)
                && (password.length() > 0)) {

            return new PasswordAuthentication(username, password);
        }

        return null;
    }
}

当我运行我的 Java 应用程序类时,它会打印: 很好1! 很好2!!

然后挂起。我怎样才能摆脱这个问题?

【问题讨论】:

  • 检查您的电脑/服务器和外部 smtp 服务器之间的通信没有防火墙。
  • 防火墙设置为在 Windows 防火墙阻止家庭和公共网络上的新程序时通知我。

标签: java email smtp jakarta-mail


【解决方案1】:

问题出在这一行:

transport.connect("smtp.gmail.com" , 465 , "username", "password");

465 是 smtp over ssl (smtps) 的端口,所以要么使用端口 25:

transport.connect("smtp.gmail.com" , 25 , "username", "password");

或改为使用 smtps

Transport transport = Session.getDefaultInstance( properties , null).getTransport("smtps");

transport.connect("smtp.gmail.com" , 465, "username", "password");

【讨论】:

  • 又出问题了,我尝试了你的两个建议。结果:很好1!很好2! javax.mail.AuthenticationFailedException: 454 4.7.0 登录尝试次数过多,请稍后再试。 z5sm47598012eee.31 - gsmtp
  • 您的 Gmail 帐户是否启用了两步验证?如果你这样做,它将不起作用
  • 我只有我发布的东西。
  • 两步验证是通过 gmail 设置的 - 不是在代码中。 support.google.com/accounts/answer/180744?hl=en 如果您在您的帐户上启用了此功能,您的代码将无法使用。您收到身份验证异常的事实意味着您尝试了太多次,可能密码错误,所以您被锁定了
  • 不,我没有,但这不起作用。对我来说 SMTP 主机无关紧要(成为 gmail 并不重要)我只想让发送邮件正常工作......
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 2014-08-09
  • 1970-01-01
  • 2017-01-31
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多