javax.mail.SendFailedException 为啥当我尝试通过我的 java 代码发送电子邮件时收到上述异常?

Posted

技术标签:

【中文标题】javax.mail.SendFailedException 为啥当我尝试通过我的 java 代码发送电子邮件时收到上述异常?【英文标题】:javax.mail.SendFailedException Why I receive the above mentioned expcetion when i try to send email through my java code?javax.mail.SendFailedException 为什么当我尝试通过我的 java 代码发送电子邮件时收到上述异常? 【发布时间】:2015-03-28 02:13:06 【问题描述】:

我想编写一个 java 程序来从我的电子邮件地址向任何电子邮件地址发送电子邮件,但是当我运行我的代码时,我得到了这个 javax.mail.SendFailedException。我的代码有 2 个类 Main.java 和 EmailManagement.java。 我收到的错误是:

javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at EmailManagement.sendEmailToOnePlayer(EmailManagement.java:43)
    at Main.main(Main.java:14)

代码如下。

导入 javax.swing.JOptionPane;

public class Main 

    public static void main(String[] args) 
        EmailManagement objEmail = new EmailManagement();

        String senderEmailId = JOptionPane.showInputDialog("ENter sender email id");
        String recieverEmailId = JOptionPane.showInputDialog("Enter reciever Email id");
        String subjectOfEmail = JOptionPane.showInputDialog("Enter email subject");
        String textOfEmail = JOptionPane.showInputDialog("Enter text Of mail");

        objEmail.sendEmailToOnePlayer(senderEmailId, recieverEmailId, subjectOfEmail, textOfEmail);
        //System.out.println(senderEmailId);

    





import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class EmailManagement 

    public void sendEmailToOnePlayer(String senderEmailId, String recieverEmailId , String subjectOfEmail , String textOfEmail )

        //using localhost for sending email
        String host ="localhost";

        //to get the system properties
        Properties  properties = System.getProperties();

        //now to setup the mail server
        properties.setProperty("mail.smtp.host", host);

        //now to get our default session object
        Session session =Session.getDefaultInstance(properties);

        //now to create default Mime message 
        MimeMessage message = new MimeMessage(session);


        try 
            //now to set the sender email address
            message.setFrom(new InternetAddress(senderEmailId));

            //now set reciever email address
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recieverEmailId));

            //set the subject of email to be sent 
            message.setSubject(subjectOfEmail);

            //actual message text of email
            message.setText(textOfEmail);


            //send message to required recipient
            Transport.send(message);


            System.out.println("email sent successfully :) ");



         catch (AddressException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
         catch (MessagingException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        



    

【问题讨论】:

您的本地机器上是否运行着 SMTP 服务器? 给我们MCVE 并校对您的问题,使其清晰易读。 如果您不介意,因为我是新手,如何检查 SMTP 服务器是否在我的本地计算机上运行? @QPaysTaxes 你能告诉我什么是 MCVE @AbdullahJahangirAbbasi 点击这个词。这是一个秘密链接。 【参考方案1】:

您没有在localhost:25. 上运行的 SMTP 服务器事实上,您没有在该 host:port 上运行任何类型的 TCP 服务器。

【讨论】:

请告诉我该怎么做 连接到确实存在的 SMTP 服务器。不要让我告诉你IP:端口。这是你的系统:没有人能告诉你。

以上是关于javax.mail.SendFailedException 为啥当我尝试通过我的 java 代码发送电子邮件时收到上述异常?的主要内容,如果未能解决你的问题,请参考以下文章