Sending HTML email with EDMessage

Discussion of the EDMessage and EDCommon frameworks.

Sending HTML email with EDMessage

Postby erik » 10 Oct 2009, 18:46

EDMailAgent has some convenience methods to send plain text. If you want to send HTML messages (or anything else, really) you will have to use the following method:

Code: Select all
- (void)sendMessage:(EDInternetMessage *)message;

It will be useful first to study the following method in EDMailAgent, which constructs plain text messages

Code: Select all
- (void)sendMailWithHeaders:(NSDictionary *)userHeaders body:(NSString *)body andAttachments:(NSArray *)attachmentList

Then you can do something similar for HTML messages. The EDInternetMessage object would be created from an EDHTMLTextContentCoder, roughly like this:

  • Allocate an EDHTMLTextContentCoder
  • Use initWithText: to pass in the HTML source
  • Call the "message" method on the coder to get the message
  • Add the headers to the message using addToHeaderFields:

In summary the code could look as follows:

Code: Select all
    NSString *htmltext = @"<html><body>Hello,<br>this is a HTML email test!</body></html>";
    EDHTMLTextContentCoder *htmlcoder=[[[EDHTMLTextContentCoder alloc] initWithText: htmltext] autorelease];
    EDInternetMessage *message=[htmlcoder message];

    [message addToHeaderFields:[EDObjectPair pairWithObjects:EDMailFrom:@""]];
    [message addToHeaderFields:[EDObjectPair pairWithObjects:EDMailTo:@""]];
    [message addToHeaderFields:[EDObjectPair pairWithObjects:EDMailSubject:@"An HTML email"]];
    EDMailAgent *mailAgent = [EDMailAgent mailAgentForRelayHostWithName:@""];
    [mailAgent sendMessage:message];
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Re: Sending HTML email with EDMessage

Postby tciuro » 19 Jan 2010, 05:08


While the example is very clear and informative, it leaves to speculation whether a content-rich HTML message can be sent via EDMessage. If so, how would the example look like? Ideally I would like to avoid additional downloads, so the images would have to be embedded. Something similar to this example:

Any pointers? Thanks!

-- Tito
Posts: 1
Joined: 19 Jan 2010, 05:06

Re: Sending HTML email with EDMessage

Postby erik » 19 Jan 2010, 12:03

Yes, you can send such emails with EDMessage. The trick lies in creating the right message structure and linking the image reference to another message part. In code this could look as follows:

Code: Select all
// Create html text part

NSString *html = @"<p>This is the <b>HTML</b> version.<p><img src=\"cid:image-1\">";
EDContentCoder *coder = [[[EDHTMLTextContentCoder alloc] initWithText:html] autorelease];
EDMessagePart *htmltextPart = [coder messagePart];

// Create image part

NSData *imageData = [NSData dataWithContentsOfFile:@"edmessage.jpg"];
coder = [[[EDMultimediaContentCoder alloc] initWithData:imageData filename:@"edmessage.jpg"] autorelease];
EDMessagePart *imagePart = [coder messagePart];
[imagePart addToHeaderFields:[EDObjectPair pairWithObjects:@"Content-ID":@"<image-1>"]];

// Create a related multipart from html text part and image part

NSArray *htmlPartList = [NSMutableArray arrayWithObjects:htmltextPart, imagePart, nil];
EDCompositeContentCoder *compositeCoder = [[[EDCompositeContentCoder alloc] initWithSubparts:htmlPartList] autorelease];
EDMessagePart *htmlPart = [compositeCoder messagePartWithSubtype:@"related"];

// Create plaintext part

NSString *plaintext = @"This is the plain text alternative.";
coder = [[[EDPlainTextContentCoder alloc] initWithText:plaintext] autorelease];
EDMessagePart *plaintextPart = [coder messagePart];

// Create alternative multipart from plaintext and html parts

NSArray *textPartList = [NSMutableArray arrayWithObjects:plaintextPart, htmlPart, nil];
compositeCoder = [[[EDCompositeContentCoder alloc] initWithSubparts:textPartList] autorelease];
EDInternetMessage *message = [compositeCoder messageWithSubtype:@"alternative"];

Note how the filename of the image is irrelevant. The HTML text refers to the image by its content ID, which is something we're free to make up.

By the way, the Python example referenced further up actually has the message structure wrong. The multipart/alternative should be at the top level and the multipart/related nested below it.

Lastly, I've attached a sample project that contains this code, and also shows how to deliver the message over a secure and password protected SMTP link. If you run this, please, please change the recipient address and mail server details.
Example: Message with embedded image.
(698.27 KiB) Downloaded 871 times
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Re: Sending HTML email with EDMessage

Postby Jocelyn » 21 Oct 2010, 03:22

Thank you Erik. :D

Really appreciate EDMessage and your posts to the forum.
Posts: 2
Joined: 20 Oct 2010, 01:02

Return to ED Frameworks