Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java发送电子邮件,连接到gmail主机挂起_Java_Email_Smtp_Jakarta Mail - Fatal编程技术网

使用Java发送电子邮件,连接到gmail主机挂起

使用Java发送电子邮件,连接到gmail主机挂起,java,email,smtp,jakarta-mail,Java,Email,Smtp,Jakarta Mail,我想通过Java代码发送电子邮件。我在我的库中添加了以下.JARs:log4j.jar、smtp.jar、mailapi.jar、activation.jar。我的Java类如下所示: import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String [] args) { St

我想通过Java代码发送电子邮件。我在我的库中添加了以下.JARs:log4j.jar、smtp.jar、mailapi.jar、activation.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应用程序类时,它会打印: 好极了!! 好极了


它挂起来了。如何解决此问题?

问题出在这一行:

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

问题出在这一行:

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

检查您的电脑/服务器与外部smtp服务器之间是否没有防火墙阻止通信。防火墙设置为在Windows防火墙阻止家庭和公共网络上的新程序时通知我。检查您的电脑/服务器与外部smtp服务器之间是否没有防火墙阻止通信。防火墙设置为在Windows防火墙阻止家庭和公共网络上的新程序时通知我。它再次出现问题,我尝试了你的两个建议。结果:fine1!!好极了!!javax.mail.AuthenticationFailedException:454.7.0登录尝试太多,请稍后再试。z5sm47598012eee.31-gsmtpDo您的Gmail帐户启用了两步验证吗?如果你只有我发布的东西,那就不行了。两步验证是通过gmail设置的,而不是代码。如果您的帐户已启用此功能,则代码将无法工作。您获得身份验证异常的事实意味着您使用可能错误的密码尝试了太多次,因此被锁定不,我没有,但这不起作用。对我来说,SMTP主机并不重要(成为gmail并不重要),我只是想让发送邮件工作起来……它又出现了问题,我尝试了你的两个建议。结果:fine1!!好极了!!javax.mail.AuthenticationFailedException:454.7.0登录尝试太多,请稍后再试。z5sm47598012eee.31-gsmtpDo您的Gmail帐户启用了两步验证吗?如果你只有我发布的东西,那就不行了。两步验证是通过gmail设置的,而不是代码。如果您的帐户已启用此功能,则代码将无法工作。您获得身份验证异常的事实意味着您使用可能错误的密码尝试了太多次,因此被锁定不,我没有,但这不起作用。对我来说,SMTP主机并不重要(成为gmail并不重要),我只想让发送邮件正常工作。。。