Sending HTML emails with attachments via SMTP and Java mail API.

Published on December 9, 2010 by Amir Sedighi

The code is clear enough and you won’t need any explanation…

package mail.send.datis.com;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

public class SMTPClient {

    private final String _CONTENT_HTML = "text/html";
    private final String _CONTENT_HOST = "mail.datispars.com";
    private final String _PORT = "25";
    private final String _USERNAME = "amir@datispars.com";
    private final String _PASSWORD = "your_password";

	public static void main(String[] args) throws MessagingException {
        SMTPClient sender = new SMTPClient();

       // sending mail, without attachment.
        sender.send("amir@datispars.com","kamran@datak.net", "Hi Kamran, how is going?" , "

this is a h1 tag so, it may appears in huge size! :D

"); // sending another mail, with 2 attachments. ArrayList fileNames = new ArrayList(); fileNames.add("/home/amir/Documents/hi_world.pdf"); fileNames.add("/home/amir/projects/datis/sendSMTP.zip"); sender.send("amir@datispars.com","kamran@datak.net", "Hi Kamran, how is going?" , "

this is a h1 tag so, it may appears in huge size! :D

", fileNames); } private void send(String from, String to, String subject, String body) throws MessagingException { Message message = new MimeMessage(getSession()); message.addRecipient(RecipientType.TO, new InternetAddress(to)); message.addFrom(new InternetAddress[] { new InternetAddress(from) }); message.setSubject(subject); message.setContent(body, _CONTENT_HTML); Transport.send(message); } private void send(String from, String to, String subject, String body, ArrayList attachments) throws MessagingException { Message message = new MimeMessage(getSession()); message.addRecipient(RecipientType.TO, new InternetAddress(to)); message.addFrom(new InternetAddress[] { new InternetAddress(from) }); message.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(body); mbp1.setContent(body, _CONTENT_HTML); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); for (String fileName: attachments){ MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileName); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(fds.getName()); mp.addBodyPart(mbp); } message.setContent(mp); message.setSentDate(new Date()); Transport.send(message); } private Session getSession() { Authenticator authenticator = new Authenticator(); Properties properties = new Properties(); properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.host", _CONTENT_HOST); properties.setProperty("mail.smtp.port", _PORT); return Session.getInstance(properties, authenticator); } private class Authenticator extends javax.mail.Authenticator { private PasswordAuthentication authentication; public Authenticator() { authentication = new PasswordAuthentication(_USERNAME, _PASSWORD); } protected PasswordAuthentication getPasswordAuthentication() { return authentication; } } }

Posted in Java

Leave a Reply

Categories

Archives