2010-08-19 162 views
20

我是ios應用程序開發的新手,下面是我用來發送電子郵件的代碼。無法在模擬器中使用MFMailComposeViewController發送電子郵件

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setSubject:@"My Subject"]; 
    [controller setMessageBody:@"Hello there." isHTML:NO]; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 



    - (void)mailComposeController:(MFMailComposeViewController*)controller 
        didFinishWithResult:(MFMailComposeResult)result 
           error:(NSError*)error { 

      if (result == MFMailComposeResultSent) { 

       NSLog(@"It's away!"); 
      } 

      [self dismissModalViewControllerAnimated:YES]; 
    } 

不幸的是委託方法永遠不會觸發任何一個可以請建議我如何通過模擬器檢查我的電子郵件?

+0

我面臨着同樣的problem..but時,我想我的設備上它工作得很好..感謝lukya的說明。 – 2011-10-13 10:46:00

+0

對於我的任務,我只需要顯示作曲家。但我無法這樣做。 MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; 。這條線彈出警報控制器 – 2017-12-15 07:45:45

回答

51

CAN NOT通過模擬器發送郵件。

相反,您可以在設備中安裝應用程序並從那裏嘗試。

模擬器只顯示作曲家,但不會允許您發送郵件。發送成功只是確認您的代碼是正確的,並且沒有任何問題在發送時終止。

+1

這是正確的答案,不要被髮送的郵件動畫愚弄,電子郵件* *不*真正發送。 – 2014-03-06 06:25:16

+0

對於我的任務,我只需要顯示作曲家。但我無法這樣做。 MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; 。這條線彈出警報控制器。 – 2017-12-15 07:44:56

7

據我所知,你不能從模擬器發送郵件。MFMailComposeViewController使用iPhone的郵件應用程序配置郵箱發送郵件。該模擬器沒有郵件應用程序。

+0

對於我的任務,我只需要顯示作曲家。但我無法這樣做。 MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; 。這條線彈出警報控制器 – 2017-12-15 07:45:14

3

您可以使用Gmail連接發送郵件,您可以將郵件發送給用戶,因爲您需要在代碼中插入一些代碼和設置,以用於發送郵件的代碼。

- (IBAction)sendMessageInBack:(id)anObject{ 

    NSLog(@"Start Sending"); 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"]; 



    NSData *dataObj = [NSData dataWithContentsOfFile:writableDBPath]; 

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; 

    testMsg.fromEmail = @"Your mail id"; 

    testMsg.toEmail = @"sender mail ids"; 

    testMsg.relayHost = @"smtp.gmail.com"; 

    testMsg.requiresAuth = YES; 

    testMsg.login = @"Uour mail id"; 

    testMsg.pass = @"your pass"; 

    testMsg.subject = @"Test application "; 

    testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS! 

    // Only do this for self-signed certs! 

    // testMsg.validateSSLChain = NO; 

    testMsg.delegate = self; 

    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, 

           @"Some text to include in body",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; 




     testMsg.parts = [NSArray arrayWithObjects:plainPart,nil]; 

    [testMsg send]; 



} 


-(void)messageSent:(SKPSMTPMessage *)message{ 
    [message release]; 
    NSLog(@"delegate - message sent"); 
} 



-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{ 
    [message release]; 
    // open an alert with just an OK button 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]); 
} 

並將以下文件複製到您的項目中。

enter image description here

要下載的樣本code here.

+0

這個api工作正常,沒有附件。當您連接兩個以上的文件時,它會崩潰。我認爲MFMailComposeViewController比SKPSMTPMessage更好 – 2013-05-16 09:00:56

相關問題