Sending emails from Cocoa

/ Published in: Objective C
Save to your folder(s)

With 10.5 Apple deprecated the use of NSMailDelivery – without a replacement. So the questions comes up regulary – how does one send emails from Cocoa? Turns out there are couple of frameworks available that can also be used to do the job.

* Pantomime (LGPL license) -
* MailCore (New BSD license) -
* EDMessage (BSD syle license) -

EDMessage has been around for ages. Checkout the example on how to use the API. MailCore is also really easy to use. But both lack support for asynchronous mail delivery. With MailCore I couldn’t even work out how to send attachments. Also no idea if it’s getting developed anymore. So if you are OK to use a LGPL library Pantomime seemed like a good choice. Sending simple emails is quite straight forward.

Copy this code and paste it in your HTML
  1. CWMessage *message = [[CWMessage alloc] init];
  3. CWInternetAddress *address;
  5. address = [[CWInternetAddress alloc] initWithString:@"[email protected]"];
  6. [message setFrom:address];
  7. [address release];
  9. address = [[CWInternetAddress alloc] initWithString:@"[email protected]"];
  10. [address setType:PantomimeToRecipient];
  11. [message addRecipient:address];
  12. [address release];
  14. [message setSubject:@"test"];
  16. [message setContentType: @"text/plain"];
  17. [message setContentTransferEncoding: PantomimeEncodingNone];
  18. [message setCharset: @"us-ascii"];
  20. [message setContent: [@"This is a simple content." dataUsingEncoding: NSASCIIStringEncoding]];
  22. smtp = [[CWSMTP alloc] initWithName:@"" port:465];
  23. [smtp setDelegate: self];
  24. [smtp setMessage: message];
  25. [message release];
  27. ssl = YES;
  28. mechanism = @"PLAIN";
  30. [smtp connectInBackgroundAndNotify];


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.