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 {

// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "vachhanijr@gmail.com";
String from = "jignesh.vachhani@gmail.com";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "gmail-smtp.l.google.com";
java.security.Security
.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;",
"text/html");

//
// 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
multipart.addBodyPart(messageBodyPart);

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

// add it
multipart.addBodyPart(messageBodyPart);

// put everything together
msg.setContent(multipart);
// Send the message
System.out.println("preparing to send
");
Transport.send(msg);
} catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
// mex.printStackTrace();
System.out.println(mex.toString());
}
}
public static void main(String arg[]) throws Exception {
sendEmailToEmployees();
}
}
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 Discussion:

Anonymous said...

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