小编Bis*_*das的帖子

发送邮件时出错 - ConnectException

我使用以下Java代码发送电子邮件.

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

public class SendEmail
{
   public static void main(String [] args)
   {    

      String to = "abcd@gmail.com";
      String from = "web@gmail.com";
      String host = "localhost";
      Properties properties = System.getProperties();
      properties.setProperty("smtp.gmail.com", 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();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

当我运行该文件时,我收到以下错误:

javax.mail.MessagingException: Could not connect to …
Run Code Online (Sandbox Code Playgroud)

java email javax.mail connectexception

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

connectexception ×1

email ×1

java ×1

javax.mail ×1