【问题标题】:Sending Mailcore2 Plain Emails in Swift在 Swift 中发送 Mailcore2 普通电子邮件
【发布时间】:2015-10-07 17:42:19
【问题描述】:

我最近将 MailCore2 合并到我的 Objective-C 项目中,它运行良好。现在,我正在将应用程序中的代码转换为 Swift。我已成功将 MailCore2 API 导入到我的 swift 项目中,但我没有看到有关如何将以下有效的 Objective-C 代码转换为 Swift 代码的文档(Google 搜索、libmailcore.com、github):

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

MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
                                              mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil 
                                            mailbox:@"hoa@gmail.com"];
[[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!");
    }
}];

有谁知道如何使用此 API 在 Swift 中成功发送电子邮件?提前感谢所有回复的人。

【问题讨论】:

    标签: objective-c swift api mailcore2 mailcore


    【解决方案1】:

    我是这样做的:

    步骤1)导入mailcore2,我用的是cocoapods

    pod 'mailcore2-ios'
    

    步骤 2) 将 mailcore2 添加到您的桥接头:Project-Bridging-Header.h

    #import <MailCore/MailCore.h>
    

    第3步)翻译成swift

    var smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "matt@gmail.com"
    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: "itsrool@gmail.com")]
    builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
    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!")
        }
    } 
    

    注意 您的 Google 帐户可能需要一个特殊的应用密码。见https://support.google.com/accounts/answer/185833

    【讨论】:

    • @Rool Paap 您是否使用Podfile 中的use_frameworks! 标志而不是使用桥接头方式进行此操作?我试过了,但是在尝试像 import MailCore 那样导入它时,它给了我一个 No such module 错误。
    • 写这篇文章的时候,我的目标还是iOS7。所以我没有使用 use_frameworks!。但让我看看我能做什么。
    • 遇到同样的“没有这样的模块”错误,找不到解决方案。我的 cocoapods 知识有限。对不起。
    • 如果您使用 MailCore,请注意,有时 gmail 可能会阻止登录,那么您必须转到您的 gmail 帐户并启用 this 设置。
    • @RoolPaap 我使用了你的代码,但我仍然收到有关凭据的错误,你可以在我的问题中看到它,请stackoverflow.com/questions/36982176/…
    【解决方案2】:

    要快速发送图片作为附件,只需添加:

        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)
    

    【讨论】:

      【解决方案3】:

      对于 Swift 3,只需复制此内容

          let smtpSession = MCOSMTPSession()
          smtpSession.hostname = "smtp.gmail.com"
          smtpSession.username = "sanipcr7@gmail.com"
          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: "sanipcr7@gmail.com")]
          builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
          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!")
              }
          }
      

      【讨论】:

      • 兄弟,我在控制台中收到“无法建立稳定连接”的错误消息
      • @VenkateshChejarla,我也遇到了同样的错误。错误:发送电子邮件时出错:可选(错误域=MCOErrorDomain Code=1“无法建立与服务器的稳定连接。” UserInfo={NSLocalizedDescription=无法建立与服务器的稳定连接。})
      • 有没有人解决这个错误? #帮助我。
      【解决方案4】:

      我会在答案下方的 cmets 中添加此内容,但数量太多了。

      接受的答案中的步骤特别适用于您的谷歌帐户的特殊应用密码链接。

      但是@RoolPaap 使用的桥接头与我使用的不同。我用#include "Mailcore.h"

      我关注了这个youtube video

      #ifndef MyProjectName_Bridging_Header_h
      #define MyProjectName_Bridging_Header_h
      
      #include "Mailcore.h"
      
      #endif /* MyProjectName_Bridging_Header_h */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-20
        • 2015-12-11
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 2018-04-08
        相关资源
        最近更新 更多