views:

107

answers:

2

I use the following method to send email in the Google App Engine servlet :

  void Send_Email(String From,String To,String Message_Text)
  {
    Properties props=new Properties();
    Session session=Session.getDefaultInstance(props,null);
    try
    {
      Message msg=new MimeMessage(session);
      msg.setFrom(new InternetAddress(From,"nmjava.com Admin"));
      msg.addRecipient(Message.RecipientType.TO,new InternetAddress(To,"Ni , Min"));
      msg.setSubject("Servlet Message");
      msg.setText(Message_Text);
      Transport.send(msg);
    }
    catch (Exception ex)
    {
      // ...
    }
  }

But it doesn't work, have I missed anything ? Has anyone got the email function working ?


Edit : I've fixed the String/Text part, but the email function still doesn't work, it says emails were sent, but I checked my mailbox, none received. I ran it on the Google server, what's wrong ?

A: 

I don't know much about Java, but maybe if you removed the try/catch stuff you could see what the error actually is?

According to the docs:

"When an application running in the development server calls the Mail service to send an email message, the message is printed to the log. The Java development server does not send the email message."

Does anything show up in the log?

Ross M Karchner
Where is the log, I can't find it in the project folder.
Frank
When you're running in development mode, your logs will print where your server is running (also, the mail will not be sent from the development server). When it's deployed, the logs will be visible from the Admin Console: http://code.google.com/appengine/docs/java/runtime.html#Logging
Jason Hall
Yes, I found it online, the error log says :Uncaught exception from servletjava.lang.IllegalArgumentException: content: String properties must be 500 characters or less. Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length.
Frank
So somewhere in your app, you have a datastore entity with a String field that's longer than 500 characters. Change that to `Text` and you should be fine.
Jason Hall
+2  A: 

I figured out, I need to give it my Gmail address as sender, otherwise no email will go out.

Frank