Question

I am trying to send an email from my servlet. There is no exception but i am not getting email in my account. I am using the following code:

 public class SendEmailServlet extends HttpServlet {
@SuppressWarnings({ "unchecked" })
public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try
    {
         String to[] = {"mygmail@gmail.com"};
            String host = "smtp.gmail.com";
            String username = "mygmail@gmail.com";
            String password = "password";
            Properties props = new Properties();
            props.put("mail.smtps.auth", "true");
            // ...
            Session session = Session.getInstance(props);
            MimeMessage msg = new MimeMessage(session);
            // set the message content here
            msg.setFrom(new InternetAddress(username,"Me"));
            msg.setSubject("Testing");
            msg.setText("Testing...");
            Address[] addresses = new Address[to.length];
            for (int i = 0; i < to.length; i++) {
                Address address = new InternetAddress(to[i]);               
                addresses[i] = address;
                // Add the given addresses to the specified recipient type.
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
            } 

            Transport t = session.getTransport("smtps");

            t.connect(host, username, password);
            t.sendMessage(msg, msg.getAllRecipients());
            t.close();

            out.println(0);
        }
    }
    catch (Exception e)
    {
       System.out.println("Exception error: " + e);
       out.println(e);
    }
    finally
    {
        pm.close();
    }
}

}

i am getting following exception now:

  Exception error: java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve)

Can anybody tell me why i am not recieving email and whats wrong with this code. Thanks in advance

Was it helpful?

Solution

Read https://developers.google.com/appengine/docs/java/mail/overview Basically stop including any JavaMail libraries in your app and don't try to connect to a host. Build the message and jump straight to Transport.send(msg);

OTHER TIPS

it seems like mistake is :

props.put("mail.smtp.host","localhost");

it should not be localhost until you are running mail server on your system..

look at this question : Using Javamail to connect to Gmail smtp server ignores specified port and tries to use 25

and yes as commented by Axel try with commandline first

Are you running an e-mail server on your localhost? If so, does it have authentication disabled?

Maybe you can see the error messages in its logs.

The reason is , you are using localhost as the smtp host , you should use deploy mail server on your computer before using localhost as a sender. Even after that you may require to register your server.

you can also do that using gmail or yahoo mail. Use the smtp host of google mail. Authenticate using your creds and then go ahead.

try this link :

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Generally you can use JavaMail to send mail using servlets. You stated that you are using AppEngine ( Why can't i send email from my servlet and getting java.security.AccessControlException? ). Google App Engine allows you to send email using JavaMail, But you cannot use JavaMail provided by appengine to connect to any mail server.SMTP configuration added to the Transport or Session is ignored. https://developers.google.com/appengine/docs/java/mail/usingjavamail#JavaMail_Features_Not_Supported

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top