To send email from gmail from java code,i wrote this sample program. This will help you use gmail in your automation task.
Sending email from gmail smtp server is almost same as normal smtp server. still it is sometimes it takes more time then general server as it need some extra authentication.
Here is the example to send automated mail from JAVA code via gmail.
Suggestions are invited to improve this code.


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage;

public class GmailMailer {

	/**
	 * 
	 * @param from
	 * @param to
	 * @param cc
	 * @param bcc
	 * @param subject
	 * @param message
	 */
	public void sendMail(String from, String to, String cc, String bcc,
			String subject, String message) {

		try {
			Session session = getSession();
			Message mailMessage = new MimeMessage(session);
			mailMessage.setFrom(new InternetAddress(from));

			// Set the mail recipients
			setRecipients(mailMessage, to, cc, bcc);

			mailMessage.setSubject(subject);

			mailMessage.setSentDate(new Date());
			mailMessage.setText(message);
			mailMessage.saveChanges();
			Transport.send(mailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Gets the session.
	 * 
	 * @return the session
	 */
	private Session getSession() {

		String host = "74.125.25.109";// set ip address (server ip for smtp.gmail.com)
		final String userName = "username";// put username here. 
		final String password = "password";// put pwd here
		final String smtpPort = "465"; // put port number.
		final String sslFactory = "javax.net.ssl.SSLSocketFactory";

		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", smtpPort);
		props.put("mail.smtp.socketFactory.port", smtpPort);
		props.put("mail.smtp.socketFactory.class", sslFactory);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getInstance(props, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		});

		return session;
	}

	/**
	 * Sets the recipients.
	 * 
	 * @param mailMessage
	 *            the mail message
	 * @param to
	 *            the to
	 * @param cc
	 *            the cc
	 * @param bcc
	 *            the bcc
	 * @throws AgMailException
	 *             the ag mail exception
	 */
	private void setRecipients(Message mailMessage, String to, String cc,
			String bcc) throws Exception {

		if (mailMessage != null) {
			try {
				if (to != null) {
					List toList = new ArrayList();
					StringTokenizer tokenizer = new StringTokenizer(to, ",");
					while (tokenizer.hasMoreTokens()) {
						toList.add(new InternetAddress(tokenizer.nextToken()));
					}
					mailMessage.setRecipients(Message.RecipientType.TO,
							(InternetAddress[]) toList
									.toArray(new InternetAddress[0]));
				}
				// set CC recipients
				if (cc != null) {
					List ccList = new ArrayList();
					StringTokenizer tokenizer = new StringTokenizer(cc, ",");
					while (tokenizer.hasMoreTokens()) {
						ccList.add(new InternetAddress(tokenizer.nextToken()));
					}
					mailMessage.setRecipients(Message.RecipientType.CC,
							(InternetAddress[]) ccList
									.toArray(new InternetAddress[0]));
				}

				// set BCC recipients
				if (bcc != null) {
					List bccList = new ArrayList();
					StringTokenizer tokenizer = new StringTokenizer(bcc, ",");
					while (tokenizer.hasMoreTokens()) {
						bccList.add(new InternetAddress(tokenizer.nextToken()));
					}
					mailMessage.setRecipients(Message.RecipientType.BCC,
							(InternetAddress[]) bccList
									.toArray(new InternetAddress[0]));
				}
			} catch (MessagingException e) {
			} catch (Exception e) {
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		//code
		GmailMailer mail = new GmailMailer();
		// cc & bcc are comma separated
		mail.sendMail("morwal89@gmail.com", "morwal89@gmail.com", "", "",
				"sec mail", "hi, this is my first mail ");
		long endTime = System.currentTimeMillis();
		System.out.println("Took "+(endTime - startTime)/1000 + " sec");// i added this line to test performance. 

	}

}
The following two tabs change content below.
He is founder and CTO of Habilelabs.io. He senior software architect. He is guru of various Mobile and web technologies including Node.js, Angular.js, Meteor.js. He is founder member of habilelabs Pvt. Ltd.
  • Mailer using thread

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Properties;
    import java.util.StringTokenizer;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class GmailRunnable implements Runnable {
    	private String to = "";
    	private String cc = "";
    	private String bcc = "";
    	private String subject = "";
    	private String message = "";
    	
    
    	@Override
    	public void run() {
    		this.sendMail();
    		
    	}
    	public void setParameters(String to, String cc, String bcc, String subject,
    			String message) {
    		this.to = to;
    		this.cc = cc;
    		this.bcc = bcc;
    		this.subject = subject;
    		this.message = message;
    
    	}
    	private void sendMail() {
    
    		try {
    			Session session = getSession();
    			Message mailMessage = new MimeMessage(session);
    	//		mailMessage.setFrom(new InternetAddress(from));
    
    			// Set the mail recipients
    			setRecipients(mailMessage, to, cc, bcc);
    
    			mailMessage.setSubject(subject);
    
    			mailMessage.setSentDate(new Date());
    			mailMessage.setText(message);
    			mailMessage.saveChanges();
    			Transport.send(mailMessage);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
    	private Session getSession() {
    
    		String host = "74.125.25.109";// set ip address (server ip for
    										// smtp.gmail.com)
    		final String userName = "username";// put username here. dont put
    											// @domain.com only username.
    		final String password = "password";// put pwd here
    		final String smtpPort = "465"; // put port number.
    		final String sslFactory = "javax.net.ssl.SSLSocketFactory";
    
    		Properties props = System.getProperties();
    		props.put("mail.smtp.host", host);
    		props.put("mail.smtp.auth", "true");
    		props.put("mail.debug", "true");
    		props.put("mail.smtp.port", smtpPort);
    		props.put("mail.smtp.socketFactory.port", smtpPort);
    		props.put("mail.smtp.socketFactory.class", sslFactory);
    		props.put("mail.smtp.socketFactory.fallback", "false");
    
    		Session session = Session.getInstance(props, new Authenticator() {
    			public PasswordAuthentication getPasswordAuthentication() {
    				return new PasswordAuthentication(userName, password);
    			}
    		});
    
    		return session;
    	}
    	private void setRecipients(Message mailMessage, String to, String cc,
    			String bcc) throws Exception {
    
    		if (mailMessage != null) {
    			try {
    				if (to != null) {
    					List toList = new ArrayList();
    					StringTokenizer tokenizer = new StringTokenizer(to, ",");
    					while (tokenizer.hasMoreTokens()) {
    						toList.add(new InternetAddress(tokenizer.nextToken()));
    					}
    					mailMessage.setRecipients(Message.RecipientType.TO,
    							(InternetAddress[]) toList
    									.toArray(new InternetAddress[0]));
    				}
    				// set CC recipients
    				if (cc != null) {
    					List ccList = new ArrayList();
    					StringTokenizer tokenizer = new StringTokenizer(cc, ",");
    					while (tokenizer.hasMoreTokens()) {
    						ccList.add(new InternetAddress(tokenizer.nextToken()));
    					}
    					mailMessage.setRecipients(Message.RecipientType.CC,
    							(InternetAddress[]) ccList
    									.toArray(new InternetAddress[0]));
    				}
    
    				// set BCC recipients
    				if (bcc != null) {
    					List bccList = new ArrayList();
    					StringTokenizer tokenizer = new StringTokenizer(bcc, ",");
    					while (tokenizer.hasMoreTokens()) {
    						bccList.add(new InternetAddress(tokenizer.nextToken()));
    					}
    					mailMessage.setRecipients(Message.RecipientType.BCC,
    							(InternetAddress[]) bccList
    									.toArray(new InternetAddress[0]));
    				}
    			} catch (MessagingException e) {
    			} catch (Exception e) {
    			}
    		}
    	}
    
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		long startTime = System.currentTimeMillis();
    		GmailRunnable gmailRunnable = new GmailRunnable();
    		gmailRunnable.setParameters("morwal89@gmail.com", "","","first call", "hi this is first call");
            Thread t = new Thread(gmailRunnable);
            t.start();
            long endTime = System.currentTimeMillis();
    		System.out.println("Took "+(endTime - startTime)/1000 + " sec");// i added this line to test performance. 
    
    	}
    
    }
    
  • you can fine this link to useful. http://www.morwalz.com/java-mail/

  • ACochrane

    Hi – had a look at your post but am getting a Connection Refused error. Any suggestions for rectifying this? Or even better can you recommend a good resource for building an email application in Java from the ground up?

    Thanks

    • .You have to use JAVA API. you can email application in Java from the ground up but thats too complicated. Build your own using JAVA API.

      Regarding this error please post your error in detail. This error is not expected.