Java Mail Api-邮件未发送

时间:2019-12-18 09:36:06

标签: java api email servlets web-project

我有一个Java Web项目。我正在尝试将“与我们联系”部分添加到我的Web项目中。 netbeans并没有警告我的任何东西,但是当我填充文本框并单击按钮时,邮件未发送。

这是我的servlet代码:

@WebServlet(name = "MailDispatcherServlet", urlPatterns = {"/MailDispatcherServlet"})
public class MailDispatcherServlet extends HttpServlet {

    @EJB
    private MailSenderBean mailSender;



    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String toEmail = request.getParameter("email");
        String subject = request.getParameter("subject");
        String message = request.getParameter("message");

        String fromEmail = "gorkemsoftware@gmail.com"; 
        String usurname = "gorkemsoftware"; 
        String password = "mypasword"; 



        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */

            mailSender.sendEmail(fromEmail, usurname, password, toEmail, subject, message);



            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>MAIL STATUS</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>MAIL STATUS !!!</h1>");
            out.println("<b>MAIL SENT SUCCESSFULLY  </b><br>"); 
            out.println("Click <a href='frmMail.jsp'>here</a> to go back!!!");
            out.println("</body>");
            out.println("</html>");
        }
    }

这是我的Bean代码:

@Stateless
public class MailSenderBean {

    public void sendEmail(String fromEmail, String username, String password,
            String toEmail, String subject, String message){

        try {
            Properties props = System.getProperties();
            props.put("mail.smtp.host", "stmp.gmail.com");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.fallback", "false");


            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(true);

            Message mailMessage = new MimeMessage(mailSession);
            mailMessage.setFrom(new InternetAddress(fromEmail));
            mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            mailMessage.setContent(message, "text/html");
            mailMessage.setSubject(subject);

            Transport transport = mailSession.getTransport("smtp");
            transport.connect("smtp.gmail.com", username, password);

            transport.sendMessage(mailMessage, mailMessage.getAllRecipients());

        } catch (Exception ex) {
            Logger.getLogger(MailSenderBean.class.getName()).log(Level.SEVERE, null, ex);
        }



    }

}

有人可以告诉我这个项目有什么问题吗?邮件地址和密码也正确。然后我更改了gmail上的安全设置。

这是我单击发送按钮时的输出:

邮件状态!!!

邮件发送成功 点击这里返回!!!

1 个答案:

答案 0 :(得分:0)

我认为通过SSL(端口465)发送邮件时,我们有pb: 我希望对您的代码进行如下更改:

public void sendEmail(String fromEmail, String username, String password,
        String toEmail, String subject, String message){
    try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "stmp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.ssl.enable", "true"); // added
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Authenticator auth = new SMTPAuthenticator(username, password); // changed
        Session mailSession = Session.getInstance(props, auth); // changed
        mailSession.setDebug(true);

        Message mailMessage = new MimeMessage(mailSession);
        mailMessage.setFrom(new InternetAddress(fromEmail));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        mailMessage.setContent(message, "text/html");
        mailMessage.setSubject(subject);

        InternetAddress[] smtpAddress = { new InternetAddress("stmp.gmail.com") }; // changed
        Transport transport = mailSession.getTransport(smtpAddress[0]); // changed
        transport.connect();// changed

        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        transport.close(); // added
    } catch (Exception ex) {
        Logger.getLogger(MailSenderBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

类SMTPAuthenticator:

private class SMTPAuthenticator extends Authenticator {
    String username = null;
    String password = null;

    SMTPAuthenticator(String user, String pass) {
        username = user;
        password = pass;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}