java java邮件smtp连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6941294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:58:19  来源:igfitidea点击:

java mail smtp connection problem

javaemailsmtpconnection

提问by Gopal

i am trying the below example to send mail using ssl picked from hereand changed with my credentials as below and get exception as shown, can you please help me to get through it

我正在尝试使用以下示例使用从此处选择的 ssl 发送邮件,并使用我的凭据进行更改,如下所示,并获得如图所示的异常,请您帮我完成它

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;


public class SimpleSSLMail {

    private static final String SMTP_HOST_NAME = "xx.xx.xx.xx";
    private static final int SMTP_HOST_PORT = 25 ;//465;
    private static final String SMTP_AUTH_USER = "[email protected]";
    private static final String SMTP_AUTH_PWD  = "xxxx";

    public static void main(String[] args) throws Exception{
       new SimpleSSLMail().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("Testing SMTP-SSL");
        message.setContent("This is a test", "text/plain");

        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("[email protected]"));

        transport.connect
          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
}

Exception:

例外:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host:xx.xx.xx.xx, port: 25;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:275)

采纳答案by Suhail Gupta

This type of problem occurs due to a network problem . May be you require authentication, you may have wrong username , password .

这种类型的问题是由网络问题引起的。可能是您需要身份验证,您可能有错误的用户名、密码。

Anyways you are missing in a lot of things in your code. Example to obtain a proper SSL connection with gmail server

无论如何,您在代码中缺少很多东西。 获取与 gmail 服务器的正确 SSL 连接的示例

props.put("mail.smtp.socketFactory.port", "465");
 props.put("mail.smtp.socketFactory.class",
                           "javax.net.ssl.SSLSocketFactory");
 props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.port", "465");

I don't see this any where in your code. Then where is the method to authenticate ?

我在你的代码中没有看到这个。那么认证的方法在哪里呢?

Just check out the following links :

只需查看以下链接:

Sending E-mail Using Gmail In JavaHere you have a choice of using TLS or SSl

在 Java 中使用 Gmail 发送电子邮件这里您可以选择使用 TLS 或 SSl

Java Mail API

Java 邮件 API

E-mailing through Java

通过 Java 发送电子邮件

keys used in java mailing system for your reference

java邮件系统中使用的键供您参考