2015-07-17 9 views
9

Ich habe kürzlich MailCore2 in mein Objective-C-Projekt integriert, und es hat perfekt funktioniert. Jetzt bin ich dabei, den Code innerhalb der App auf Swift umzustellen. Ich habe erfolgreich die MailCore2 API in meinem schnelles Projekt importiert, aber ich sehe keine Dokumentation (Google-Suche, libmailcore.com, Github), wie der folgenden Arbeits Objective-C-Code in Swift-Code zu aktivieren:Senden von Mailcore2 Plain E-Mails in Swift

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init]; 
smtpSession.hostname = @"smtp.gmail.com"; 
smtpSession.port = 465; 
smtpSession.username = @"[email protected]"; 
smtpSession.password = @"password"; 
smtpSession.authType = MCOAuthTypeSASLPlain; 
smtpSession.connectionType = MCOConnectionTypeTLS; 

MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init]; 
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R" 
               mailbox:@"[email protected]"]; 
MCOAddress *to = [MCOAddress addressWithDisplayName:nil 
              mailbox:@"[email protected]"]; 
[[builder header] setFrom:from]; 
[[builder header] setTo:@[to]]; 
[[builder header] setSubject:@"My message"]; 
[builder setHTMLBody:@"This is a test message!"]; 
NSData * rfc822Data = [builder data]; 

MCOSMTPSendOperation *sendOperation = 
    [smtpSession sendOperationWithData:rfc822Data]; 
[sendOperation start:^(NSError *error) { 
    if(error) { 
     NSLog(@"Error sending email: %@", error); 
    } else { 
     NSLog(@"Successfully sent email!"); 
    } 
}]; 

Does Wer weiß, wie man mit dieser API erfolgreich eine E-Mail in Swift sendet? Vielen Dank im Voraus an alle, die antworten.

Antwort

22

Hier ist, wie ich es tat:

Schritt 1) ​​ Import mailcore2, ich bin mit cocoapods

pod 'mailcore2-ios' 

Schritt 2) mailcore2 zu Ihrem Überbrückung Header hinzufügen: Projekt-Bridging- header.h

#import <MailCore/MailCore.h> 

Schritt 3) Tr anslate zu schnellen

var smtpSession = MCOSMTPSession() 
smtpSession.hostname = "smtp.gmail.com" 
smtpSession.username = "[email protected]" 
smtpSession.password = "xxxxxxxxxxxxxxxx" 
smtpSession.port = 465 
smtpSession.authType = MCOAuthType.SASLPlain 
smtpSession.connectionType = MCOConnectionType.TLS 
smtpSession.connectionLogger = {(connectionID, type, data) in 
    if data != nil { 
     if let string = NSString(data: data, encoding: NSUTF8StringEncoding){ 
      NSLog("Connectionlogger: \(string)") 
     } 
    } 
} 

var builder = MCOMessageBuilder() 
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "[email protected]")] 
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "[email protected]") 
builder.header.subject = "My message" 
builder.htmlBody = "Yo Rool, this is a test message!" 

let rfc822Data = builder.data() 
let sendOperation = smtpSession.sendOperationWithData(rfc822Data) 
sendOperation.start { (error) -> Void in 
    if (error != nil) { 
     NSLog("Error sending email: \(error)") 
    } else { 
     NSLog("Successfully sent email!") 
    } 
} 

Hinweis Sie können ein spezielles App-Passwort für Ihr Google-Konto werden müssen. Siehe https://support.google.com/accounts/answer/185833

+0

Thank you so much! Der Code hat perfekt funktioniert! – iProgramIt

+0

@Rool Paap Hast du das mit 'use_frameworks!' Im 'Podfile' gemacht, anstatt mit dem Bridging-Header zu gehen? Ich probierte das, aber es gibt mir ein ** No solche Modul ** Fehler beim Versuch, es zu importieren, wie so 'Import MailCore'. – Isuru

+0

Als ich dies schrieb, zielte ich immer noch auf iOS7. Also habe ich kein use_frameworks! Aber lass mich sehen, was ich tun kann. –

3

Um ein Bild als Anhang in swift senden Sie einfach hinzufügen:

var dataImage: NSData? 
    dataImage = UIImageJPEGRepresentation(image, 0.6)! 
    var attachment = MCOAttachment() 
    attachment.mimeType = "image/jpg" 
    attachment.filename = "image.jpg" 
    attachment.data = dataImage 
    builder.addAttachment(attachment) 
6

Für Swift 3 einfach kopieren diese

let smtpSession = MCOSMTPSession() 
    smtpSession.hostname = "smtp.gmail.com" 
    smtpSession.username = "[email protected]" 
    smtpSession.password = "xxxxxxx" 
    smtpSession.port = 465 
    smtpSession.authType = MCOAuthType.saslPlain 
    smtpSession.connectionType = MCOConnectionType.TLS 
    smtpSession.connectionLogger = {(connectionID, type, data) in 
     if data != nil { 
      if let string = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){ 
       NSLog("Connectionlogger: \(string)") 
      } 
     } 
    } 

    let builder = MCOMessageBuilder() 
    builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "[email protected]")] 
    builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "[email protected]") 
    builder.header.subject = "My message" 
    builder.htmlBody = "Yo Rool, this is a test message!" 

    let rfc822Data = builder.data() 
    let sendOperation = smtpSession.sendOperation(with: rfc822Data!) 
    sendOperation?.start { (error) -> Void in 
     if (error != nil) { 
      NSLog("Error sending email: \(error)") 
     } else { 
      NSLog("Successfully sent email!") 
     } 
    }