0

我正在尝试使用“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]];

我不明白究竟什么是 nil 有人能帮我解决这个问题吗?提前致谢。

4

1 回答 1

0

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

        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];
于 2012-10-07T12:18:09.657 回答