Using JavaMail in a GWT application

187 views
Skip to first unread message

Jared Smith

unread,
Sep 29, 2009, 5:37:16 PM9/29/09
to google-we...@googlegroups.com
I am having a bit of an issue testing my web application.  I have written a customized Emailer class that is used to send an email to the specified email address.  I am able to get the class to successfully send an email while using a driver class, however, when I attempt to use the Emailer class on the server side, no email is sent.  I have embeded the code in a try-catch block and I am not catching any exceptions.  Does anyone have any knowledge as to why this may be the case?



Server Side Code:
************************************************************************************************************************
/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class EmailServiceImpl extends RemoteServiceServlet implements EmailService {
    public String sendEmail(String email) {
        try {
            Emailer eMail = new Emailer();
            eMail.setEmailRecipient(email);
            eMail.setEmailSender(email);
            eMail.setEmailSubject("Its Jail!");
            eMail.setEmailText("No, java mail silly!!...");
            eMail.send();
            return "an email has successfully been sent to the specified email address.";
        } catch(Exception e) {
            return "An exception has occured.";
        }       
    }
}
************************************************************************************************************************

Emailer class:
************************************************************************************************************************
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;


public class Emailer {
    private String smtpHost;
    private int smtpPort;
    private String emailSender;
    private String emailRecipient;
    private String emailSubject;
    private String emailText;
   
    public Emailer() {
        smtpHost = "mysmtphost";
        smtpPort = 25;
    }
   
    public Emailer(String host, int port) {
        smtpHost = host;
        smtpPort = port;
    }
   
    public void setEmailSender(String sender) {
        emailSender = sender;
    }
   
    public void setEmailRecipient(String recipient) {
        emailRecipient = recipient;
    }
   
    public void setEmailSubject(String subject) {
        emailSubject = subject;
    }
   
    public void setEmailText(String text) {
        emailText = text;
    }
   
    public void send() {
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPort);
        Session ses = Session.getDefaultInstance(props, null);
        Message msg = new MimeMessage(ses);
        try {
            msg.setFrom(new InternetAddress(emailSender));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient));
            msg.setSubject(emailSubject);
            msg.setText(emailText);
            Transport.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }   
    }
}
************************************************************************************************************************

mikedshaffer

unread,
Sep 30, 2009, 4:54:02 PM9/30/09
to Google Web Toolkit
Your javax code looks fine (I have props.put
("mail.transport.protocol", "SMTP" ); also, but I don't recall if it's
necessary). So I'd check to see if the mail server is receiving the
email. A couple ways to do that is to turn on logging on the mail
server (that's not my area of expertise but we've had to do it a time
or two) and/or use a product like Wireshark to look at the traffic on
the network. To do that, you'll need console access to your GWT
server...but sniffing the wire is pretty much the only way to see
what's going on in javax.

Good luck.

On Sep 29, 3:37 pm, Jared Smith <jaredcsm...@gmail.com> wrote:
> I am having a bit of an issue testing my web application.  I have written a
> customized Emailer class that is used to send an email to the specified
> email address.  I am able to get the class to successfully send an email
> while using a driver class, however, when I attempt to use the Emailer class
> on the server side, no email is sent.  I have embeded the code in a
> try-catch block and I am not catching any exceptions.  Does anyone have any
> knowledge as to why this may be the case?
>
> Server Side Code:
> ***************************************************************************­*********************************************
> /**
>  * The server side implementation of the RPC service.
>  */
> @SuppressWarnings("serial")
> public class EmailServiceImpl extends RemoteServiceServlet implements
> EmailService {
>     public String sendEmail(String email) {
>         try {
>             Emailer eMail = new Emailer();
>             eMail.setEmailRecipient(email);
>             eMail.setEmailSender(email);
>             eMail.setEmailSubject("Its Jail!");
>             eMail.setEmailText("No, java mail silly!!...");
>             eMail.send();
>             return "an email has successfully been sent to the specified
> email address.";
>         } catch(Exception e) {
>             return "An exception has occured.";
>         }
>     }}
>
> ***************************************************************************­*********************************************
>
> Emailer class:
> ***************************************************************************­*********************************************
> import java.util.*;
> import javax.mail.*;
> import javax.mail.internet.*;
>
> public class Emailer {
>     private String smtpHost;
>     private int smtpPort;
>     private String emailSender;
>     private String emailRecipient;
>     private String emailSubject;
>     private String emailText;
>
>     public Emailer() {
>         smtpHost = "*mysmtphost*";
> ***************************************************************************­*********************************************

charlie

unread,
Sep 30, 2009, 4:57:29 PM9/30/09
to google-we...@googlegroups.com
Throwable is the root exception class, so something might be getting thrown you're not catching.

Jared Smith

unread,
Oct 1, 2009, 5:27:15 PM10/1/09
to google-we...@googlegroups.com
Well only errors would not be caught.  However, I have revised my code to catch anything throwable and am not getting anything.  I also checked the exchange box, and the emails are not hitting it, so it has something to do with the appengine I suppose.  I also noticed that the javax.mail is included in the appengine SDK so no import is necessary to use it. 

Mike, are you able to get yours to send an email successfully?  If so, are you using the appengine sdk?  Also, are you developing the web app in Eclipse? 

alex.d

unread,
Oct 2, 2009, 2:22:11 AM10/2/09
to Google Web Toolkit
this code works for me:

String smtpServer = "mailserver01";
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props);
Message msg = new MimeMessage(session);

InternetAddress addressFrom;
addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress addressTo = new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);

msg.setSubject(subject);
msg.setContent(message, "text/plain");
// msg.setHeader("Content-Type", "text/plain" + (charset == null ?
"" : "; charset="+charset));
Transport.send(msg);
} catch (Exception e) {
System.err.println(e.getMessage()); e.printStackTrace();
}

On Oct 1, 11:27 pm, Jared Smith <jaredcsm...@gmail.com> wrote:
> Well only errors would not be caught.  However, I have revised my code to
> catch anything throwable and am not getting anything.  I also checked the
> exchange box, and the emails are not hitting it, so it has something to do
> with the appengine I suppose.  I also noticed that the javax.mail is
> included in the appengine SDK so no import is necessary to use it.
>
> Mike, are you able to get yours to send an email successfully?  If so, are
> you using the appengine sdk?  Also, are you developing the web app in
> Eclipse?
>
> On Wed, Sep 30, 2009 at 3:57 PM, charlie <charlie.f...@gmail.com> wrote:
> > Throwable is the root exception class, so something might be getting thrown
> > you're not catching.
>
Reply all
Reply to author
Forward
0 new messages