如何使用 skpsmtpmessage 正确附加 plist 文件?

Posted

技术标签:

【中文标题】如何使用 skpsmtpmessage 正确附加 plist 文件?【英文标题】:How to properly attach plist files using skpsmtpmessage? 【发布时间】:2011-10-18 08:40:16 【问题描述】:

每次我尝试通过 skpsmtpmessage 库附加 plist 文件时,它们都会以各自正确的名称显示在电子邮件中,但大小为 0 字节。这是代码:

-(void) sendMessage

NSLog(@"Start Sending");

SKPSMTPMessage *test_smtp_message = [[SKPSMTPMessage alloc] init];
test_smtp_message.fromEmail = @"jaredloo@live.com.sg";
test_smtp_message.toEmail = @"vest@un0wn.org";
test_smtp_message.relayHost = @"smtp.live.com";
test_smtp_message.requiresAuth = YES;
test_smtp_message.login = @"jaredloo@live.com.sg";
test_smtp_message.pass = @"blabla";
test_smtp_message.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
test_smtp_message.subject = @"An application has crashed!";




//    test_smtp_message.bccEmail = @"testbcc@test.com";

// Only do this for self-signed certs!
// test_smtp_message.validateSSLChain = NO;
test_smtp_message.delegate = self;

NSMutableArray *parts_to_send = [NSMutableArray array];

//If you are not sure how to format your message part, send an email to your self.  
//In Mail.app, View > Message> Raw Source to see the raw text that a standard email client will generate.
//This should give you an idea of the proper format and options you need
    /*NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
                                     [@"Sample message body here!" stringByAppendingString:@"\n"], kSKPSMTPPartMessageKey,
                                     @"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
                                     nil];
    [parts_to_send addObject:plain_text_part];*/



    //NSString *image_path = [[NSBundle mainBundle] pathForResource:@"Success" ofType:@"png"];
    NSArray *crashList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/User/Library/Logs/CrashReporter" error:nil];
    NSLog(@"CrashList: %@", crashList);
    if ([crashList count] != 0)
    
        for (int i=0; i < [crashList count]; i++) 
            NSString *filePath = [NSString stringWithFormat:@"/User/Library/Logs/CrashReporter/%@",[crashList objectAtIndex:i]];
            NSData *crashData = [NSData dataWithContentsOfFile:filePath]; 
            NSString *directory = @"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"";
            directory = [directory stringByAppendingFormat: filePath];
            directory = [directory stringByAppendingFormat: @"\""];
            NSString *attachment = @"attachment;\r\n\tfilename=\"";
            attachment = [attachment stringByAppendingFormat: filePath];
            attachment = [attachment stringByAppendingFormat: @"\""];
            NSDictionary *image_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                        directory,kSKPSMTPPartContentTypeKey,
                                        attachment,kSKPSMTPPartContentDispositionKey,
                                        [crashData encodeBase64ForData],kSKPSMTPPartMessageKey,
                                        @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
            /*
             [NSDictionary dictionaryWithObjectsAndKeys:
             @"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,
             @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,
             [vcard_data encodeBase64ForData],kSKPSMTPPartMessageKey,
             @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

            */
            [parts_to_send addObject:image_part];
        
    

test_smtp_message.parts = parts_to_send;

[test_smtp_message send];


这是电子邮件(在 vest@un0wn.org 一侧)[GMAIL]

Jared Aaron Loo jaredloo@live.com.sg to me

show details 3:58 PM (39 minutes ago) 
6 attachments — Download all attachments  

    /User/Library/Logs/CrashReporter/LatestCrash-TableViewControl.plist
0K   Import to Contacts   Download  
    /User/Library/Logs/CrashReporter/LatestCrash-iSoda.plist
0K   Import to Contacts   Download  
    /User/Library/Logs/CrashReporter/LatestCrash.plist
0K   Import to Contacts   Download  
    /User/Library/Logs/CrashReporter/TableViewController_09_2011-10-18-104430_Jareds-iPhone.plist
0K   Import to Contacts   Download  
    /User/Library/Logs/CrashReporter/TableViewController_09_2011-10-18-104822_Jareds-iPhone.plist
0K   Import to Contacts   Download  
    /User/Library/Logs/CrashReporter/iSoda_2011-10-18-105543_Jareds-iPhone.plist
0K   Import to Contacts   Download 

非常感谢任何帮助! :)

【问题讨论】:

我使用了与此处显示的类似代码,到目前为止没有任何问题。你的控制台/日志说什么?可能是图片太大了? 【参考方案1】:

这有(至少)两个可能的原因:

 NSData *crashData = [NSData dataWithContentsOfFile:filePath];

 ...
 [crashData encodeBase64ForData],kSKPSMTPPartMessageKey, 
 ...

如果您无法从磁盘读取文件,第一个可能会失败,如果您的 base64 编码由于某种原因失败,第二个可能会失败。尝试为这些方法中的任何一种插入 nil 结果的验证并正确处理错误。

【讨论】:

我已经确定 crashData 和 kSKPSMTPPartMessageKey 在发送时都不为零。我什至尝试用 encodeWrappedBase64ForData 替换 encodeBase64ForData 但无济于事。 您查看过发送/接收的原始电子邮件吗?也许这可以提供线索。 嗨,我已经在另一个示例应用程序中实现了完全相同的逻辑,这似乎可行。但是,当它在我的应用程序中实现时,它不会。不知道为什么=/

以上是关于如何使用 skpsmtpmessage 正确附加 plist 文件?的主要内容,如果未能解决你的问题,请参考以下文章

iOS 发送邮件SKPSMTPMessage

为啥这个使用 SKPSMTPMessage 的应用程序被拒绝了?

iPhone- 使用 SKPSMTPMessage 的应用程序崩溃

使用 SKPSMTPMessage 向 2​​ 个收件人发送电子邮件

使用 SKPSMTPMessage 时 encodeBase64ForData 和 encodeWrappedBase64ForData 有啥区别

SKPSMTPMessage liker 错误