// How to send an email by Java application using Gmail/ Yahoo/ Hotmail
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class sendmail {
public static void main(String[] args) {
try{
String host = "smtp.gmail.com";
String from = "from@gmail.com";
String pass = " njkgyddddshqd "; //if you are getting any error related to authentication use application specific password here.
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);
String[] to = {"to@gmail.com"}; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
/*********************************************/
if you are getting any type of error then read below carefully
My client isn't accepting my username and password
We
suggest making some adjustments in your mail client's settings so you
aren't always prompted to enter your username and password. Please check
the following settings in your mail client:
Note: use that application specific password at the place of original password of sender password.- Make sure that you've entered your full email address (e.g. username@gmail.com)
- Re-enter your password to ensure that it's correct. Keep in mind that passwords are case-sensitive.
- Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
- Open your web browser and sign in to Gmail at http://mail.google.com/mail. If you see a word verification request, type the letters in the distorted picture and finish signing in.
- Close your browser and try accessing your messages in your email client again.
- If you're still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.
- Click Continue.
- Restart your mail client and try accessing messages in your email client again.
read more on : http://support.google.com/mail/answer/14257