Solved

Javamail Multipart not working on Outlook

Posted on 2008-06-20
9
2,149 Views
Last Modified: 2013-11-24
Hi experts,
                i was trying out a program on sending a mail using Javamail. The contents of the mail would be just an embedded image (not an attachment). I was able a similar mail. Gmail seems to work, but if the same mail is sent to an id which is viewed through outlook express (2003) my image is lost. Attaching the code below. The contents of the html file i am using is as below :
 <BODY>
  <img src="cid:image">
 </BODY>

Please let me know what i am supposed to add..
package org.me.mail.Test;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.*;

import javax.mail.internet.*;
 

import java.io.File;

import java.util.*;
 

public class MailOnGmailnSSL

{

  String  d_email = "xxxxx@gmail.com",

          d_password = "xxxx",

          d_host = "smtp.gmail.com",

          d_port  = "465",

          m_to = "xxxx@companyName.com",

          m_subject = "Testing",

          m_text = "Hey, this is the test mail.";

  

  public static final String TXT_FILE = "c:\\temp\\text_contents.txt";

  public static final String HTML_FILE = "c:\\temp\\html_contents.html";

  public static final String IMAGE_FILE = "c:\\temp\\ch851118.gif";

  

  public MailOnGmailnSSL()

  {

      Properties props = new Properties();

      props.setProperty("proxySet","true");

      props.setProperty("socksProxyHost","proxy.sgp.hp.com");

      props.setProperty("socksProxyPort","8080");

      props.put("mail.smtp.user", d_email);

      props.put("mail.smtp.host", d_host);

      props.put("mail.smtp.port", d_port);

      props.put("mail.smtp.starttls.enable","true");

      props.put("mail.smtp.auth", "true");

      //props.put("mail.smtp.debug", "true");

      props.put("mail.smtp.socketFactory.port", d_port);

      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

      props.put("mail.smtp.socketFactory.fallback", "false");

      
 

      SecurityManager security = System.getSecurityManager();
 

      try

      {

          Authenticator auth = new SMTPAuthenticator();

          Session session = Session.getInstance(props, auth);

          session.setDebug(true);
 

          MimeMessage msg = new MimeMessage(session);

          Multipart mp = new MimeMultipart("alternative");

          BodyPart bp = getFileBodyPart(TXT_FILE);

          mp.addBodyPart(bp);

          

          

          //msg.setText(m_text);

          msg.setSubject(m_subject);

          msg.setFrom(new InternetAddress(d_email));

          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

          

          Multipart mp_html = new MimeMultipart("related");

          BodyPart bp_html = getFileBodyPart(HTML_FILE);

          mp_html.addBodyPart(bp_html);

          

          String cid = "cid:image";

          MimeBodyPart mp_image = new MimeBodyPart();

          FileDataSource fd = new FileDataSource(IMAGE_FILE);

          mp_image.setFileName(IMAGE_FILE);

          mp_image.setText("Image 1");

          mp_image.setDataHandler(new DataHandler(fd));

          mp_image.setHeader("Content_id", "<"+cid+">");

          mp_image.setDisposition("inline");

          mp_html.addBodyPart(mp_image);

          

          BodyPart bp2 = new MimeBodyPart();

          bp2.setContent(mp_html);

          

          mp.addBodyPart(bp2);

          

          msg.setContent(mp);

          

          Transport.send(msg);

      }

      catch (Exception mex)

      {

          mex.printStackTrace();

      } 

  }

  

  private BodyPart getFileBodyPart(String txt_file) throws javax.mail.MessagingException {
 

	  BodyPart bp = new MimeBodyPart();

	  bp.setDataHandler(new DataHandler(new FileDataSource(txt_file)));

	  

	return bp;

}
 

public static void main(String[] args)

  {

      MailOnGmailnSSL blah = new MailOnGmailnSSL();

  }
 

  private class SMTPAuthenticator extends javax.mail.Authenticator

  {

      public PasswordAuthentication getPasswordAuthentication()

      {

          return new PasswordAuthentication(d_email, d_password);

      }

  }

}

Open in new window

0
Comment
Question by:royalcyber
  • 6
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21831331
Are you sure it's not just the Outlook config? What happens when you send an embedded image with another app to that Outlook?
0
 

Author Comment

by:royalcyber
ID: 21831380
Hi CEHJ.. sorry, couldn't really get that ? Can you give me an example
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21831604
I'm asking: what happens if you send an embedded image to the same instance of Outlook from another program?
0
 

Author Comment

by:royalcyber
ID: 21831892
from another program ? Sorry CEHJ .. i still didnt get it.. i tried sending an image to gmail, it wrked.. But outlook express is failing.. i send to 2 different outlook clients. Both give the same result. Its just showing a cross n no image .. i hope i answered your question..
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
ID: 21832070
>>from another program ? Sorry CEHJ .. i still didnt get it

Send an embedded image from Thunderbird
Send an embedded image from Eudora
...

0
 

Author Comment

by:royalcyber
ID: 21832197
oh.. that works.. Correct me if i am wrong but an embedded image is just an image being pasted onto the mail window (in html format) and on receiving it comes inline with the mail and not as an attachment rt .. If thts the case, it works when i sent it from gmail..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21832508
An embedded image is just a multipart with an inline html image reference to the image content
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 21832584
Check out

http://java.sun.com/developer/onlineTraining/JavaMail/exercises/MailHtml/

I think you'll find it's Content-ID you need (note hyphen, not underscore). Check out the html source of the one that *does* work in Outlook
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21834048
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 50
wordlen challenge 3 47
JDeveloper 12c for 32 bit 4 35
thymeleaf natural templating vs JSP 2 25
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now