objective-c swift api mailcore2 mailcore

objective c - Envío de correos electrónicos simples de Mailcore2 en Swift



objective-c api (3)

Así es como lo hice:

Paso 1) Importar mailcore2, estoy usando cocoapods

pod ''mailcore2-ios''

Paso 2) Agregue mailcore2 a su encabezado de puente: Project-Bridging-Header.h

#import <MailCore/MailCore.h>

Paso 3) Traduce rápido

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

Nota: es posible que necesite una contraseña de aplicación especial para su cuenta de Google. Consulta https://support.google.com/accounts/answer/185833.

Recientemente incorporé MailCore2 en mi proyecto Objective-C, y funcionó perfectamente. Ahora, estoy en proceso de transición del código dentro de la aplicación a Swift. He importado con éxito la API de MailCore2 en mi proyecto rápido, pero no veo documentación (búsqueda de Google, libmailcore.com, github) sobre cómo convertir el siguiente código Objective-C en código 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!"); } }];

¿Alguien sabe cómo enviar correctamente un correo electrónico en Swift usando esta API? Gracias de antemano a todos los que responden.


Para enviar una imagen como archivo adjunto en poco tiempo solo agregue:

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)


Para Swift 3 solo copie esto

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