Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaMail - Problem while sending file attachment  RSS feed

Aniruddha Biswas
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.
David Newton
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!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!