by 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.
- Attachments
-
- MWEI.tar.gz
- Example: Message with embedded image.
- (698.27 KiB) Downloaded 1062 times
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]
// 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"];
[/code]
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.