java - 通过 Java 发送电子邮件 - javax.mail.MessagingException : Could not connect to SMTP host: localhost, port: 587;

标签 java api sockets email jar

我目前正在从事一个涉及创建 .jar 文件的项目。以下是要实现的,我应该导出/读取 friend 注册表的路径,完成后我应该通过电子邮件取回结果。整个概念是创建 jar 文件,一旦点击它,我就会通过电子邮件收到结果,因为我实际上是通过电子邮件发送的。

(我希望这是有道理的)

所以首先,我结合了以下代码来实际读取注册表并显示键(我从流行的关于读/写注册表的堆栈溢出帖子中获得它)所以读取过程工作正常,现在我的问题带有电子邮件代码,

(我不太确定此代码的原始所有者是谁,但完全归功于他) 我正在尝试使此电子邮件代码的基本概念起作用,以便我可以继续将我的 jar 文件作为附件发送,并以用户单击 jar 文件时的方式将其链接到我的代码,注册表结果将通过电子邮件发回给我。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailCode {

  public static void main(String[] args) throws Exception {


     final String smtp_host = "smtp.gmail.com";
    final String smtp_username = "user@gmail.com";
    final String smtp_password = "password";
    final String smtp_connection = "TLS";  // Use 'TLS' or 'SSL' connection

    final String toEmail="tomail@hotmail.com";
    final String fromEmail="**@gmail.com";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");

   if (smtp_connection.equals("TLS")) {
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.port", "587");
    } else{
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.port", "465");
    }

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(smtp_username, smtp_password);
            }
      });

    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromEmail, "NoReply"));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(toEmail, "Mr. Recipient"));
        msg.setSubject("Welcome To JavaMail API");
        msg.setText("JavaMail API Test - Sending email example through remote smtp server");
        Transport.send(msg);
        System.out.println("Email sent successfully...");
    } catch (AddressException e) {
        throw new RuntimeException(e);
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
   }
 }

这是我收到的错误消息:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587;
nested exception is:
java.net.SocketException: Permission denied: connect  

最佳答案

下面的代码可能会帮助你解决你的问题,它的工作............

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

public class Email {

private static String USER_NAME = "username";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "password"; // GMail password

private static String RECIPIENT = "xxxxx@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 = "hi ....,!";

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

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
  String host = "smtp.gmail.com";

    props.put("mail.smtp.starttls.enable", "true");

    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");
    props.put("mail.smtp.auth", "true");


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

    try {


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



        message.setSubject(subject);
        message.setText(body);


        Transport transport = session.getTransport("smtp");


        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
    }
   } 

关于java - 通过 Java 发送电子邮件 - javax.mail.MessagingException : Could not connect to SMTP host: localhost, port: 587;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087018/

相关文章:

api - REST 模式创建、更新和删除相同的端点

C# UDP Socket 客户端和服务器

java - 如何将事务插入到 mysql 中的表中,使其对每个用户都是唯一的

java - 只有一个类的构建器模式

python - 获取由我公司完成的 DHL 的所有追踪号码

ruby - Ruby 异常的命名约定

c套接字: flushing data to let client know its okay to type

ruby - Socket.recv 有效但无法获取或读取?

java - 将文件读入数组返回错误的元素

java 正则表达式 - 必需的字符串验证