Dummy SMTP Server Send Email Java

Dummy SMTP Server Send Email Java

Sending email from java is an easy task. In development environment usually company blocks use of SMTP server from development environment. You could use only authorized box to send and receive an email. In this situation unit testing of our email program is difficult task. Dummy SMTP server is very suitable in this scenario that can be use to test email functionality without using any authorized SMTP server. I did test on many dummy SMTP server and found FAKESMTP is right choice to use an dummy SMTP server and it’s easy to use.

  • Download FAKESMTP server from here
  • Once download is completed unzip the file. You will see fakeSMTP-xxx.jar (In my case it is fakeSMTP-1.13.jar) got extracted. Because it’s an executable jar file, so that you can double click to open it. You will see as below, click start to start the server.

Dummy SMTP Server Send Email Java
Dummy SMTP Server Send Email Java

  • As you see FAKESMTP server started on port number 25 on localhost (You can also change the port if you want). Use below java program to send and test email:

 SendEmailJavaFakeSMTP.java

package com.javahonk;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmailJavaFakeSMTP {
	
	public static void main(String[] args) {
		sendEmailToGmailAccount();
	}

	private static void sendEmailToGmailAccount() {
		
		try {

			InternetAddress[] distributionList = InternetAddress.parse("javahonk@gmail.com,javahonk@javahonk.com",false);
			String from = "javahonk@gmail.com";
			String subject = "Test email";

			Properties props = new Properties();
			props.put("mail.smtp.host", "localhost");
			props.put("mail.smtp.port", "25");
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(false);
			
			Message msg = new MimeMessage(session);
			String message = "<div style=\"color:red;\">Email with static template</br></br><table border=\"1\" style=\"width:20%\"><tr><td>Email</td><td>Test</td><td>50</td></tr><tr><td>Email</td><td>Test</td><td>50</td></tr><tr><td>Email</td><td>Test</td><td>50</td></tr></table></div>";
			msg.setContent(message, "text/html; charset=utf-8");
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, distributionList);
			msg.setSubject(subject);
			msg.setSentDate(new Date());
			Transport.send(msg);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	

}
  • Output:

Dummy SMTP Server Send Email JavaDummy SMTP Server Send Email Java

download Download fakeSMTP-latest if you don’t find it.

Leave a Reply

Your email address will not be published. Required fields are marked *