javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:587; java.net.ConnectException:连接超时:连接

时间:2016-04-04 06:28:29

标签: java smtp javamail

在通过提供相同问题的帖子后,我编写了以下代码。但我得到以下例外:

  

javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:587;嵌套异常是:java.net.ConnectException:连接超时:连接

package com.nura.mail;

import constants.Constants;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import logger.LoggerUtil;

/**
 *
 * @author Arun kumar
 */
public class SendMail implements Constants {

    private final static LoggerUtil log = new LoggerUtil();

    public static void main(String receiver, String msg, String header) {

        log.addLog("Entry in SendMail");
        final String username = "sendmailnotification";
        final String password = "version5.0";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(receiver));
            message.setSubject(header);
            message.setText(msg);
            Transport.send(message);
            log.addLog("Your mail has been sent");
            log.addLog("Exit from Send Mail");
        } catch (MessagingException e) {
            log.addLog(e.getLocalizedMessage());
        }
    }
    public static void main(String[] args) {
        SendMail.main("aprodigalboy@gmail.com", "test", "test");
        System.out.println("End of the program");
    }
}

我创建了一个注册表单,我从注册字段收到电子邮件并发送OTP但由于此错误我无法收到OTP

  

SLF4J:类路径包含多个SLF4J绑定。   SLF4J:在[jar:file:/ C:/ Program%20Files%20(x86)/NetBeans%208.0.1/java/modules/ext/hibernate4/slf4j-simple-1.6.1.jar!/org/中找到绑定SLF4J / IMPL / StaticLoggerBinder.class]   SLF4J:在[jar:file:/ D:/ Ashok / Final / CloudArmour%20(2)/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定   SLF4J:在[jar:file:/ D:/ Ashok / Final / CloudArmour%20(2)/lib/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定   SLF4J:有关解释,请参阅http://www.slf4j.org/codes.html#multiple_bindings。   492 [main] INFO logger.LoggerUtil - SendMail中的条目   程序结束   22060 [main] INFO logger.LoggerUtil - 无法连接到SMTP主机:smtp.gmail.com,port:587

0 个答案:

没有答案