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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail - Problem while sending file attachment" Watch "JavaMail - Problem while sending file attachment" New topic
Author

JavaMail - Problem while sending file attachment

Aniruddha Biswas
Greenhorn

Joined: Jun 13, 2010
Posts: 1
Hi,
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(
"saniruddhabiswas@yahoo.co.in"));
message.addRecipient(Message.RecipientType.TO,
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");
mpContent.addBodyPart(mbp1);

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

// Add the root body part to the root multipart
mpRoot.addBodyPart(contentPartRoot);

// Add an attachment
MimeBodyPart mbp3 = new MimeBodyPart();
DataSource source = new FileDataSource("javamail.html");
mbp3.setDisposition(Part.ATTACHMENT);
mbp3.setDataHandler(new DataHandler(source));
mbp3.setFileName("javamail.html");
mbp3.setHeader("Content-Type", "text/html");
mpRoot.addBodyPart(mbp3);
message.setContent(mpRoot);
message.saveChanges();
session.setDebug(true);
// Send the message
Transport.send(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....

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

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

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

<P>Hello World</P>
------=_Part_1_14410104.1276373943174--

------=_Part_0_5439109.1276373943170
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>=
.org/TR/REC-html40/loose.dtd">
<html><head>
<meta http-equiv=3D"content-type" content=3D"text/html;
charset=3DISO-8859-1=
<<.....more html code....>>
</div>
<hr>
=A9 Vipan Singla 2001

------=_Part_0_5439109.
<div class="ii gt">
1276373943170--




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

Thanks In Advance.
Aniruddha
David Newton
Author
Rancher

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaMail - Problem while sending file attachment