发送邮件时出错(kSKPSMTPPartMessageKey为零)

时间:2012-10-07 09:33:31

标签: iphone ios ipad gmail

我尝试使用" SKPSMTPMessage"在iPhone上发送邮件我添加了库,

在我的班级中,我添加了以下代码:

- (IBAction)sendMail:(id)sender 
{
// if there are a connection
if ([theConnection isEqualToString:@"true"]) {
    if ([fromEmail.text isEqualToString:@""] || [toEmail.text isEqualToString:@""]) {
        UIAlertView *warning = [[UIAlertView alloc] initWithTitle:@"تحذير" message:@"لم يتم ادخال جميع المجالات" delegate:self cancelButtonTitle:@"موافق" otherButtonTitles:nil, nil];
        [warning show];
    }else {

        SKPSMTPMessage *test_smtp_message = [[SKPSMTPMessage alloc] init];
        test_smtp_message.fromEmail = fromEmail.text;
        test_smtp_message.toEmail = toEmail.text;
        test_smtp_message.relayHost = @"smtp.gmail.com";
        test_smtp_message.requiresAuth = YES;
        test_smtp_message.login = @"ebookmsg@gmail.com";
        test_smtp_message.pass =  @"myPass";
        test_smtp_message.wantsSecure = YES;

        NSString *subject= @"Suggest a book for you";
        test_smtp_message.subject = [NSString stringWithFormat:@"%@ < %@ > ",fromEmail.text, subject];
        test_smtp_message.delegate = self;

        NSMutableArray *parts_to_send = [NSMutableArray array];


        NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                         @"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
                                         [messageBody.text stringByAppendingString:@"\n"], kSKPSMTPPartMessageKey,
                                         @"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
                                         nil];
        [parts_to_send addObject:plain_text_part];

        // to send attachment

        NSString *image_path = [[NSBundle mainBundle] pathForResource:BookCover ofType:@"jpg"];
        NSData *image_data = [NSData dataWithContentsOfFile:image_path];        
        NSDictionary *image_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"inline;\r\n\tfilename=\"image.png\"",kSKPSMTPPartContentDispositionKey,
                                    @"base64",kSKPSMTPPartContentTransferEncodingKey,
                                    @"image/png;\r\n\tname=Success.png;\r\n\tx-unix-mode=0666",kSKPSMTPPartContentTypeKey,
                                    [image_data encodeWrappedBase64ForData],kSKPSMTPPartMessageKey,
                                    nil];


        [parts_to_send addObject:image_part];



        test_smtp_message.parts = parts_to_send;

        Spinner.hidden = NO;
        [Spinner startAnimating];
        ProgressBar.hidden = NO;
        HighestState = 0;

        [test_smtp_message send];

    }

}else {
    UIAlertView *alertNoconnection = [[UIAlertView alloc] initWithTitle:@"تحذير" message:@"لا يوجد شبكة " delegate:self cancelButtonTitle:@"الغاء" otherButtonTitles:nil, nil];
    [alertNoconnection show];
}
}

但是当我尝试发送它时,给出了以下例外:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString appendString:]: nil argument'

并在SKPSMTPMessage.m中强调了这一行

 [message appendString:[part objectForKey:kSKPSMTPPartMessageKey]];

我无法理解究竟是什么 任何人都可以帮我解决这个问题吗? 在此先感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,问题是因为image_data为null而我将其替换为:

        NSString *image_path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],[NSString stringWithFormat:@"%@.jpg",BookCover]];

        NSString *imgLink = [NSString stringWithFormat:@"http://iktab.com/global/modules/bookstore/files/book_cover_photo/%@",BookCover];

        NSString *urlString = [imgLink stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

        NSURL *url = [NSURL URLWithString:urlString];

        NSData* image_data = [NSData dataWithContentsOfURL:url];