22 July, 2011

Mail API

Hello Java Folks,

We are using mail sending functionlaity in most of the web applications.
So have a look on below java class which can help you to send the mail using JAVA API.

package com.myspace.utility;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {

public static void sendEmailToEmployees() throws Exception {

String to = "vachhanijr@gmail.com";
String from = "jignesh.vachhani@gmail.com";
String host = "gmail-smtp.l.google.com";
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

// Create properties, get Session
Properties props = new Properties();

// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// To see what is going on behind the scene
props.put("mail.debug", "true");

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

// Instantiatee a message
Message msg = new MimeMessage(session);

// Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());

// Set message content
msg.setContent("lt;h1gt;This is a test lt;/h1gt;"
+ "lt; img src=\"http://www.rgagnon.com/images/jht.gif\"gt;",

// This HTML mail have to 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "lt;H1gt;Hello this is test mail with imagelt;/H1gt;lt;img src=\"cid:image\">gt;";
messageBodyPart.setContent(htmlText, "text/html");

// add it

// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource
("E:\\jignesh photo\\FAMILY\\jig%20copy.JPG");
messageBodyPart.setDataHandler(new DataHandler(fds));

// add it

// put everything together
// Send the message
System.out.println("preparing to send
} catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
// mex.printStackTrace();
public static void main(String arg[]) throws Exception {
class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "jignesh.vachhani@gmail.com";
String password = "********";
return new PasswordAuthentication(username, password);

Rate Me:

1 comment:

  1. hi all www.liferaysolution.com owner discovered your website via search engine but it was hard to find and I see you could have more visitors because there are not so many comments yet. I have discovered site which offer to dramatically increase traffic to your blog http://xrumerservice.org they claim they managed to get close to 1000 visitors/day using their services you could also get lot more targeted traffic from search engines as you have now. I used their services and got significantly more visitors to my blog. Hope this helps :) They offer best backlinks Take care. Jason


Your Comments on this post

Re:(4) Do you want a million for the New Year?

To unsubscribe, send us an email with the subject 'unsubscribe' qiWv qiWv Do you want a million for the New Year? qiWv qiWv ...