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) - http://www.collaboration-world.com/pantomime/
* MailCore (New BSD license) - http://www.mronge.com/m/MailCore/
* EDMessage (BSD syle license) - http://www.mulle-kybernetik.com/software/EDFrameworks/download.html#EDMessage

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];
  2.  
  3. CWInternetAddress *address;
  4.  
  5. address = [[CWInternetAddress alloc] initWithString:@"[email protected]"];
  6. [message setFrom:address];
  7. [address release];
  8.  
  9. address = [[CWInternetAddress alloc] initWithString:@"[email protected]"];
  10. [address setType:PantomimeToRecipient];
  11. [message addRecipient:address];
  12. [address release];
  13.  
  14. [message setSubject:@"test"];
  15.  
  16. [message setContentType: @"text/plain"];
  17. [message setContentTransferEncoding: PantomimeEncodingNone];
  18. [message setCharset: @"us-ascii"];
  19.  
  20. [message setContent: [@"This is a simple content." dataUsingEncoding: NSASCIIStringEncoding]];
  21.  
  22. smtp = [[CWSMTP alloc] initWithName:@"smtp.gmail.com" port:465];
  23. [smtp setDelegate: self];
  24. [smtp setMessage: message];
  25. [message release];
  26.  
  27. ssl = YES;
  28. mechanism = @"PLAIN";
  29.  
  30. [smtp connectInBackgroundAndNotify];

URL: http://vafer.org/blog/20080604120118

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.