Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Sending Email through GMail

Posted on 13/12/2009

In my last 2 posts I have shown you how to send SMTP email using Java.
But I still haven’t shown how to send an Email through a secured SMTP server.

To do so I’ll show you how to use GMail secured SMTP server to send Emails.

Take a look at this class:


	public GMail() {
		try {
			String from = "everyDayDeveloper";
			String to = "aviyehuda@gmail.com";
			String body = "bla bla ...";
			String subject = "gmail"; 
			String senderEmail = "aaa@gmail.com";
			String senderPassword = "myPassword";
			
			MimeMessage message = new MimeMessage(getGmailSession());
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, 
			  new InternetAddress(to));
			message.setSubject(subject);
			message.setText(body);
	
			Transport.send(message);
		} catch (Exception e){
			e.printStackTrace();
		}
		
	}
	
	public static Session getGmailSession()	{
			Properties props = System.getProperties();
			props.put("mail.smtp.host", "smtp.gmail.com");
	        props.put("mail.smtp.auth", "true");
	        props.put("mail.debug", "true");
	        props.put("mail.smtp.port ", "465");
	        props.put("mail.smtp.socketFactory.port", "465");
	        props.put("mail.smtp.socketFactory.class",
                              "javax.net.ssl.SSLSocketFactory");
	        props.put("mail.smtp.socketFactory.fallback", "false");
	        Session session = 
                    Session.getDefaultInstance(props, 
                               new javax.mail.Authenticator() {
	 protected PasswordAuthentication getPasswordAuthentication() {
	    return new PasswordAuthentication(senderEmail, senderPassword);
	             }
	        }
	        );
	        
	        return session;
	}

The basic is the same, like sending a simple SMTP message.

What’s different here is the creation of the Session object in the function getGmailSession().

The Session object is created with much more properties and with the Authenticator which holds the authentication properties.

You will also need the mail.jar that can be found here.

downloaddownload source

Leave a Reply Cancel reply

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


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
d8252545e156cc094d04b47597b4fad1-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme