Loading
View RSS Feed

Angad Kumar Shukla's blog

How To Send E-Mail Through Java Code?

Rating: 16 votes, 3.75 average.
How To Send E-Mail Through Java Code?

We all know that sending email from java code in real life projects is very integral part. We all need this added features for directly sending some important information directly to emails. So now we will show you the way.
We are selecting Gmail SMTP server. This means we will show the demonstration of how to send emails from a Gmail Id to any other Email ids through Java code.
Note:
1. We need some external jar files namely: smtp , mailapi, mail, smtp , dsn and imap. These jar files should be in our Tomcat server lib folder.
2.We can also directly add these external jar files while executing our code in Eclipse software.
Code:
package JavaMi;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*; 

public class JavaMailEx {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws MessagingException {
		// TODO Auto-generated method stub
		// Recipient's email ID needs to be mentioned.
			String sender_email = "sender_id@gmail.com",
			sender_password = "password",
			sender_host = "smtp.gmail.com",
			sender_port = "465",
			reciever_id = "recipient_id@abc.com",
			subject_to_be_given = "TF4U",
			message_mi = "Welcome to TF4U.";

			Properties props = new Properties();
			 props.put("mail.smtp.user", sender_email);
			 props.put("mail.smtp.host", sender_host);
			 props.put("mail.smtp.port", sender_port);
			 props.put("mail.smtp.starttls.enable","true");
			 props.put("mail.smtp.auth", "true");
			 //props.put("mail.smtp.debug", "true");
			 props.put("mail.smtp.socketFactory.port",sender_port);
			 props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			 props.put("mail.smtp.socketFactory.fallb","false");

			 SecurityManager security = System.getSecurityManager();

			 try
			 {
			 Authenticator auth = new SMTPAuthenticator();
			 Session session = Session.getInstance(props, auth);
			 //session.setDebug(true);

			 MimeMessage msg = new MimeMessage(session);
			 msg.setText(message_mi);
			 msg.setSubject(subject_to_be_given);
			 msg.setFrom(new InternetAddress(sender_email));
			 msg.addRecipient(Message.RecipientType.TO,new InternetAddress(reciever_id);
			 Transport.send(msg);
			 }
			 catch (Exception mex)
			 {
				 mex.printStackTrace();
			 } 

	}

}
class SMTPAuthenticator extends javax.mail.Authenticator
{
	String sender_email = "sender_id@gmail.com",
			sender_password = "password",
			sender_host = "smtp.gmail.com",
			sender_port = "465",
			reciever_id = "angad.shukla2929@gmail.com",
			subject_to_be_given = "TF4U",
			message_mi = "Welcome to TF4U.";
	public PasswordAuthentication getPasswordAuthentication()
	{
			return new PasswordAuthentication(sender_email, sender_password);
	}
}

Submit "How To Send E-Mail Through Java Code?" to Digg Submit "How To Send E-Mail Through Java Code?" to del.icio.us Submit "How To Send E-Mail Through Java Code?" to StumbleUpon Submit "How To Send E-Mail Through Java Code?" to Google

Updated 07-21-2012 at 05:55 AM by angad

Categories
Java , Web Design

Comments

  1. sampath palreddy's Avatar
    is it work for mini project sir...???
  2. angad's Avatar
    this will be helpful in your case study also dear.
  3. mandurikarthik's Avatar
    Thank you so much Angad.



Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.