java - 用Java发送/接收电子邮件

标签 java email sendmail post-receive-email

我想通过 Java 发送电子邮件(任何来自 yahoo、gmail 或任何其他部分的电子邮件)。

我尝试了代码 here ,但是我得到了异常

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at myemailtesting.MyEmailTesting.main(MyEmailTesting.java:72)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
    ... 7 more

我的代码是

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myemailtesting;

/**
*
* @author xxxx
*/
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MyEmailTesting {

    public static void main(String[] args) {

        System.out.println("This is EMAIL testing!!!");
        // Recipient's email ID needs to be mentioned.
        String to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3cbcbf3d4ded2dadf9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>";

        // Sender's email ID needs to be mentioned
        String from = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c74744c6b616d6560226f6361" rel="noreferrer noopener nofollow">[email protected]</a>";

        // Assuming you are sending email from localhost
        String host = "localhost";

        // Get system properties
        System.out.println("test 001");
        Properties properties = System.getProperties();
        System.out.println("test 002");

        // Setup mail server
        System.out.println("test 003");
        properties.setProperty("mail.smtp.host", host);

        // Get the default Session object.
        System.out.println("test 004");
        Session session = Session.getDefaultInstance(properties);

        try {
            // Create a default MimeMessage object.
            System.out.println("test 005");

            MimeMessage message = new MimeMessage(session);

            System.out.println("test 006");

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            System.out.println("test 007");
            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            System.out.println("test 008");
            // Set Subject: header field
            message.setSubject("This is the Subject Line!");

            System.out.println("test 009");
            // Now set the actual message
            message.setText("This is actual message");

            System.out.println("test 010");
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

为了调试,我使用的语句为 System.out.println("test 00X");

我得到的输出为

This is EMAIL testing!!!
test 001
test 002
test 003
test 004
test 005
test 006
test 007
test 008
test 009
test 010
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

我尝试了很多代码,但没有得到任何输出。遇到一些异常。

我在某个地方看到,我需要保持 SMTP 服务器正常运行。我不知道需要做什么。我相信 apache commons 将是一个不错的选择。

有人可以帮我完成以下步骤吗

  1. 所需的 jar 文件
  2. 如何设置 smptp
  3. 发送电子邮件(从任何网站,即从 yahoo 或 gmail 或任何私有(private)电子邮件 ID)

用java发送电子邮件的分步过程...

最佳答案

试试这个...它的工作...

import org.apache.commons.mail.*;
public class EmailTest {
    public static void main(String[] args) {
        try {
            Email email = new SimpleEmail();
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ddd5d9d1d4d1dcf8dfd5d9d1d496dbd7d5" rel="noreferrer noopener nofollow">[email protected]</a>",
                    "yourPassword"));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.setFrom("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72171f131b1e1b1632151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>");
            email.setSubject("Hi");
            email.setMsg("This is a test mail ... :-)");
            email.addTo("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f1e7ece6e7f0cbe6c2fbe3eaededace1edacebec" rel="noreferrer noopener nofollow">[email protected]</a>");
            email.setTLS(true);
            email.send();
            System.out.println("Mail sent!");
        } catch (Exception e) {
            System.out.println("Exception :: " + e);
        }
    }
}

关于java - 用Java发送/接收电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254003/

相关文章:

ios - 在 Sprite Kit 中以编程方式发送邮件

ruby-on-rails - 使用 Ruby on Rails 发送邮件的最简单方法

java - 使用 org.eclipse.swt.ole 通过 java 代码发送电子邮件

iphone - 如何在不显示 MFMailComposeViewController 的情况下从 iPhone 应用程序发送邮件?

java - 将 Double 舍入到小数点后 1 位 kotlin : from 0. 044999 到 0.1

java - 从流中获取最后 n 个元素

email - 在数据库中存储用户电子邮件地址的最佳和最安全的方法是什么?

ssl - 我想我缺少了一些东西:无法使TLS的Rails + Postfix/Sendmail正常工作

java - install4j:在 MacOS 上安装的应用程序无法运行

java - 在 Java、Android 上将视频转换为 mp3