简体   繁体   中英

MailConnectException : nested exception: java.net.SocketException: Permission denied: connect

I was trying to send a mail from gmail id to another using this example . Here is my file:

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();
  }

I am running Tomcat server on localhost:9091 port. I am receiving following error:

screen shot from cmd

how do I solve this?

Well, you set "host = localhost" so your program will try to use a smtp server located at localhost. Problem is: You don't have (and don't want) a smtp server at localhost installed. At least it seems like that.

I think what you want to do is: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

If you trying this example you will need to keep in mind not to publish it because it contains hard-coded gmail credentials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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