发送电子邮件时抛出com.sun.mail.util.MailConnectException

时间:2014-08-16 01:28:55

标签: java

我已经编写了这个用于发送电子邮件的java代码,但是这段代码引发了异常 (com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1;   嵌套异常是:) 请帮忙。

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

public class SendEmail
{
   public static void main(String [] args)
   {    
      String to = "to@gmail.com";
      String from = "from@seecs.edu.pk";
      String host = "localhost";
      Properties properties = System.getProperties();
      properties.setProperty("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(properties);

      try{
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         message.setSubject("This is the Subject Line!");
         message.setText("This is actual message");
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }
      catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

0 个答案:

没有答案