无法连接到SMTP主机:smtp.gmail.com,port:465,响应:-1(JSP)

时间:2018-04-05 07:07:21

标签: email jsp

我只是尝试使用JAVA BEANS(JSP)发送确认电子邮件,但我收到了这个恼人的错误:

  

无法连接到主机" smtp.gmail.com",端口:465,响应:-1

得到这个课程

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; 

public class MailServiceBean {
  private String fromEmail, password;
  private String toEmail, subject, message;

public MailServiceBean(){
    fromEmail = "dumb@gmail.com";
    password = "dumbpass";
    toEmail = "";
    subject = "";
    message = "";
}

public void sendConfirmationMail(){
    try {
        Properties properties = System.getProperties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");    
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");                        
        Session mailSession = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail,password);
        }
        });
        mailSession.setDebug(true);
        Message mail = new MimeMessage(mailSession);      
        mail.setFrom(new InternetAddress(fromEmail));
        mail.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
        mail.setContent(message, "text/html");
        mail.setSubject(subject);
        Transport.send(mail);
        System.out.println("Ya se mando");
    } catch (MessagingException ex) {
        Logger.getLogger(MailServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

/**
 * @return the fromEmail
 */
public String getFromEmail() {
    return fromEmail;
}

/**
 * @param fromEmail the fromEmail to set
 */
public void setFromEmail(String fromEmail) {
    this.fromEmail = fromEmail;
}

/**
 * @return the password
 */
public String getPassword() {
    return password;
}

/**
 * @param password the password to set
 */
public void setPassword(String password) {
    this.password = password;
}

/**
 * @return the toEmail
 */
public String getToEmail() {
    return toEmail;
}

/**
 * @param toEmail the toEmail to set
 */
public void setToEmail(String toEmail) {
    this.toEmail = toEmail;
}

/**
 * @return the subject
 */
public String getSubject() {
    return subject;
}

/**
 * @param subject the subject to set
 */
public void setSubject(String subject) {
    this.subject = subject;
}

/**
 * @return the message
 */
public String getMessage() {
    return message;
}

/**
 * @param message the message to set
 */
public void setMessage(String message) {
    this.message = message;
}
}

我在这个页面中调用了MailServiceBean类:

<%@page import="prueba.MailServiceBean"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
   MailServiceBean mail = new MailServiceBean();
   mail.setToEmail("toMail@hotmail.com");
   mail.setSubject("Dumb Subject");
   mail.setMessage("Testing mail");
   mail.sendConfirmationMail();
   response.sendRedirect("pagina.html");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

我该如何纠正?我必须承认代码工作了几次,现在它没用了。 :(

1 个答案:

答案 0 :(得分:0)

好的,最终找到了解决方案......只需添加此属性:

properties.put("mail.smtp.ssl.enable", "true");

就是这样。