Click here to Skip to main content
13,558,199 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a requirement like "I need to fetch the details from database and send every record as a mail to the gmail account"please help me in doing this

What I have tried:

I tried below code.please help me in getting the results
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
public class MyOwn {
	   Outgoing Mail (SMTP) Server
	   requires TLS or SSL: (use authentication)
	   Use Authentication: Yes
	   Port for TLS/STARTTLS: 587
	public static void main(String[] args) {
		final String fromEmail = "****"; //requires valid gmail id
		final String password = "****"; // correct password for gmail id
		final String toEmail = "****"; // can be any email id 
		System.out.println("TLSEmail Start");
		Properties props = new Properties();
		props.put("", ""); //SMTP Host
		props.put("mail.smtp.port", "465"); //TLS Port
		props.put("mail.smtp.auth", "true"); //enable authentication
		props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
             //create Authenticator object to pass in Session.getInstance argument
		Authenticator auth = new Authenticator() {
			//override the getPasswordAuthentication method
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(fromEmail, password);
		Session session = Session.getInstance(props, auth);
		EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");

Posted 17-May-18 22:49pm
Updated 17-May-18 22:56pm
Thaddeus Jones 18-May-18 4:56am
I've edited your question to remove your e-mail address and password. I would recommend changing it just in case.
18-May-18 5:04am
Thank you Jones
Richard MacCutchan 18-May-18 6:18am
What is the question?
18-May-18 6:23am
I have a requirement like "I need to fetch the details from database and send every record as a mail to the gmail account"please help me in doing this
Richard MacCutchan 18-May-18 6:31am
Well that is the third time you have said that, but we still have no idea what your problem is.
18-May-18 6:42am
Actually by using the above code,when I run it using Eclipse Iam successfully getting the printed lines,but iam not receiving the mail.

Below is the code for EmailUtil

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

public class EmailUtil {

* Utility method to send simple HTML email
* @param session
* @param toEmail
* @param subject
* @param body
public static void sendEmail(Session session, String toEmail, String subject, String body){
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");

msg.setFrom(new InternetAddress("***", "NoReply-JD"));

msg.setReplyTo(InternetAddress.parse("****", false));

msg.setSubject(subject, "UTF-8");

msg.setText(body, "UTF-8");

msg.setSentDate(new Date());

System.out.println("Try Successfully!!");

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");

System.out.println("EMail Sent Successfully!!");
catch (Exception e) {
Richard MacCutchan 18-May-18 6:51am
Sorry, but it is impossible to guess what happens to the mail, or even if it gets sent. You need to gather more information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100