2009-11-21 67 views
2

我已經使用MFMailComposeViewController發送生成的報告(csv)。無法通過郵件發送csv文件附件

現在郵件被髮送到:電子郵件ID,&但是當我檢查郵件我收到了郵件但附件不在那裏。

然後我還試圖MailComposer例如:

https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html

其中PNG圖像被附加到演示郵件。我還使用該應用程序發送郵件,但未傳送相同的結果圖像附件。

幫忙,找出有什麼問題? 在此先感謝。

這裏是該應用代碼:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
picker.mailComposeDelegate = self; 

[picker setSubject:@"Hello from California!"]; 


// Set up recipients 
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

[picker setToRecipients:toRecipients]; 
[picker setCcRecipients:ccRecipients]; 
[picker setBccRecipients:bccRecipients]; 

// Attach an image to the email 
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"]; 
NSData *myData = [NSData dataWithContentsOfFile:path]; 
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"]; 

// Fill out the email body text 
NSString *emailBody = @"It is raining in sunny California!"; 
[picker setMessageBody:emailBody isHTML:NO]; 

[self presentModalViewController:picker animated:YES]; 
[picker release]; 

回答

0

如果你的代碼看起來不錯,我會懷疑是myData沒有得到加載數據nil。輸入NSLog([myData description])或使用調試器檢查myData不是nil

+0

謝謝兄弟... 我檢查了mydata,它不是零.. !!!! 但我仍無法找到附件。 – Nic 2009-11-21 10:38:38

0

我出來的想法。如果我被困在這種情況下,我會用你提供的示例代碼創建一個獨立的項目,看看我是否能夠正常工作。通常在這些情況下,您關注的代碼可能是正確的,錯誤來自其他地方。

另外:

  • 檢查是否路徑變量是零,因爲這將導致附件無法顯示。
  • 確保郵件正文中有郵件撰寫視圖的圖標。當您成功添加附件時,您應該在視圖中看到圖標表示。
  • 而且您可能希望將您的fileName設置爲rainy.png,即[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"]

祝你好運。

+0

謝謝..我檢查了路徑,它是真實的和郵件camposerview顯示附件圖標表示.... – Rambo 2009-11-27 09:55:16

1

我試圖用「text/plain」附加.csv文件&我成功了。我想你應該嘗試一樣的。祝你好運。

3

在我的情況下,我的iPhone發送的郵件很好,但從我妻子的iPhone發送時沒有任何附件。原來,她的默認帳戶設置爲她的雅虎帳戶,並且不允許附件。我只是將它切換到她的移動我的帳戶,並附上了。我使用Gmail,所以我從來沒有遇到過問題。

此外,我曾嘗試將MIME類型從text/csv切換到text/plain,但沒有任何幫助。它在我的iPhone上以任何方式工作,而根本不在我妻子的身上。

希望這會有所幫助!

1

我遇到同樣的問題,我通過 解決了這一問題做的,我寫了兩個函數的真的很難看:

One從定義文件名。plist文件,另一個實際上設置文件的位置(包括它的路徑和文件名) 。

使用正確的函數來檢索文件名中MFMailViewController 固定的問題:

-(NSString *)dataFilePath { //this gets the ,plist file 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

// --------------------- -------

-(NSString *)fileName { // this defines the fileName from the values in the .plist 

NSString *patientDetails = [self dataFilePath]; 
NSArray *dataArray = [[NSArray alloc] initWithContentsOfFile:patientDetails]; 

NSString *firstName = [dataArray objectAtIndex:0]; 
NSString *lastName = [dataArray objectAtIndex:1]; 

NSString *fileName = [firstName stringByAppendingString:lastName]; 

return [fileName stringByAppendingString:@".csv"]; 

// -------------------------

 -(NSString *)setFileLocation { //this puts name and folder together. 

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docDirectory = [sysPaths objectAtIndex:0]; 
NSString *fileName = [self fileName]; 
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@", docDirectory,fileName]; 
    // NSLog(@"Filepath is : %@",fullFilePath); 
return fullFilePath; 

}

// -------------------------

最後一個是你想在你MFMailComposeViewController調用函數:

 ... 
NSData *csvData = [NSData dataWithContentsOfFile:[self setFileLocation]]; 
    [picker addAttachmentData:csvData mimeType:@"text/csv" fileName:fileName]; 
    ... 

文件名,當然是一個NSString,通過調用文件名功能檢索到: 的NSString *文件名= [自文件名];

希望這會有所幫助!

相關問題