Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // build session with credentials and authentication
  2. NSString *smtpHostname = @"mrint.1and1.com";
  3. NSUInteger smtpPort = 587;
  4. NSString *smtpUsername = @"<intranetuser>";
  5. NSString *smtpPassword = @"<password>";
  6. NSString *fromEmail = @"<email?";
  7. NSString *fromDisplayname = @"Peter Breitling";
  8.  
  9. MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
  10. smtpSession.checkCertificateEnabled = NO;
  11. smtpSession.hostname = smtpHostname;
  12. smtpSession.port = (unsigned int) smtpPort;
  13. smtpSession.username = smtpUsername;
  14. smtpSession.password = smtpPassword;
  15. smtpSession.connectionType = MCOConnectionTypeStartTLS;
  16.  
  17. //smtpSession.authType = MCOAuthTypeSASLPlain;
  18. //smtpSession.authType = MCOAuthTypeSASLNone;
  19.  
  20. // build from
  21.  
  22. MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
  23. [[builder header] setFrom:[MCOAddress addressWithDisplayName:fromDisplayname mailbox:fromEmail]];
  24. //[[builder header] setFrom:[MCOAddress addressWithRFC822String:fromEmail]];
  25.  
  26. // build to, cc and bcc adresses
  27.  
  28. NSMutableArray *to = [[NSMutableArray alloc] init];
  29. for (NSString *email in [mail sortedToMailContacts])
  30. {
  31. [to addObject:[MCOAddress addressWithMailbox:email]];
  32. }
  33. [[builder header] setTo:to];
  34. NSMutableArray *cc = [[NSMutableArray alloc] init];
  35. for (NSString *email in [mail sortedCcMailContacts])
  36. {
  37. [cc addObject:[MCOAddress addressWithMailbox:email]];
  38. }
  39. [[builder header] setCc:cc];
  40. NSMutableArray *bcc = [[NSMutableArray alloc] init];
  41. for (NSString *email in [mail sortedBccMailContacts])
  42. {
  43. [bcc addObject:[MCOAddress addressWithMailbox:email]];
  44. }
  45. [[builder header] setBcc:bcc];
  46.  
  47. // subject, text- & html-body
  48.  
  49. [[builder header] setSubject:mail.subject];
  50. builder.textBody = mail.textBody.content;
  51. builder.htmlBody = htmlBody;
  52.  
  53. // attachments
  54.  
  55. // MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:path];
  56. // [builder addAttachment:attachment];
  57. NSData * rfc822Data = [builder data];
  58. smtpSession.connectionLogger = ^(void * connectionID, MCOConnectionLogType type, NSData * data){
  59. NSLog(@"smtp session[%li] %@", (long)type, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  60. };
  61.  
  62. MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
  63. [sendOperation start:^(NSError *error) {
  64. if(error) {
  65. NSLog(@"Error sending email:%@", error);
  66. } else {
  67. NSLog(@"Successfully sent email!");
  68. }
  69. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement