2013-05-06 131 views
1

在我的iPhone应用程序中,我使用SMTP发送邮件。但有时邮件发送后,应用程序突然出现以下错误信息使用SMTP发送邮件后,应用程序崩溃

<Warning>: Application 'UIKitApplication:com.myid.smtpsample[0x2630]' exited abnormally with signal 11: Segmentation fault: 11 
��May 6 17:07:21 Device-3 ReportCrash[13041] <Error>: libMobileGestalt copySystemVersionDictionaryValue: Could not lookup ReleaseType from system version dictionary 

崩溃这是我的代码:

-(void) sendEmail 
{ 
    NSData *imagedata=UIImageJPEGRepresentation(image, 0.2f); 

    SKPSMTPMessage *Message = [[SKPSMTPMessage alloc] init]; 
    Message.fromEmail = @"my email"; 
    Message.toEmail = receiverEmailString; 
    Message.relayHost = @"smtp.gmail.com"; 
    Message.requiresAuth = YES; 
    Message.login = @"my email"; 
    Message.pass = @"my password"; 
    Message.subject = @"Details"; 
    Message.wantsSecure = YES; 
    Message.delegate = self; 

    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,@"Message Body",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; 

    NSDictionary *vcfPart= [NSDictionary dictionaryWithObjectsAndKeys:@"image/jpeg;\r\n\tx-unix-mode=0644;\r\n\tname=\"MyPhoto.jpg\"",kSKPSMTPPartContentTypeKey, 
          @"attachment;\r\n\tfilename=\"MyPhoto.jpg\"",kSKPSMTPPartContentDispositionKey,[imagedata encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; 

    Message.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; 
    [Message send]; 

} 


- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{ 

    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]); 
} 

- (void)messageSent:(SKPSMTPMessage *)message{ 

    NSLog(@"delegate - message sent"); 


} 

请告诉我,我做错了

回答

0

你在方法中创建消息,将其作为委托给它自己(意味着它在完成时会发送消息),但在发送消息之后,当离开方法时由ARC发布消息。因此,创建一个消息ivar,并且在它告诉你它成功或失败之后(并且在调度到主线程的块中这样做,在委托回调中直接删除或释放对象是有风险的),它只会是零。 PS:同样,对于类对象,请使用小写字母和大写第一个字母。

+0

由于您没有给出示例代码,因此我必须为代码投票选出相同的解决方案。 – Thiru 2015-06-04 13:00:53

+0

@Thiru,我明白了。它更容易复制和粘贴解决方案,而不必考虑代码的功能,或者为什么。愚蠢的我 - 我只是一个老派的开发者。 – 2015-06-04 13:55:29

4

我知道我有点迟来回答这个问题。但可能会帮助别人。所以在这里。

我有同样的问题,这就是我解决它的方法。我唯一需要做的就是给SKPSMTPMessage对象添加一个强引用,并在发送电子邮件时引用它。奇迹般有效。 (哦,也是我留在机智message = nil;并没有引起任何问题对我来说)。

 @interface MyViewController()  
    @property (nonatomic, strong) SKPSMTPMessage *Message; 
    @end 

    -(void) sendEmail 
    { 
     _Message = [[SKPSMTPMessage alloc] init]; 
     _Message.fromEmail = @"my email"; 
     _Message.toEmail = receiverEmailString; 
     ... 
     [_Message send]; 

    } 


    - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{ 
     message = nil; 
     NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]); 
    } 

    - (void)messageSent:(SKPSMTPMessage *)message{  
     message = nil; 
     NSLog(@"delegate - message sent"); 
    } 

希望这有助于。