2015-07-17 21 views
9

Niedawno dodałem MailCore2 do mojego projektu Objective-C i działało idealnie. Teraz jestem w trakcie przenoszenia kodu w aplikacji do Swift. Mam zaimportowany API MailCore2 do mojego szybkiego projektu, ale nie widzę dokumentacja (wyszukiwanie w Google, libmailcore.com, GitHub), w jaki sposób włączyć następujący kod Objective-C roboczego do kodeksu SWIFT:Wysyłanie Mailcore2 Zwykłe wiadomości e-mail w 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!"); 
    } 
}]; 

robi Czy ktoś wie, jak skutecznie wysłać wiadomość e-mail w Swift za pomocą tego interfejsu API? Z góry dziękuję wszystkim, którzy odpowiadają.

Odpowiedz

22

Oto jak to zrobiłem:

etap 1) importu mailcore2 używam cocoapods

pod 'mailcore2-ios' 

Krok 2) Dodaj mailcore2 do pomostowego nagłówka: Project-Bridging- header.h

#import <MailCore/MailCore.h> 

Etap 3) Tr anslate do szybkiej

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!") 
    } 
} 

Note Możecie być wymagające specjalnej aplikacji hasło do swojego konta Google. Zobacz https://support.google.com/accounts/answer/185833

+0

Dziękuję bardzo! Kod działa doskonale! – iProgramIt

+0

@Rool Paap Czy zrobiłeś to z flagą 'use_frameworks!' W 'Podfile' zamiast przechodzić w sposób mostkowania nagłówka? Próbowałem, ale daje mi ** Brak takiego modułu ** błąd podczas próby zaimportowania go tak jak "Import MailCore". – Isuru

+0

Kiedy to pisałem, nadal kierowałem się na iOS7. Więc nie użyłem use_frameworks !. Ale zobaczmy, co mogę zrobić. –

3

Aby wysłać zdjęcie jako załącznik w Swift wystarczy dodać:

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

Dla Swift 3 wystarczy skopiować ten

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!") 
     } 
    }