wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes JavaMail - Problem while sending file attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail - Problem while sending file attachment" Watch "JavaMail - Problem while sending file attachment" New topic

JavaMail - Problem while sending file attachment

Aniruddha Biswas

Joined: Jun 13, 2010
Posts: 1
I am using java mail api to send mail from a standalone java application. The mail has one html file attachment. I am using the below java program.

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
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;
import javax.mail.internet.MimeMultipart;

public class AttachExample {
public static void main(String args[]) throws Exception {
String host = "smtp.gmail.com";
String from = "myuser@gmail.com";
String to = "anotheruser@gmail.com";
String fileAttachment = "javamail.html";

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");

Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
String username = "user@gmail.com";
String password = "password";
return new PasswordAuthentication(username, password);
// Get session
Session session = Session.getInstance(props, authenticator);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
new InternetAddress(from));
message.setSubject("Hello JavaMail Attachment");

// create the mail root multipart
MimeMultipart mpRoot = new MimeMultipart("mixed");

// Create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
// Create the content multipart (for text and HTML)
MimeMultipart mpContent = new MimeMultipart("alternative");
// Add text
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Hello World");

// Add html
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setContent("<P>Hello World</P>", "text/html");

// Add the root body part to the root multipart

// Add an attachment
MimeBodyPart mbp3 = new MimeBodyPart();
DataSource source = new FileDataSource("javamail.html");
mbp3.setDataHandler(new DataHandler(source));
mbp3.setHeader("Content-Type", "text/html");
// Send the message

In my web mail I am not able to find the attachment in inbox/sent item. However I can see the mail is present there.

Here is the mail....

<div class="ii gt">
Content-Type: multipart/alternative; boundary="----=_Part_1_14410104.1276373943174"

Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello World
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<P>Hello World</P>

Content-Type: text/html
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=javamail.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "<a href="http://www.w3/" target="_blank">http://www.w3</a>=
<meta http-equiv=3D"content-type" content=3D"text/html;
<<.....more html code....>>
=A9 Vipan Singla 2001

<div class="ii gt">

I think I am missing something. Please suggest if you have any idea.

Thanks In Advance.
David Newton

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

And welcome to JavaRanch!
GeeCON Prague 2014
subject: JavaMail - Problem while sending file attachment