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.
Cảm ơn bạn rất nhiều! Mã hoạt động hoàn hảo! – iProgramIt
@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
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. –