2

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

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 服务器?

4

3 回答 3

1

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

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

ping smtp.gmail.com

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

于 2011-12-23T05:54:09.290 回答
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();
    }
}
于 2011-12-23T06:52:01.413 回答
0

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

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

于 2011-12-23T05:15:36.373 回答