2016-03-08 85 views
1

我想从昨天发送邮件但未能发送。始终出现此错误 发送电子邮件时出错:错误域= MCOErrorDomain代码= 1“无法建立与服务器的稳定连接。”的UserInfo = {NSLocalizedDescription =到服务器的稳定连接不能建立。}使用mailcore发送Ios邮件

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 [email protected]"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; 

[email protected]"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!"); 
    } 
}]; 

错误块和错误总是会和越来越错误错误发送电子邮件:错误域= MCOErrorDomain代码= 1

+0

使用'smtpSession.connectionType = MCOConnectionTypeTLS;' –

+0

也试过,但没有财产 –

回答

1

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

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

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

source of reference

,并注释掉的authType:

//smtpSession.authType = MCOAuthTypeSASLPlain; 

经进一步研究在这个thread他们删除的authType线,改变MCOConnectionTypeStartTLS到MCOConnectionTypeTLS。

+1

改变,但同样的结果 –

+0

我不得不这样做完全相反:从改变'TLS'到'startTLS'。 – lionello