2015-07-17 18 views
9

Gần đây tôi đã kết hợp MailCore2 vào dự án Objective-C của tôi và nó đã hoạt động hoàn hảo. Bây giờ, tôi đang trong quá trình chuyển mã trong ứng dụng sang Swift. Tôi đã nhập thành công API MailCore2 vào dự án nhanh chóng của tôi, nhưng tôi thấy không có tài liệu (tìm kiếm Google, libmailcore.com, github) về cách chuyển mã làm việc Objective-C sau vào Swift Code:Gửi Mailcore2 Email đồng bằng trong 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!"); 
    } 
}]; 

Liệu có ai biết cách gửi thành công một email trong Swift bằng API này không? Cảm ơn trước tất cả những ai trả lời.

Trả lời

22

Đây là cách tôi đã làm nó:

Bước 1) nhập mailcore2, Tôi đang sử dụng cocoapods

pod 'mailcore2-ios' 

Bước 2) Thêm mailcore2 để tiêu đề cầu nối của bạn: Dự án-Bridging- Header.h

#import <MailCore/MailCore.h> 

Bước 3) Tr anslate để nhanh chóng

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 Bạn có thể cần một mật khẩu ứng dụng đặc biệt cho tài khoản Google của bạn. Xem https://support.google.com/accounts/answer/185833

+0

Cảm ơn bạn rất nhiều! Mã hoạt động hoàn hảo! – iProgramIt

+0

@Rool Paap Bạn đã làm điều này với 'use_frameworks!' Flag trong 'Podfile' thay vì đi theo tiêu đề cầu nối? Tôi đã thử nó nhưng nó mang lại cho tôi một ** Không có mô-đun ** lỗi khi cố gắng nhập khẩu nó như vậy 'nhập khẩu MailCore'. – Isuru

+0

Khi tôi viết bài này, tôi vẫn đang nhắm mục tiêu iOS7. Vì vậy, tôi đã không sử dụng use_frameworks !. Nhưng hãy để tôi xem những gì tôi có thể làm. –

3

Để gửi một hình ảnh như tập tin đính kèm trong nhanh chóng chỉ cần thêm:

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

Đối Swift 3 chỉ cần sao chép này

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!") 
     } 
    } 
Các vấn đề liên quan