Sending Email using Java[TM] Mail API

“can somebody help me to send email using java?”

a year ago, i had a project which requires mail functionality. the most required function was to send email. then we decided to design a class which exploit Java Mail API to fulfill the requirement.
actually we designed only one class, but for improvement, i break it into two classes, these are the classes and the codes:
MailSender

package javamail;

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

public class MailSender {

 private String SMTPHost;
 private String SMTPPort;
 private String username;
 private String password;

 public MailSender() {
 SMTPHost = "";
 SMTPPort = "";
 username = "";
 password = "";
 }

 public MailSender(String _SMTPHost, String _SMTPPort,
 String _username, String _password) {
 SMTPHost = _SMTPHost;
 SMTPPort = _SMTPPort;
 username = _username;
 password = _password;
 }

 public void setPassword(String _password) {
 password = _password;
 }

 public void setUsername(String _username) {
 username = _username;
 }

 public String getSMTPHost() {
 return SMTPHost;
 }

 public void setSMTPHost(String _SMTPHost) {
 SMTPHost = _SMTPHost;
 }

 public String getSMTPPort() {
 return SMTPPort;
 }

 public void setSMTPPort(String _SMTPPort) {
 SMTPPort = _SMTPPort;
 }

 public void sendMail(List<String> _to, List<String> _toCC,
 List<String> _toBCC, String _subject, String _content)
 throws Exception {
 if (IMAPAuthenticator.getIMAPAuthentication(SMTPHost, username, password)) {
 Properties props = System.getProperties();

 props.put("mail.smtp.host", SMTPHost);
 props.put("mail.debug", "true");
 props.put("mail.smtp.port", "25");
 props.put("mail.smtp.localhost", SMTPPort);
 props.put("mail.smtp.auth", "false");

 Session session = Session.getInstance(props);
 Message message = new MimeMessage(session);

 try {
 message.setFrom(new InternetAddress(username +
 "@" + SMTPHost));

 for (int counter = 0; counter < _to.size(); ++counter) {
 message.setRecipient(Message.RecipientType.TO, new InternetAddress(_to.get(counter)));
 }

 if (_toCC != null) {
 for (int counterCC = 0; counterCC < _toCC.size(); ++counterCC) {
 message.setRecipient(Message.RecipientType.CC, new InternetAddress(_toCC.get(counterCC)));
 }
 }

 if (_toBCC != null) {
 for (int counterBCC = 0; counterBCC < _toBCC.size(); ++counterBCC) {
 message.setRecipient(Message.RecipientType.BCC, new InternetAddress(_toBCC.get(counterBCC)));
 }
 }

 message.setSubject(_subject);
 message.setText(_content);
 message.setSentDate(new Date());

 Transport.send(message);

 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }
 } else {
 throw new AuthenticationFailedException("invalid user");
 }
 }
}

IMAPAuthenticator

package javamail;

import com.sun.mail.imap.IMAPStore;
import java.util.Properties;
import javax.mail.Session;

public class IMAPAuthenticator {

 public static boolean getIMAPAuthentication(String _IMAPHost,
 String _username, String _password) throws Exception {
 boolean isAuthenticated = false;

 try {
 Properties properties = System.getProperties();
 Session session = Session.getDefaultInstance(properties);
 IMAPStore store = (IMAPStore) session.getStore("imap");

 store.connect(_IMAPHost, _username, _password);
 store.close();
 isAuthenticated = true;

 } catch (Exception ex) {
 ex.printStackTrace();
 isAuthenticated = false;
 }

 return isAuthenticated;
 }
}

to test it, write this code in your Main class then run it.

package javamail;

import java.util.ArrayList;

public class Main {

 public static void main(String[] args) throws Exception {
 MailSender ms = new MailSender();
 ms.setUsername("myusername");
 ms.setPassword("myPASSwORD");
 ms.setSMTPHost("mail.myhosting.ac.id");
 ms.setSMTPPort("25");

 ArrayList<String> to = new ArrayList();
 ArrayList<String> toCC = new ArrayList();
 String subject = "[JavaMail]: subject";
 String content = "[JavaMail]: content";
 to.add("mr@mail.hishost.com");
 toCC.add("ms@herhost.co.id");
 toCC.add("bthr@mail.somewhere.net");

 ms.sendMail(to, toCC, toCC, subject, content);
 }
}

check your inbox and have a good day…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s