2016-03-08 28 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