Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing mail with appengine development server (java)

I'm using javamail to send mails from my appengine application. It works perfectly in the deployment, but I can't figure out how to do this using the development server. Whenever I need to test the sendmail, I'm having to deploy the application which is quite annoying.

Background Information (Why logs don't work):

We know emails go to the logs on the appengine development server. However, the primary reason for wanting to send emails from the development server is to be able to test the format of the email. How does it look? Do changes need to be made to the email template so it looks good in email clients A, B, and C, and can it be done quickly without the hassle of deploying to a real, default appengine version each and every time.

We're not spammers. We're not trying to circumvent any type of security. In short, we want to legitimately be able to see the real, actual email in one or more email clients and then make code changes instantly so we can tweak them without having to go through the painstaking process of the edit, compile, wait 5 minutes for it to deploy, test, repeat cycle. Since there are no standards in how each email client renders an email, this painstaking process is amplified by trying to get something to work in many clients.

Question:

How can the Java Google App Engine Development server be configured to send emails from the local computer or an SMTP service for the purpose of testing emails sent to real, actual email clients?

like image 466
akula1001 Avatar asked Jun 21 '10 05:06

akula1001


3 Answers

From Eclipse, select the Run menu, Debug Configurations..., and then select your app’s configuration. Select the Arguments tab, then in the “VM arguments” section, set VM properties like this:

-Dmail.log_mail_level=WARNING -Dmail.log_mail_body=true

like image 130
zawhtut Avatar answered Nov 10 '22 20:11

zawhtut


When I worked with an e-mail service implementation I used a cool hint. So if you use MimeMessage too, and want just check if the message is formatted as expected, checking if attachments are there, HTML is well formatted, images are right referenced and so on, you could build the entire message, and during debug you could have some code like this:

MimeMessage msg = new MimeMessage(session);
...
if ("1".equals(System.getProperty("mail.debug"))) {
    msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
}

Every time this is executed the MimeMessage instane will be saved to emailSent.eml. This file you can open with your e-mail reader and check if everything is fine.

Of course that you need to execute your application with -Dmail.debug=1 parameter.

An example with attached file, text message and html message with this approach could be like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class MimeMessageTest {

    @Test
    public void tesstMimeMessage() throws MessagingException, FileNotFoundException, IOException {
        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("[email protected]", "Foo Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]", "Baz User"));
        msg.setSubject("Subject from admin e-mail to baz user");

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText("test message and so on");
        mbp1.setContent("<h1>test message and so on in HTML</h1>", "text/html");

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource("/tmp/fileToBeAttached");
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        if ("1".equals(System.getProperty("debug"))) {
            msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
        }
    }
}
like image 6
Francisco Spaeth Avatar answered Nov 10 '22 20:11

Francisco Spaeth


From 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.

So just check the logs when you intend to send mail, and make sure that it shows up there. No real mail will actually get sent.

like image 3
Jason Hall Avatar answered Nov 10 '22 21:11

Jason Hall