views:

274

answers:

1

Hi,

I am creating a form, which will send out the details via email upon user completes his details and click submit.

Mail Submission with JavaMail:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    try {
        String host = "localhost";
        String from = "[email protected]";

        try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true);
        Transport transport = session.getTransport("smtp");

        MimeMessage message = new MimeMessage(session);
        Address fromAddress = new InternetAddress("[email protected]");

        message.setFrom(fromAddress);

        InternetAddress to = new InternetAddress("[email protected]");
        message.addRecipient(Message.RecipientType.TO, to);

        message.setSubject("Email Details Sending");
        message.setText("This is my testing content.");

        transport.connect(host, from);
        message.saveChanges();
        Transport.send(message);
        transport.close();
    } finally { 
        out.close();
    }
} 

I am using Email aliases for [email protected] which means I could have 4 email aliases from sendToAliases. However, I am unable to reach any emails upon deploying and running the above mail file. Can anyone please advise me?

Thank you.

+1  A: 
  • Have you checked the log files?
  • Do you get any exceptions or errors when running the program?
  • Do you have a SMTP server running in localhost?
  • Is the SMTP server accepting connections from localhost?
  • Can you send emails via that server using normal email client and receive them somehow?
  • Try to make your program a standalone commandline program and try to execute it

You seem to have a missing quote in message.setSubject("Email Details Sending);. Are you sure that your servlet actually compiles?

Juha Syrjälä
Hi,I tested that sending emails from the commandline using telnet, it works out perfectly. Regarding the message.setSubject("Email Details Sending); is a typo due to pasting. I have that in the code. Compilation is fine.
jl