ZOFTINO.COM android and web dev tutorials

Send Emails Using Google App Engine Email Service without Spam and Via

Emails are still a best way to communicate with users or customers to keep them up to date with news, offers, new features, products & services. Giving an option for users to enroll for news letters from the day one is a good marketing strategy for companies to accumulate list of email addresses which can be used to communicate news and offers with users.

There are several ways in which you can send emails in bulk. But in this article I am going to show how to send 100 free emails daily using Google App Engine and its free emails quota. You need to be familiar with Google App Engine in order for you to run the email program which I am going to show.

There are few important points related to email you need to take care of, which are general and not specific to Google App Engine email service, such as spam preventions, unsubscribe link, stopping display of via information.

How to prevent an email from ending up in SPAM folder

If your email ends up in spam folder of recipients, chances of opening the email by recipients are very slim. Emails go to spam when emails are sent in bulk. Meaning, all to-email addresses are listed in cc when emails are sent. To prevent your emails from going into spam folder, you need to send email to each email address separately. And also, if you use third party email service for sending emails, like Google App Engine email service, you need to add SPF record for your domain. Using SPF record, email receiver client such as gmail can verify that sender email server (third party email server) is allowed to send emails on behalf of the sender if SPF record for the sender domain has IP of sender email server.

How to stop email clients from displaying via information

Most of the email clients show via information for emails sent using third party email services to let email receiver client know that email has been sent using third party email service. If you don’t want this information to be read by your email receiver user, there is a way to suppress it. By adding SPF record and DKIM key to DNS records for your domain and signing the message with DKIM key. If you use Google apps, setting up of DKIM for your domain is very easy.

Below is the class which can be used to send emails on Google App Engine.


import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class AppEngineEmailSender {
	public void sendEmailFromName(String toEmail,String msgbody,String subject, String userIdentifer, String fromName, String fromEmail){
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null); 

		if(msgbody == null){
			return;
		}
		try {
			Message msg = new MimeMessage(session);            
			Multipart mp = new MimeMultipart();

			MimeBodyPart htmlPart = new MimeBodyPart();
			htmlPart.setContent(msgbody, "text/html");
			mp.addBodyPart(htmlPart);
			if(userIdentifer != null){
				msg.setHeader("List-Unsubscribe", "http://yourdomain.com/unsubsribe/"+userIdentifer);
			}
			msg.setSubject(subject);
			msg.setContent(mp);
			msg.setFrom(new InternetAddress(fromEmail, fromName));
			msg.addRecipient(Message.RecipientType.TO,
					new InternetAddress(toEmail, ""));     

			Transport.send(msg);

		} catch (AddressException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
} 

Map url mentioned in the cron to the action in controller like below.


	@RequestMapping("/email/daily-email")
	public String sendDialyEmail( ModelMap model) {
		AppEngineEmailSender emailSender = new AppEngineEmailSender();
		
		List<String> emailAddLst = getNextHunderEmailIds();		
		String emailMsg = getEmailMsg("dailyemail.html");
		
		for(String emailAddress :  emailAddLst){
			String userIdentifer = getIdentifierByEmailId(emailAddress);
			emailSender.sendEmailFromName(emailAddress, emailMsg, "Email Subject", userIdentifer, "yourdomain.com", "newsletter@yourdomain.com");
		}
	}
 

Below is the cron configuration to run above program daily once


<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
   <cron>
    <url>/email/daily-email</url>
    <description>send email daily to 100 users</description>
    <schedule>every day 11:00</schedule>
    <target>myappbackend</target>
  </cron>
</cronentries>