2

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

我尝试了这里给出的代码,但是我得到了异常

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 = "xx@gmail.com";

        // Sender's email ID needs to be mentioned
        String from = "xx@gmail.com";

        // 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 或任何私人电子邮件 ID)

或者

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

4

3 回答 3

1

这不是严格意义上的 Java 问题。您需要一个传出 SMTP 服务器,您可以将消息发送到该服务器,并且该服务器会注意将其传送到适当的 google、yahoo、aol 服务器。

作为“主机”,您必须设置该 SMTP 服务器,或者如果您愿意,可以在您的机器上安装 SMTP 服务器,但这是一项艰巨的任务。通常大公司和服务提供商都有 SMTP 服务器,可以接受来自其网络内部的任何邮件。

然而,大多数 SMTP 服务器是不开放的,并且不会让您随意发送电子邮件,至少不是免费的。您可以创建一个 gmail 帐户,并使用 gmail SMTP (smtp.gmail.com),使用您的 Transport 的帐户用户名和密码向 Google 的 SMTP 服务器进行身份验证。

此外,您不能总是指定“发件人”标头,它可能会被 SMTP 服务器重写以反映发送邮件的实际帐户,或者如果不是来自正确的 SMTP 服务器,则可能在另一端被视为垃圾邮件。

我建议您阅读 SMTP 的工作原理,它是一种过于复杂且相当陈旧的协议,但值得了解它的工作原理。

于 2012-06-28T23:00:48.143 回答
1

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

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("emailid@gmail.com",
                    "yourPassword"));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.setFrom("emailid@gmail.com");
            email.setSubject("Hi");
            email.setMsg("This is a test mail ... :-)");
            email.addTo("senderId@yahoo.co.in");
            email.setTLS(true);
            email.send();
            System.out.println("Mail sent!");
        } catch (Exception e) {
            System.out.println("Exception :: " + e);
        }
    }
}
于 2012-06-28T23:26:57.923 回答
0

您没有在 localhost:25 上运行的 SMTP 邮件服务器。要么您的邮件配置错误,您应该与其他邮件服务器通信,或者您确实打算与 localhost 中的一个通信,在这种情况下,您需要安装和/或运行它。

于 2012-06-28T23:00:26.703 回答