You can send email from your java application using Gmail SMTP Server.
Google Apps SMTP settings to send mail from a java app -
For more detail about Gmail SMTP server and sending limits please click on following linksSMTP Service - smtp.gmail.comPort - Port 465 (SSL required)- Port 587 (TLS required)Sending Limits - 2000 Messages per day.
Google Apps SMTP settings to send mail
Google Apps email sending limits
Example to send email from a java app -
Here is the example to send email from a java application.import java.util.Properties; 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 SendEmailUsingGmailSMTPServer { public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("userName@gmail.com", "password"); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("userName@gmail.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("userName@gmail.com")); message.setSubject("JavaMail API"); message.setText("JavaMail API - Send Email Using Gmail SMTP server"); Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } } }
Replace username and password with your gmail username and password.
Run this code as java application. After execution of code, a email will be send to recipient email address.
If email is not send and you get error javax.mail.AuthenticationFailedException than solve it using following steps
1. Check your user name and password again.
2. By default, login for less secure apps is turn off. You can turn On this from account setting
Click on this link security settings for less secure apps and turn ON the access of your account for less secure apps.
No comments:
Post a Comment