繁体   English   中英

MailConnectException:嵌套异常:java.net.SocketException:权限被拒绝:connect

[英]MailConnectException : nested exception: java.net.SocketException: Permission denied: connect

我试图使用此示例将邮件从gmail ID发送到另一个。 这是我的文件:

public class SendHTMLEmail

  // Recipient's email ID needs to be mentioned.
  String to = "harsh.hr99@gmail.com";

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

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

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.user", "Admin");
  properties.setProperty("mail.password", "admin");

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

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

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Send the actual HTML message, as big as you like
     message.setContent("<h1>This is actual message</h1>", "text/html" );

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

我在localhost:9091端口上运行Tomcat服务器。 我收到以下错误:

屏幕截图来自cmd

我该如何解决?

好吧,您设置“ host = localhost”,这样您的程序将尝试使用位于localhost的smtp服务器。 问题是:您没有(也不想)在本地主机上安装了smtp服务器。 至少看起来是这样。

我认为您想做的是: http : //www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

如果尝试此示例,则需要记住不要发布它,因为它包含硬编码的Gmail凭据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM