This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes problems adding attachements to Javamail with Outlook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "problems adding attachements to Javamail with Outlook" Watch "problems adding attachements to Javamail with Outlook" New topic

problems adding attachements to Javamail with Outlook


Joined: May 10, 2006
Posts: 1

I'm using javamail 1.3, and trying to write a util that sends an email with a list of attachments.

When the email goes through, it puts the attachments in the message body (inline). I've tried a number of variations, including the commented out lines. Any assistance would be most welcome.



public static void sendEmailWithFileAttachment(String p_emailList, String p_fromUser, String p_subject, String p_msg, String[] _attachFileName) {

try {
if (p_emailList == null) return;
StringTokenizer emailAddresses = new StringTokenizer(p_emailList);
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
String smtpServer = System.getProperty("");
if (smtpServer != null)
props.setProperty("", smtpServer);
else {
props.setProperty("", SEMPRA_DEFAULT_SMTP_SERVER);

if (p_fromUser != null) props.setProperty("mail.user", p_fromUser);

Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);

Multipart multiPart = new MimeMultipart();

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(p_msg, "text/plain");

if (_attachFileName != null) {
for (int i = 0; i < _attachFileName.length; i++) {
BodyPart attachmentBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(_attachFileName[i]);

File file = new File(_attachFileName[i]);
if (file.isFile()) {
int length = (int)file.length();
byte[] fileData = new byte[length];
try {
InputStream fis = new FileInputStream(file);
int ii =;
attachmentBodyPart.setDataHandler(new DataHandler(fds));
//DataSource source = new ByteArrayDataSource(fis, "application/x-any");
//attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(new File(_attachFileName[i]).getName());
} catch (FileNotFoundException e) {
} catch (IOException e) {

//System.out.println("addresses: "+p_emailList);
while (emailAddresses.hasMoreTokens()) {
String toAddress = emailAddresses.nextToken(LIST_DELIMITER);
//System.out.println("sending email to "+toAddress);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));

transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
catch (MessagingException e) {
System.out.println("Failed sending email " + e.getMessage());

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249


Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.



Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: problems adding attachements to Javamail with Outlook
Similar Threads
How to embed image (relative path) in an HTML e-mail?
Sending file as an attachment
JavaMail API error
How to send mail to more than one recipients
JavaMail installation