简体   繁体   中英

Java Mail Api - Mail Doesn't Sent

I have a java web project. I am trying to add a contact us part to my web project. There no anything that netbeans warn me but when I fill the textboxes and click button mail doesn't sent.

Here is my code of 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>");
        }
    }

And here is my code of 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);
        }



    }

}

Can someone tell me what is wrong in this project? The mail address and password is also correct. And I changed security setting on gmail.

and here is the output when I click send button:

MAIL STATUS !!!

MAIL SENT SUCCESSFULLY Click here to go back!!!

I think we have pb when send mail by SSL (port 465): I prefer some changes in your code like this :

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);
    }
}

The class 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM