Posted By:
Rob_Boellaard
Posted On:
Wednesday, April 9, 2003 07:06 AM
Hi everybody, I am really hoping on a quick answer here, I have a crazy deadline in a few days, so sorry for barging in like this... The problem is as follows, i am to send an email with a (hidden) plaintext part, an html part and a few embedded images. This went pretty smoothly while testing it on Outlook Express, but when I looked at Yahoo and hotmail, the images show up as attachments and are not displayed inline, inside the html. The last two days I have been trying to get it to work, by trying hundreds (really!) of combinations, i.e. different ways of combining the related and alternative multiparts and their bodyparts ; by adding and removing and setting the headers of the individual bodytParts programmatically ; comparing th
More>>
Hi everybody,
I am really hoping on a quick answer here, I have a crazy deadline in a few days, so sorry for barging in like this...
The problem is as follows, i am to send an email with a (hidden) plaintext part, an html part and a few embedded images. This went pretty smoothly while testing it on Outlook Express, but when I looked at Yahoo and hotmail, the images show up as attachments and are not displayed inline, inside the html.
The last two days I have been trying to get it to work, by trying hundreds (really!) of combinations, i.e. different ways of combining the related and alternative multiparts and their bodyparts ; by adding and removing and setting the headers of the individual bodytParts programmatically ; comparing the raw source of my emails and other (3d party) emails that do work on Yahoo & Hotmail, etc, etc.
No luck so far and time is running out
Below you can find the source of my mail composition class, please have a look at it and tell me where I am messing up. Sorry if the code is messy (all those commented-out lines) but this is really work under construction.
Thanks a lot,
Rob Boellaard
//
// BLMailComposition.java
// CampaignManager
//
// Created by Rob Boellaard on Mon Apr 07 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
import java.lang.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
public class BLMailComposition {
protected String host = "mail.chello.nl"; //$$$hardcoded settings
protected Message mainMessage;
protected MimeMultipart multipartAlternative;
protected MimeMultipart multipartRelated;
protected BodyPart plainTextBodyPart;
protected BodyPart htmlBodyPart;
protected BodyPart relatedAttachmentsBodyPart;
//$$$hardcoded settings
protected String pathName = "/Volumes/lithium/Users/rboell/Developer/Projects/bluem/products/TotalMail/resources/images/";
public BLMailComposition() {
super();
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null);
mainMessage = new MimeMessage(session);
multipartAlternative = new MimeMultipart("alternative");
multipartRelated = new MimeMultipart("related");
relatedAttachmentsBodyPart = new MimeBodyPart();
}
public void setPlainTextContent(String textContent) throws Exception {
plainTextBodyPart = new MimeBodyPart();
plainTextBodyPart.setContent(textContent, "text/plain");
}
public void setComponentContent(WOComponent aComponent) throws Exception {
htmlBodyPart = new MimeBodyPart();
WOContext context = aComponent.context ();
//ProjectWonder warning (code was copied from ERMailDeliveryComponentBased, rboell)
// CHECKME: It's probably not a good idea to do this here
// since the context could also have been generating relative URLs
// unless the context is created from scratch
context._generateCompleteURLs ();
WOMessage response = aComponent.generateResponse ();
htmlBodyPart.setContent((response.contentString()), "text/html");
multipartRelated.addBodyPart(htmlBodyPart); //rboell zojuist veranderd
}
public void setToAddress(String anAddress) throws Exception {
mainMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(anAddress));
}
public void setFromAddress(String someAddress) throws Exception {
mainMessage.setFrom(new InternetAddress(someAddress));
}
public void setReplyToAddress(String anAddress) throws Exception {
//mainMessage.setReplyTo(new Address[(new InternetAddress(anAddress))]); //!!probably not right
}
public void setSubject(String subjectText) throws Exception {
mainMessage.setSubject(subjectText);
}
public void attachImages(NSArray images) throws Exception {
BLImage anImage;
String imagePath;
java.util.Enumeration enumerator = images.objectEnumerator();
while (enumerator.hasMoreElements()) {
anImage = (BLImage)enumerator.nextElement();
imagePath = pathName + anImage.name();
MimeBodyPart imageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(imagePath);
imageBodyPart.setDataHandler(new DataHandler(fds));
//imageBodyPart.removeHeader("Content-Disposition");//removal is necessary for yahoo and hotmail
imageBodyPart.removeHeader("Content-Type");
imageBodyPart.setHeader ("Content-ID", anImage.altText()); //using using altText for now as the cid
imageBodyPart.setHeader ("Content-Type", "image/gif");
//imageBodyPart.setFileName (anImage.name());
//imageBodyPart.setDisposition("INLINE");
multipartRelated.addBodyPart (imageBodyPart);
java.util.Enumeration myEnum = imageBodyPart.getAllHeaders();
Header anObject;
while (myEnum.hasMoreElements()) {
anObject = (Header)myEnum.nextElement();
System.out.println("header is " + anObject.getName() + " " + anObject.getValue());
}
}
//relatedAttachmentsBodyPart.setDataHandler (new DataHandler (multipartRelated, multipartRelated.getContentType ()));
}
public void sendMail() throws Exception {
//composing - exceptions should be thrown? if not all parts are set properly - will worry about that later, rboell
multipartAlternative.addBodyPart(plainTextBodyPart);
//multipartAlternative.addBodyPart(multipartRelated);
//multipartAlternative.addBodyPart(htmlBodyPart);
//multipartRelated.addBodyPart(htmlBodyPart);
//relatedAttachmentsBodyPart.setDataHandler (new DataHandler (multipartRelated, multipartRelated.getContentType ()));
relatedAttachmentsBodyPart.setContent(multipartRelated);
multipartAlternative.addBodyPart (relatedAttachmentsBodyPart);
mainMessage.setContent(multipartAlternative);
System.out.println("mainMessage.getContent() is " + mainMessage.getContent());
Transport.send(mainMessage);
}
}
<<Less