How to send mail using Google App Engine?Very Urgent, I have been waiting for your reply.

29 views
Skip to first unread message

Abraham

unread,
Sep 12, 2011, 6:41:58 AM9/12/11
to Google Web Toolkit
I don't know what mistake i done on my code segment to send e-mail
using Google App Engine. I refered the documentation of Google App
Engine from http://code.google.com/appengine/docs/python/mail/sendingmail.html.

I attach my code segment of client and server.

Clientside code.

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class MainEntry implements EntryPoint {
RequestBuilder rb = null;

@Override
public void onModuleLoad() {
// Window.Location.replace("/hello");
rb = new RequestBuilder(RequestBuilder.POST, "mailserver");
rb.setCallback(new RequestCallback() {

@Override
public void onResponseReceived(Request request, Response response)
{
// TODO Auto-generated method stub
Window.alert("Mail send");
}

@Override
public void onError(Request request, Throwable exception) {
// TODO Auto-generated method stub

}
});
Button button = new Button("Mail service");
button.addClickHandler(new ClickHandler() {

@Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub

try {
rb.send();
} catch (RequestException e) {

}
}
});

RootPanel.get().add(button);
}

Serverside code.

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class MailServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Mail Servlet is called");
Properties properties = new Properties();
Session session = Session.getDefaultInstance(properties, null);
String message = "Welcome to WWW.DataStoreGwt.com";
try
{
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("gan.t...@gmail.com",
"Rajaganapathi velayutham"));
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress("j.ga...@datastoregwt.com", "J.Ganesan"));
msg.setSubject("Invitation from www.tanya.com");
msg.setText(message);
Transport.send(msg);

}
catch (AddressException e1)
{

}
catch (MessagingException e2)
{

}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
}

Shawn Brown

unread,
Sep 12, 2011, 7:00:51 AM9/12/11
to google-we...@googlegroups.com
>        public void init(ServletConfig config) throws ServletException {
>                super.init(config);
>                System.out.println("Mail Servlet is called");
>                Properties properties = new Properties();
>                Session session = Session.getDefaultInstance(properties, null);
>                String message = "Welcome to WWW.DataStoreGwt.com";
>                try

Will you really have active session within the init method of a
servlet? I would think not.

Shawn

Shawn Brown

unread,
Sep 12, 2011, 7:05:35 AM9/12/11
to google-we...@googlegroups.com

try

@Override public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
//in here
}

Stephen Buergler

unread,
Sep 12, 2011, 8:51:58 AM9/12/11
to google-we...@googlegroups.com
You are not using the python API http://code.google.com/appengine/docs/java/mail/overview.html
You are not currently on the App Engine group:
The init() method of the Java HTTP servlet is only called when the servlet instance is loaded.
What you are doing is similar to sending an email in a constructor.
Do what Shawn suggested and override the doGet or doPost methods (depending on what type
of http request you are going to use) and make sure that the Servlet is properly defined in the
I looks like you will want something like:   
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>mailserver</servlet-name>
       
<servlet-class>MailServlet</servlet-class>   </servlet> <servlet-mapping>         <servlet-name>mailserver</servlet-name>
       
<url-pattern>/mailserver/*</url-pattern>
   
</servlet-mapping> </web-app>
Also like Shawn said, if you are planning on using sessions you will need to enable them on App

Abraham

unread,
Sep 13, 2011, 5:16:23 AM9/13/11
to Google Web Toolkit
Thank you Mr.shawn Brown. I override the dopost method it works.


On Sep 12, 4:05 pm, Shawn Brown <big.coffee.lo...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages