Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29758

Java Mail Api - Mail Doesn't Sent

$
0
0

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!!!


Viewing all articles
Browse latest Browse all 29758

Trending Articles