Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

How to send email using java

In this program we are going to discuss about how to sent mail in java using javax.mail.
SOURCE : EmailSender.java

package com.ocg.post;

import java.io.File;
import java.util.Properties;

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;

public class EmailSender {
private String username = "";//use gamil username
private String password ="";//use gmail password
private String senderMailID = ""; //sender mail id
private String receiverMailID = "";// receiver mail id
public boolean sentMail(String subject, String pageContent, File fileName) {
boolean isMailSent = false;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderMailID));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(receiverMailID));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
message.setContent(pageContent, "text/html");
Transport.send(message);
isMailSent = true;
System.out.println("Mail sent successfully");
} catch (MessagingException e) {
isMailSent = false;
throw new RuntimeException(e);
}
return isMailSent;
}
}



OUTPUT

Mail sent successfully






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+





Sending email attachment using mail api with text content

Sending email attachment using mail api with text content
We are going learn about how to sent email with Image ATTACHMENT content.
The Steps are listed below
  • Create EmailAttachment and MultiPartEmailinstance.

  • Set the host name of the server smtp.googlemail.com

  • Set email account details .

  • Set email address for To and From.

  • Set Email subject and attachment of the Email.

  • Finally sending mail

SourceFile: TextEmailAttachment.java Output:
Email sent successfully 

Download:Sending email attachment using Gmail with text content

How to sent html content formatted email using Gmail SSL

How to sent html content formatted email using Gmail SSL
We are going learn about how to sent email with HTML content.
The Steps are listed below
  • Create HtmlEmail instance.

  • Set the host name of the server smtp.googlemail.com

  • Set email account details .

  • Set email address for To and From.

  • Set Email subject and HTML content of the Email.

  • Finally sending mail

SourceFile: HTMLEmailDemo.java Output:
Email sent successfully 

Download: How to sent html content formatted email using Gmail SSL

Introduction to Apache Commons Email API

In this article we are going learn about how compose and send emails using apache commons api in java.
Commons Email api is designed by apache used for sending email. It is designed on top of the Java Mail APIand simplifies email operations.
Some of the important classes are listed below :
SimpleEmail - It is used for sending basic text based emails.
MultiPartEmail- It is used to send multipart messages. Like text message with attachments either inline or attached.
HtmlEmail - It is used to send HTML formatted emails. It supports both MultiPartEmail allowing attachments and embedded images.
ImageHtmlEmail - It is used to send HTML formatted emails with images.
EmailAttachment - It is a container class used for easy handling of attachments.
It is for use with instances of MultiPartEmail and HtmlEmail.
For environment setup , first we need to download commons-email-1.3.2.jar and mail.jar file and set it in to class path.
Click here to download apache commons email jars.