使用mailcore发送Ios邮件

使用mailcore发送Ios邮件,ios,iphone,gmail-api,mailcore2,Ios,Iphone,Gmail Api,Mailcore2,我试图发送昨天的邮件,但未能发送。总是得到这个错误 发送电子邮件时出错:Error Domain=MCOErrorDomain Code=1“无法建立到服务器的稳定连接。”UserInfo={NSLocalizedDescription=无法建立到服务器的稳定连接。} NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(self.emailimage)]; //Create a base64 string rep

我试图发送昨天的邮件,但未能发送。总是得到这个错误 发送电子邮件时出错: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使用MCOConnectionTypeTLS。connectionType

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

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

并注释掉authType:

//smtpSession.authType = MCOAuthTypeSASLPlain;

进一步研究后,他们删除了authType行,并将MCOConnectionTypeStartTLS更改为MCOConnectionTypeTLS。

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

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

回复:成功发送电子邮件

以下是完美的解决方案:

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

使用
smtpSession.connectionType=MCOConnectionTypeTLS
也尝试过,但没有改变,但同样的结果必须完全相反:从
TLS
更改为
startTLS
    @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!")
        }
    }
}