首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用mailcore发送Ios邮件

使用mailcore发送Ios邮件
EN

Stack Overflow用户
提问于 2016-03-08 09:58:27
回答 2查看 854关注 0票数 1

我试图寄出昨天的邮件,但未能寄出。总是收到错误发送电子邮件: error Domain=MCOErrorDomain Code=1“无法建立到服务器的稳定连接。无法建立到服务器的UserInfo={NSLocalizedDescription=A稳定连接。}

代码语言:javascript
复制
  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

EN

回答 2

Stack Overflow用户

发布于 2016-03-10 01:52:09

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

MCOConnectionTypeStartTLS 在同一个TCP连接上。 在MCOConstants.h中申报。 MCOConnectionTypeTLS 使用SSL/SSL加密连接。 在MCOConstants.h中申报。

参考来源

并评论一下authType:

代码语言:javascript
复制
//smtpSession.authType = MCOAuthTypeSASLPlain;

经过对这个线程的进一步研究,他们删除了authType行,并将MCOConnectionTypeStartTLS更改为MCOConnectionTypeTLS。

票数 1
EN

Stack Overflow用户

发布于 2020-03-05 15:32:10

请尝试这个解决方案,您可以解决您的问题,也可以有人得到解决方案。

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

代码语言:javascript
复制
Also Need to Disable Captcha :  [https://accounts.google.com/DisplayUnlockCaptcha][1]

回复:成功发送电子邮件!

以下是完美的解决方案:

代码语言:javascript
复制
    @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!")
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35864215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档