简体   繁体   English

通过smtp使用Java代码发送电子邮件

[英]sending email using java code through smtp

I'm getting this below error and timeout exception. 我正在下面的错误和超时异常。

enter code: 输入代码:

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

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL true Exceptioninthread"main"java.lang.RuntimeException:com.sun.mail.util.MailConnectException: Couldn't connect to host,port:smtp.gmail.com, 587; 调试SMTP:尝试连接到主机“ smtp.gmail.com”,端口587,isSSL true Exceptioninthread“主” java.lang.RuntimeException:com.sun.mail.util.MailConnectException:无法连接到主机,端口: smtp.gmail.com,587; timeout -1. 超时-1。

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
  //String host="localhost";
  //props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.starttls.enable", "true");
  //props.put("mail.smtp.host", host);
    props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");//587
    props.put("mail.smtp.auth", "true");
  //System.out.println("1");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
         //System.out.println("2");

        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

         //System.out.println("3");

        message.setSubject(subject);
        message.setText(body);
        // System.out.println("4");

        Transport transport = session.getTransport("smtp");
        // System.out.println("5");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        //System.out.println("sent successfully...");
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
  }

calling above method and pass these parameters to the method 调用上述方法并将这些参数传递给该方法

private static String USER_NAME = "xxxx";  // username "@gmail.com")
private static String PASSWORD = "xxxx"; // password
private static String RECIPIENT = "xxx@gmail.com";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "Welcome to JavaMail!";

    sendFromGMail(from, pass, to, subject, body);
}

And i used following jar files , 我使用了以下jar文件,

java-mail-1.4.4
javamail-smtp-1.4.2
pop3
mail-6.0.0-sources

Ignore most of the other answers and comments, which are wrong or irrelevant. 忽略其他大多数错误或不相关的答案和评论。 Many of them include these common JavaMail mistakes . 其中许多都包含这些常见的JavaMail错误 Your original code was fine. 您的原始代码很好。

See the JavaMail FAQ for GMail instructions . 有关GMail的说明,请参见JavaMail FAQ

Most likely you can't connect because there is some firewall preventing direct access. 您很可能无法连接,因为有一些防火墙阻止直接访问。 The JavaMail FAQ has connection debugging tips . JavaMail FAQ具有连接调试技巧

If you can't connect directly and need to connect through a proxy server, the JavaMail FAQ also covers that . 如果您无法直接连接,而需要通过代理服务器连接,则JavaMail FAQ还将介绍该内容

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

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