Saturday, May 2, 2009

Java code to send mail form your Gmail account

hi all, writing a code for sending mails from your GMail account ! Does it fills exiting, ya so here you have the code to send mail using your GMail account. but for this you need to have to do some basic thing as follows

  • You should have your gmail account and internet
  • you should activate your IMAP access
  • copy the code given below and run the code entering your user id and password
How to activate your imap access??

you can activiate your imap access by
  1. log into your gmail account
  2. click on settings
  3. go to Forwarding and POP/IMAP tab
  4. click on Enable IMAP option in IMAP Access: option
  5. this will enable your IMAP access.
Here is the code to send mail from your GMmail account

click here to download code


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class mail

{
public synchronized static boolean sendMail(
String[] to,
String[] cc,
String[] bcc,
String subject,String text){
boolean target;
String host="smtp.gmail.com";
String port="465";
String starttls= "true";
String auth="true";
String socketFactoryClass="javax.net.ssl.SSLSocketFactory";
String fallback="false";
boolean debug=true;
String userName="abcd@gmail.com";//your user id
String passWord="password";//Your password
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("abce@gmail.com"));
for(int i=0;i
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
for(int i=0;i
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
}
for(int i=0;i
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
target= true;
} catch (Exception mex) {
target= false;
}
return target;
}
}


call this method by follwing code

mail.sendMail(to,cc,bcc,subject,body);

to,cc & bcc are the array of receipiants and subject and body are of data type String .