java email coding


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sendmail 
{
  public static void main(String[] args)
{

String from="Sender Emailid here";      // Sender Emailid here Ex:mysample@gmail.com 
String to="Receiver Emailid here";                           //Receiver Emailid here 
String SMTP_AUTH_PWD="";                                  //Your Gmail Password
String fileAttachment="Specify File Path here";       //Specify File Path here  
String text="hi";                                                       //Text here
System.out.println(fileAttachment);
System.out.println(from);
System.out.println(to);
try
{
String subject="hi";                                         //Subject here
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
String SMTP_HOST_NAME = "smtp.gmail.com";
int SMTP_HOST_PORT = 465;
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
//Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("", "text/plain");
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, from, SMTP_AUTH_PWD);
// Get a mail session
// Define a new mail message
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(text);
//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
// addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
// Transport.send(message);

}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
}
Required Jar Files:
  1. activation.jar
  2. concurrent-1.3.2.jar
  3. log4j-1.2.15.jar
  4. mailapi-1.4.4.jar
  5. simple-java-mail-2.0.jar
  6. smtp-1.4.4.jar

1 comments:

  1. Anonymous said...:

    nice

Post a Comment

 
java4practices © 2013 | Designed by Ganesh Rengarajan