【问题标题】:Unable to send e-mail through Java无法通过 Java 发送电子邮件
【发布时间】:2012-01-26 14:23:57
【问题描述】:

在查看了针对相同问题提供的帖子后,我编写了以下代码。但我得到以下异常:

javax.mail.MessagingException: 无法连接到 SMTP 主机:smtp.gmail.com,端口:587; 嵌套异常是:java.net.ConnectException:连接超时:连接

public static void main(String[] args) {

    String to = "xxx@gmail.com" // valid gmail address.     
    String from = "yyy@gmail.com"; // valid gmail address

    String host = "smtp.gmail.com";
    String password = "****"; // password of the gmaill acc used in from

    int port = 587;


    Properties properties = System.getProperties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.smtp.host",host );
    properties.setProperty("mail.smtp.user", from);
    properties.setProperty("mail.smtp.password", password);
    properties.setProperty("mail.smtp.port", "587");
    properties.setProperty("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(properties,null);

    try {

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        message.setSubject("Test Mail");

        message.setText("This is just a test mail generated");

       Transport transport = session.getTransport("smtp");
       transport.connect(host,from,password);
       InternetAddress[] addresses = new InternetAddress[1];
       addresses[0] = new InternetAddress(to);
       transport.sendMessage(message,addresses);


        System.out.println("Message Sent Successfully");
    }catch(MessagingException excp){
        System.out.println(excp);
    }

}

有人能说出我正在做的错误吗?我的 gmail 帐户中是否有任何设置需要设置才能使用 gmail smtp 服务器?

【问题讨论】:

  • 你在代理/防火墙后面吗?
  • 不,我尝试 ping “smtp.gmail.com”,我确实得到了回复,没有丢包。所以我认为这不是防火墙问题。

标签: java email smtp gmail gmail-pop


【解决方案1】:

试试下面的代码。您将需要下载javax.mail 包(一个 jar 文件),我假设您已经拥有该 jar 文件,因为您已经尝试过此代码,因此,我没有提供下载该 jar 文件的链接。正确设置类路径并导入必要的包。注意防火墙和您选择的主机端口。

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

final class MailClient
{
    private class SMTPAuthenticator extends Authenticator
    {
        private PasswordAuthentication authentication;

        public SMTPAuthenticator(String login, String password)
        {
             authentication = new PasswordAuthentication(login, password);
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
             return authentication;
        }
    }

    public void mail()
    {
        try
        {
            String from = "xyz.com";
            String to = "abc.com";
            String subject = "Your Subject.";
            String message = "Message Text.";
            String login = "xyz.com";
            String password = "password";

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

            Authenticator auth = new SMTPAuthenticator(login, password);

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);

           try
           {
                msg.setText(message);
                msg.setSubject(subject);
                msg.setFrom(new InternetAddress(from));
                msg.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
                Transport.send(msg);
           }
           catch (MessagingException ex)
           {
                Logger.getLogger(MailClient.class.getName()).
                log(Level.SEVERE, null, ex);
           }
        }
    }
}

final public class Main
{
    public static void main(String...args)
    {
        new MailClient().mail();
    }
}

【讨论】:

  • @user1043422 在这种情况下,这篇文章必须被点赞。请不要忘记投票,如果它是您的答案,因为您对这个网站有些陌生。
  • @bavesh 不,他需要将其标记为正确,他已经这样做了。 Upvoting 是针对第三方的。
【解决方案2】:

存在连接问题。首先检查与“smtp.gmail.com”的连接。

转到命令提示符并运行 ping 命令如下。

ping smtp.gmail.com

如果您没有收到服务器的回复,则可能是防火墙问题。

【讨论】:

  • 我试过了,我确实得到了回复,没有丢包……所以我在这里看不到防火墙问题。
  • @user1043422 ping 成功并不意味着与邮件服务器的连接成功。可能有防火墙阻止了该端口。
  • @EJP : 如何检查防火墙是否阻止了该端口?
【解决方案3】:

连接超时表示它甚至没有连接,这意味着它甚至无法访问您的 gmail 帐户,因此在那里设置一些东西也无济于事。

您可能想尝试一个简单的 telnet 到该主机/端口,看看您是否可以连接。如果不能,则可能是连接位置错误,或者可能是防火墙问题。

【讨论】:

  • 我尝试 ping smtp.gmail.com 并收到回复,所以我不认为这是防火墙问题。
  • @Bill “...它甚至无法访问您的 gmail 帐户”。不,这意味着它甚至无法访问他的 gmail 服务器。
  • 进而无法访问您的帐户。
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 2013-02-21
  • 2014-10-23
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多