-1

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

Piscean
  • 3,069
  • 12
  • 47
  • 96
  • 1
    Try sending from command line or email client via SMTP on your localhost and check if that works first. Perhaps the local SMTP server isn't configured correctly, then this would be something to ask for in another forum (perhaps superuser?). – Axel Jul 26 '12 at 13:19
  • 2
    This problem has got nothing to do with servlets. Get it to work first using a normal Java class which you execute/test by a simple `main()` method, then reuse it in the servlet. Please don't repost the same question over and over. Edit instead the question to improve it. http://stackoverflow.com/questions/11672491/why-cant-i-send-email-from-my-servlet-and-getting-java-security-accesscontrolex – BalusC Jul 26 '12 at 16:58
  • You should be passing an SMTPAuthenticator to your properties when you construct it. – Joel Jul 26 '12 at 17:17

5 Answers5

0

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

Community
  • 1
  • 1
Harmeet Singh
  • 2,555
  • 18
  • 21
0

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.

Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
0

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/

Mohan Singh
  • 349
  • 1
  • 3
  • 5
0

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

Community
  • 1
  • 1
Raghav
  • 4,590
  • 1
  • 22
  • 32
0

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

Chase
  • 3,123
  • 1
  • 30
  • 35
  • plus i uploaded it on app engine and use gmail as host. on localhost its not working but when i upload it on app engine then it works fine – Piscean Aug 08 '12 at 10:20