【问题标题】:sending email using java code through smtp通过 smtp 使用 java 代码发送电子邮件
【发布时间】:2018-09-01 18:48:21
【问题描述】:

我得到这个错误和超时异常。

输入代码:

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");

调试 SMTP:尝试连接到主机“smtp.gmail.com”,端口 587,isSSL true Exceptioninthread“main”java.lang.RuntimeException:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;超时 -1。

【问题讨论】:

  • 发布堆栈跟踪、生成它的代码等以重现问题。
  • 顺便说一句,您正在使用代码 sn-p 来编写简单的英语,反之亦然
  • @Aniket sahrawat 属性 props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host",host); props.put("mail.smtp.port", 端口); props.put("mail.debug", "true");
  • host="smtp.gmail.com";端口=:587";
  • yes..Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); @莫里斯佩里

标签: java smtp jakarta-mail


【解决方案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();
    }
  }

调用上述方法并将这些参数传递给方法

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);
}

我使用了以下 jar 文件,

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

【讨论】:

  • 那么属性“mail.smtp.ssl.trust”会有所作为吗?
【解决方案2】:

忽略大多数错误或不相关的其他答案和 cmets。其中许多包括这些common JavaMail mistakes。您的原始代码很好。

请参阅JavaMail FAQ for GMail instructions

您很可能无法连接,因为有一些防火墙阻止了直接访问。 JavaMail FAQ 有connection debugging tips

如果不能直接连接,需要通过代理服务器连接,JavaMail FAQ also covers that

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 2016-10-30
    • 2018-10-12
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2011-04-06
    • 2017-05-12
    相关资源
    最近更新 更多