【问题标题】:Ios Mail sending using mailcore使用 mailcore 发送 Ios 邮件
【发布时间】:2016-06-22 05:05:04
【问题描述】:

我正在尝试发送昨天的邮件,但发送失败。总是收到这个错误 发送电子邮件时出错:Error Domain=MCOErrorDomain Code=1“无法建立与服务器的稳定连接。” UserInfo={NSLocalizedDescription=无法建立与服务器的稳定连接。}

  NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(self.emailimage)];
//Create a base64 string representation of the data using NSData+Base64
NSString *base64String = [imageData base64EncodedString];

//userdefaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *userName = [prefs stringForKey:@"username"];
NSString *password = [prefs stringForKey:@"password"];



MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];

smtpSession.hostname =@"smtp.gmail.com";
//
smtpSession.port = 465;

smtpSession.username =userName;
smtpSession.password =password;
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType =MCOConnectionTypeStartTLS;

MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from1 = [MCOAddress addressWithDisplayName:@""
                                               mailbox:userName];
MCOAddress *to1 = [MCOAddress addressWithDisplayName:nil
                                            mailbox:self.to.text];
[[builder header] setFrom:from1];
[[builder header] setTo:@[to1]];
[[builder header] setSubject:self.subject.text];
NSDate *now = [NSDate date];

 double seconds1 = [now timeIntervalSince1970];
NSNumber *seconds = [NSNumber numberWithInteger:seconds1];
NSLog(@"id is=======================%@",seconds);
AppDelegate *tokenD = [[UIApplication sharedApplication]delegate];
  NSLog(@"token in Composeviewcontroller %@",tokenD.Dtoken);
NSString *htmlbody1;

htmlbody1=@"abc";
[builder setHTMLBody:htmlbody1];
MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:self.filename];
[builder addAttachment:attachment];

NSData * rfc822Data = [builder data];


MCOSMTPSendOperation *sendOperation =
[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
    NSLog(@"Entered");
    if(error) {

        NSLog(@"Error sending email: %@", error);
    }

    else {

        NSLog(@"Successfully sent email!");
    }
}];

总是进入错误块和错误并收到错误发送电子邮件时出错:Error Domain=MCOErrorDomain Code=1

【问题讨论】:

  • 使用smtpSession.connectionType =MCOConnectionTypeTLS;
  • 也尝试过,但没有成功

标签: ios iphone gmail-api mailcore2


【解决方案1】:

请尝试此解决方案,您可能会解决您的问题或有人得到解决方案

Swift 5、iOS 13、Xcode 版本 11.3.1 (11C504)

Also Need to Disable Captcha :  [https://accounts.google.com/DisplayUnlockCaptcha][1]

响应:已成功发送电子邮件!

这里是完美的解决方案:

    @IBAction func btnSendMailClicked(_ sender: UIButton) {

    print(#function)
    let smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "emailaddress@gmail.com"
    smtpSession.password = "password" //You can create [App Password from gmail setting][1] 

    smtpSession.port = 587 //25
    smtpSession.authType = MCOAuthType.saslPlain
    smtpSession.connectionType = MCOConnectionType.startTLS
    smtpSession.isCheckCertificateEnabled = false

    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: "Swifty Developers", mailbox: "swiftydevelopers@gmail.com")!]
    builder.header.from = MCOAddress(displayName: "Mehul Parmar", mailbox: "mehulasjack@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: \(String(describing: error))")
        } else {
            NSLog("Successfully sent email!")
        }
    }
}

【讨论】:

    【解决方案2】:

    您应该尝试使用MCOConnectionTypeTLS for smtpSession.connectionType

    MCOConnectionTypeStartTLS
    在同一个 TCP 连接上。
    在 MCOConstants.h 中声明。

    MCOConnectionTypeTLS
    使用 TLS/SSL 的加密连接。
    在 MCOConstants.h 中声明。

    source of reference

    并注释掉 authType:

    //smtpSession.authType = MCOAuthTypeSASLPlain;
    

    在此thread 的进一步研究中,他们删除了 authType 行并将 MCOConnectionTypeStartTLS 更改为 MCOConnectionTypeTLS。

    【讨论】:

    • 改变但结果相同
    • 我必须完全相反:从 TLS 更改为 startTLS
    猜你喜欢
    • 2013-04-17
    • 2015-04-18
    • 2012-08-21
    • 2012-08-27
    • 2016-07-01
    • 2014-12-12
    • 2013-09-18
    • 2013-08-04
    • 2012-10-21
    相关资源
    最近更新 更多