Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

后端 未结 4 1398
悲&欢浪女
悲&欢浪女 2021-02-14 22:56

Here is the code of the application. I have been trying to run this using eclipse IDE. I also added all the required java mail jar files namely dsn.jar,imap.jar,mailapi.j

4条回答
  •  礼貌的吻别
    2021-02-14 23:23

    Email succeeded through Gmail using JDK 7 with below Gmail settings.

    Go to Gmail Settings > Accounts and Import > Other Google Account settings > and under Sign-in & security

    1. 2-Step Verification: Off
    2. Allow less secure apps: ON
    3. App Passwords: 1 password (16 characters long), later replaced the current password with this.

    used following maven dependencies:

    spring-core:4.2.2
    spring-beans:4.2.2
    spring-context:4.2.2
    spring-context-support:4.2.2
    spring-expression:4.2.2
    commons-logging:1.2
    
    
        javax.mail
        mail
        1.4.7
    
    

    and my source code is:

    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import javax.swing.JOptionPane;
    import java.util.List;
    import java.util.Properties;
    
    public class Email {
    
        public final void prepareAndSendEmail(String htmlMessage, String toMailId) {
    
            final OneMethod oneMethod = new OneMethod();
            final List resourceList = oneMethod.getValidatorResource();
    
            //Spring Framework JavaMailSenderImplementation    
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost("smtp.gmail.com");
            mailSender.setPort(465);
    
            //setting username and password
            mailSender.setUsername(String.valueOf(resourceList.get(0)));
            mailSender.setPassword(String.valueOf(resourceList.get(1)));
    
            //setting Spring JavaMailSenderImpl Properties
            Properties mailProp = mailSender.getJavaMailProperties();
            mailProp.put("mail.transport.protocol", "smtp");
            mailProp.put("mail.smtp.auth", "true");
            mailProp.put("mail.smtp.starttls.enable", "true");
            mailProp.put("mail.smtp.starttls.required", "true");
            mailProp.put("mail.debug", "true");
            mailProp.put("mail.smtp.ssl.enable", "true");
            mailProp.put("mail.smtp.user", String.valueOf(resourceList.get(0)));
    
            //preparing Multimedia Message and sending
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            try {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setTo(toMailId);
                helper.setSubject("I achieved the Email with Java 7 and Spring");
                helper.setText(htmlMessage, true);//setting the html page and passing argument true for 'text/html'
    
                //Checking the internet connection and therefore sending the email
                if(OneMethod.isNetConnAvailable())
                    mailSender.send(mimeMessage);
                else
                    JOptionPane.showMessageDialog(null, "No Internet Connection Found...");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    Hope this will help someone.

提交回复
热议问题