Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

JavaMail - Problem while sending file attachment  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.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 = "";
String from = "";
String to = "";
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("", host);
props.put("mail.smtp.auth", "true");

Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
String username = "";
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.
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
mooooooo ..... tiny ad ....
how do I do my own kindle-like thing - without amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!