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

Wednesday, 26 September 2012

Adding An Image Attachment To An Email In Java

This post describes how to add an image attachment to an email in Java. The code example is available from Github in the Java-Email-Sending directory. To make it work, you will need to set a proper 'to' email address in the code, together with a sending email server login and password if necessary.

Adding An Image Attachment

The following adds a text body to the email, together with image:
String to = "jverstry@gmail.com";
String from = "ffff@ooop.com";

// Which server is sending the email?
String host = "localhost";

// Setting sending mail server
Properties ps = System.getProperties();
ps.setProperty("mail.smtp.host", host);

// Providing email and password access to mail server
ps.setProperty("mail.user", "jverstry@gmail.com");
ps.setProperty("mail.password", "xxxxxxxxxxxxx");

// Retrieving the mail session
Session session = Session.getDefaultInstance(ps);

// Create a default MimeMessage
MimeMessage m = new MimeMessage(session);
m.setFrom(new InternetAddress(from));
m.addRecipient(
    Message.RecipientType.TO, new InternetAddress(to));
m.setSubject("This an email test !!!");

// Create a multipart message
Multipart mp = new MimeMultipart();

// Body text
BodyPart messageBP = new MimeBodyPart();
messageBP.setText("Some message body !!!");
mp.addBodyPart(messageBP);

// Attachment
BodyPart messageBP2 = new MimeBodyPart();
String image = "/MyImage.jpg";

InputStream is = EmailWithAttachment.class
    .getResourceAsStream(image);

DataSource source = new ByteArrayDataSource(
    IOUtils.toByteArray(is), "image/jpeg");

messageBP2.setDataHandler(new DataHandler(source));
mp.addBodyPart(messageBP2);
m.setContent(mp);

// Sending the message
Transport.send(m);
We use the Apache IOUtils tool to convert our image into a byte array.

The email will look like this:

Email With Image Attachment

Sending Plain Text Or HTML Emails In Java

This post describes how to send plain text or HTML emails from Java. The code examples are available from Github in the Java-Email-Sending directory. To make these work, you will need to set a proper 'to' email address in the code, and eventually a sending email server login and password.

Plain Text Email

The following creates a simple email with plain text content and sends it to the recipient:
String to = "jverstry@gmail.com";
String from = "ffff@ooop.com";

// Which server is sending the email?
String sender = "localhost";

// Setting sending mail server
Properties ps = System.getProperties();
ps.setProperty("mail.smtp.host", sender);

// Providing email and password access to mail server
ps.setProperty("mail.user", "jverstry@gmail.com");
ps.setProperty("mail.password", "xxxxxxxxxxxxx");

// Retrieving the mail session
Session session = Session.getDefaultInstance(ps);

// Create a default MimeMessage
MimeMessage m = new MimeMessage(session);

m.setFrom(new InternetAddress(from));
m.addRecipient(
    Message.RecipientType.TO, new InternetAddress(to));

m.setSubject("This an email test !!!");
m.setText("Some email message content");

// Sending the message
Transport.send(m);
The email will look like this:

Email with Plain Text Content

HTML Email

The same procedure can be used for an HTML email. The m.setText("Some email message content"); line should be replaced with:
StringBuilder htmlContent = new StringBuilder();
htmlContent.append("<h1>My Email Content Header</h1>");
htmlContent.append("Some body text");

m.setContent(htmlContent.toString(), "text/html" );
The email will look like this:

Email with HTML Content