我试图寄出昨天的邮件,但未能寄出。总是收到错误发送电子邮件: error Domain=MCOErrorDomain Code=1“无法建立到服务器的稳定连接。无法建立到服务器的UserInfo={NSLocalizedDescription=A稳定连接。}
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
发布于 2016-03-10 01:52:09
您应该尝试使用MCOConnectionTypeTLS for smtpSession.connectionType
MCOConnectionTypeStartTLS 在同一个TCP连接上。 在MCOConstants.h中申报。 MCOConnectionTypeTLS 使用SSL/SSL加密连接。 在MCOConstants.h中申报。
参考来源
并评论一下authType:
//smtpSession.authType = MCOAuthTypeSASLPlain;经过对这个线程的进一步研究,他们删除了authType行,并将MCOConnectionTypeStartTLS更改为MCOConnectionTypeTLS。
发布于 2020-03-05 15:32:10
请尝试这个解决方案,您可以解决您的问题,也可以有人得到解决方案。
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!")
}
}
}https://stackoverflow.com/questions/35864215
复制相似问题