Source Code : Send & Receive emails through a GMail account using Java

Send & Receive emails through a GMail account using Java

Introduction
All of us have the experience of sending emails using web services such as Google mail,Yahoo mail, Hotmail, …etc. But here I’m going to mention how to send and read emails through a Java desktop application.

You can Send emails, Read emails, Send replies, Add attachments and many more through Java applications. But in here I’m going to mention first two only.

What you need
1. I’m going to do this using NetBeans. So NetBeans is essential.

2. You have to get JavaMail API. You can have it from here.

How to setup JavaMail API
Unzip the downloaded file. Open the NetBeans application. Open Project window and right click on the Libraries. Click on Add JAR/Folder . Select mail.jar file and add it.

How to do it
When you refer internet you can see common ways of sending mails. But there’re some special things to follow when dealing with GMail account. So here is the example that worked for me.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 import java.util.*;import javax.mail.Folder;import javax.mail.Message;import javax.mail.Message.RecipientType;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Store;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.swing.JOptionPane; /** * * @author BUDDHIMA */ public class GmailClient {  private String userName;  private String password;  private String sendingHost;  private int sendingPort;  private String from;  private String to;  private String subject;  private String text;  private String receivingHost;//  private int receivingPort;  public void setAccountDetails(String userName,String password){  this.userName=userName;//sender's email can also use as User Name  this.password=password;  }  public void sendGmail(String from, String to, String subject, String text){  // This will send mail from -->sender@gmail.com to -->receiver@gmail.com  this.from=from;  this.to=to;  this.subject=subject;  this.text=text;  // For a Gmail account--sending mails-- host and port shold be as follows  this.sendingHost="smtp.gmail.com";  this.sendingPort=465;  Properties props = new Properties();  props.put("mail.smtp.host", this.sendingHost);  props.put("mail.smtp.port", String.valueOf(this.sendingPort));  props.put("mail.smtp.user", this.userName);  props.put("mail.smtp.password", this.password);  props.put("mail.smtp.auth", "true");  Session session1 = Session.getDefaultInstance(props);  Message simpleMessage = new MimeMessage(session1);  //MIME stands for Multipurpose Internet Mail Extensions  InternetAddress fromAddress = null;  InternetAddress toAddress = null;  try {  fromAddress = new InternetAddress(this.from);  toAddress = new InternetAddress(this.to);  } catch (AddressException e) {  e.printStackTrace();  JOptionPane.showMessageDialog(null, "Sending email to: " + to + " failed !!!", "Falied to Send!!!", JOptionPane.ERROR_MESSAGE);  }  try {  simpleMessage.setFrom(fromAddress);  simpleMessage.setRecipient(RecipientType.TO, toAddress);  // to add CC or BCC use  // simpleMessage.setRecipient(RecipientType.CC, new InternetAddress("CC_Recipient@any_mail.com"));  // simpleMessage.setRecipient(RecipientType.BCC, new InternetAddress("CBC_Recipient@any_mail.com"));  simpleMessage.setSubject(this.subject);  simpleMessage.setText(this.text);  //sometimes Transport.send(simpleMessage); is used, but for gmail it's different  Transport transport = session1.getTransport("smtps");  transport.connect (this.sendingHost,sendingPort, this.userName, this.password);  transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());  transport.close();  JOptionPane.showMessageDialog(null, "Mail sent successfully ...","Mail sent",JOptionPane.PLAIN_MESSAGE);  } catch (MessagingException e) {  e.printStackTrace();  JOptionPane.showMessageDialog(null, "Sending email to: " + to + " failed !!!", "Falied to Send!!!", JOptionPane.ERROR_MESSAGE);  }  }  public void readGmail(){  /*this will print subject of all messages in the inbox of sender@gmail.com*/  this.receivingHost="imap.gmail.com";//for imap protocol  Properties props2=System.getProperties();  props2.setProperty("mail.store.protocol", "imaps");  // I used imaps protocol here  Session session2=Session.getDefaultInstance(props2, null);  try {  Store store=session2.getStore("imaps");  store.connect(this.receivingHost,this.userName, this.password);  Folder folder=store.getFolder("INBOX");//get inbox  folder.open(Folder.READ_ONLY);//open folder only to read  Message message[]=folder.getMessages();  for(int i=0;iWhat’s more
Using JavaMail API you can send/receive mails using other email services such as Yahoo, Hotmail, … etc. or your own mail server !

But you have to set the appropriate host and port numbers.

For setting up your own mail server use JAMES.

Note
I’ve found that there’s a special API only for GMail accounts (g4j). But in here I used the method which can be used with any other email services.

Resources
If you are more interested in this, following resources would be helpful.

1. http://www.roseindia.net/javamail/index.shtml

2. http://www.javabeat.net/tips/33-sending-mail-from-java.html

3. http://www.osix.net/modules/article/?id=39