How to send email in java using gmail smtp server

How to send email in java using gmail smtp server

Use this code which runs smoothly on your IDE

===========================================================

package Demo;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MainA
{
public static void main(String[] args) throws Exception {
String[] to = { “xxxxxxasad@gmail.com” };
String[] cc = { “yyyyy.shetti@gmail.com” };
String[] bcc ={ “shiva.uuuuu@gmail.com” };
// This is for google
MainA.sendMail(“sjsjsjsj@gmail.com”, “*******”, “smtp.gmail.com”, “25”, “true”, “true”,
true, “javax.net.ssl.SSLSocketFactory”, “false”, to, cc, bcc,
“hi baba don’t send virus mails..”,
“This is my style…of reply..If u send virus mails..”);
}

public synchronized static boolean sendMail(String userName,
String passWord, String host, String port, String starttls,
String auth, boolean debug, String socketFactoryClass,
String fallback, String[] to, String[] cc, String[] bcc,
String subject, String text) {
Properties props = new Properties();
props.put(“mail.smtp.user”, userName);
props.put(“mail.smtp.host”, host);
props.put(“port”, port);
props.put(“mail.smtp.starttls.enable”, starttls);
props.put(“mail.smtp.auth”, auth);
props.put(“mail.smtp.ssl.trust”, “smtp.gmail.com”);
if (debug) {
props.put(“mail.smtp.debug”, “true”);
} else {
props.put(“mail.smtp.debug”, “false”);
}
try {
Session smtpSession = Session.getDefaultInstance(props, null);
smtpSession.setDebug(true);
MimeMessage msg = new MimeMessage(smtpSession);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(“sjsjsjsj@gmail.com”));
for (int i = 0; i < to.length; i++) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
to[i]));
}
for (int i = 0; i < cc.length; i++) {
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(
cc[i]));
}
for (int i = 0; i < bcc.length; i++) {
msg.addRecipient(Message.RecipientType.BCC,
new InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = smtpSession.getTransport(“smtp”);
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
} catch (Exception mex) {
mex.printStackTrace();
System.out.println(mex.getLocalizedMessage());
return false;
}
}
}

============================================================

NOTE : Before execution STEPS to be followed

1.Go to This link https://www.google.com/settings/security/lesssecureapps

2.Login to your gmail

3.Access for less secure apps select “Turn on”

4.Run the code in IDE

 

=============================================================

Output Console:

DEBUG: setDebug: JavaMail version 1.4.3
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host “smtp.gmail.com”, port 25, isSSL false
220 smtp.gmail.com ESMTP m2sm14303859pdr.64 – gsmtp
DEBUG SMTP: connected to host “smtp.gmail.com”, port: 25

EHLO SP-PC
250-smtp.gmail.com at your service, [123.136.219.84]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension “SIZE”, arg “35882577”
DEBUG SMTP: Found extension “8BITMIME”, arg “”
DEBUG SMTP: Found extension “STARTTLS”, arg “”
DEBUG SMTP: Found extension “ENHANCEDSTATUSCODES”, arg “”
DEBUG SMTP: Found extension “PIPELINING”, arg “”
DEBUG SMTP: Found extension “SMTPUTF8”, arg “”
STARTTLS
220 2.0.0 Ready to start TLS
EHLO SP-PC
250-smtp.gmail.com at your service, [123.136.219.84]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension “SIZE”, arg “35882577”
DEBUG SMTP: Found extension “8BITMIME”, arg “”
DEBUG SMTP: Found extension “AUTH”, arg “LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH”
DEBUG SMTP: Found extension “ENHANCEDSTATUSCODES”, arg “”
DEBUG SMTP: Found extension “PIPELINING”, arg “”
DEBUG SMTP: Found extension “SMTPUTF8”, arg “”
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN
334 VXNlcm5hbWU6
c2hldHRpLnNoaXZhcHJhc2FkQGdtYWlsLmNvbQ==
334 UGFzc3dvcmQ6
eWFjb3lvZDc=
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<shetti.shivaprasad@gmail.com>
250 2.1.0 OK m2sm14303859pdr.64 – gsmtp
RCPT TO:<shetti.shivaprasad@gmail.com>
250 2.1.5 OK m2sm14303859pdr.64 – gsmtp
RCPT TO:<shiva.shetti@gmail.com>
250 2.1.5 OK m2sm14303859pdr.64 – gsmtp
RCPT TO:<shiva.shetti@gmail.com>
250 2.1.5 OK, duplicate recipients will be consolidated. m2sm14303859pdr.64 – gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: shetti.shivaprasad@gmail.com
DEBUG SMTP: shiva.shetti@gmail.com
DEBUG SMTP: shiva.shetti@gmail.com
DATA
354 Go ahead m2sm14303859pdr.64 – gsmtp
From: shetti.shivaprasad@gmail.com
To: shetti.shivaprasad@gmail.com
Cc: shiva.shetti@gmail.com
Message-ID: <4422554.0.1439058938277.JavaMail.SP@SP-PC>
Subject: =?Cp1252?Q?hi_baba_don=92t_send_virus_mails..?=
MIME-Version: 1.0
Content-Type: text/plain; charset=Cp1252
Content-Transfer-Encoding: quoted-printable

This is my style=85of reply..If u send virus mails..
.
250 2.0.0 OK 1439058958 m2sm14303859pdr.64 – gsmtp
QUIT
221 2.0.0 closing connection m2sm14303859pdr.64 – gsmtp

2 thoughts on “How to send email in java using gmail smtp server

  1. How to resolve duplicate recipients will be consolidated. ij6sm537806igb.1 – gsmtp issue while sending mail through gmail

Leave a Reply to assist you..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s