-6

Not work,QQ port is 435 package com.ys.service;

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

public class SendMailController {
    public static void main(String [] args)
       {    
          // Recipient's email ID needs to be mentioned.
          String to = "1255****@qq.com";

          // Sender's email ID needs to be mentioned
          String from = "5915******@qq.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);

          // 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!");

             // Now set the actual message
             message.setText("This is actual message");

             // Send message
             Transport.send(message);
             System.out.println("Sent message successfully....");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }
       }
}
  1. Error:com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  2. nested exception is:java.net.ConnectException: Connection refused: connect;
  3. Caused by: java.net.ConnectException: Connection refused: connect;
Ironxi
  • 1
  • 4
  • It's saying that it can't connect to a mail server on port 25 on your local machine. This doesn't seem to be a java problem. Have you tried to send an email manually from the command line to see if it works? – assylias Jun 06 '18 at 11:09
  • you can try with your gmail account with host smtp.gmail.com or you have ti install a mock mail server locally, I think.. – Amit Jun 06 '18 at 11:13
  • I try to other port,like port 456,but still no work;I try to send my friends email,it seems,no work; – Ironxi Jun 06 '18 at 12:46
  • Gmail,I do not want show my password for this Demo, I use 163.com,still no work; – Ironxi Jun 06 '18 at 12:49
  • make sure that you've allowed 3rd-party access to your gmail account. https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#connectedapps – Nikolai Shevchenko Jun 06 '18 at 13:45
  • I allow 3rd-party access to my gmail account. – Ironxi Aug 12 '18 at 11:48
  • I use 163 mail successful send the information on this Demo – Ironxi Aug 12 '18 at 11:51

1 Answers1

-1

In my case this issue was because my internet provider have a filter on port 25. I had to deactivate the filter in my account settings.

GauthierG
  • 308
  • 1
  • 7