简体   繁体   English

无法连接到SMTP问题

[英]Cannot connect to smtp problems

I try to create some auto send mail using java But there's some error when buliding the projects. 我尝试使用Java创建一些自动发送邮件,但在创建项目时会出现一些错误。 Here's the code. 这是代码。

package sendmail2;

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

public class SendMail2 {

    public static void main(String[] args) {
        try{
            String host ="smtp.gmail.com" ;
            String user = "myemail@gmail.com";
            String pass = "mypassword";
            String to = "my reciever";
            String from = "myemail@gmail.com";
            String subject = "Test App";
            String messageText = "Congrats";
            boolean sessionDebug = false;

            Properties props = System.getProperties();

            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.required", "true");

            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject); msg.setSentDate(new Date());
            msg.setText(messageText);

           Transport transport=mailSession.getTransport("smtp");
           transport.connect(host, user, pass);
           transport.sendMessage(msg, msg.getAllRecipients());
           transport.close();
           System.out.println("message send successfully");
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}

The error is 错误是

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; nested exception is: java.net.ConnectException: Connection timed out: connect BUILD SUCCESSFUL (total time: 21 seconds) 嵌套的异常是:java.net.ConnectException:连接超时:connect BUILD SUCCESSFUL(总时间:21秒)

I already add activation.jar and also mail.jar in the library. 我已经在库中添加了activation.jar和mail.jar。 and Turn on less secure app access in gmail account. 并在Gmail帐户中打开不太安全的应用访问权限。

The question is I don't know if my code or something wrong and the solutions to make it works 问题是我不知道我的代码或某些错误以及使它起作用的解决方案

This is my first time using stack overflow so If my question is not clear or hard to read. 这是我第一次使用堆栈溢出,所以如果我的问题不清楚或很难阅读。 just let me know and sorry for the inconvinience. 请让我知道,对于您的不便,我们深表歉意。

The JavaMail FAQ has tips for debugging connection problems . JavaMail FAQ 提供了调试连接问题的技巧

Most likely there's a firewall preventing you from connecting directly. 很可能有防火墙阻止您直接连接。 The JavaMAil FAQ also describes how to connect through a proxy server . JavaMAil FAQ还介绍了如何通过代理服务器进行连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM