无法在模拟器中使用MFMailComposeViewController发送电子邮件

时间:2010-08-19 09:44:12

标签: ios iphone

我是ios app开发的新手,下面是我用来发送电子邮件的代码。

   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];
    }

不幸的是,委托方法永远不会被触发,任何人都可以建议我如何通过模拟器检查我的电子邮件?

3 个答案:

答案 0 :(得分:51)

无法通过模拟器发送邮件。

相反,您可以在设备中安装应用程序并从那里尝试。

模拟器只显示作曲家但不允许你发送邮件。 “成功发送”只是确认您的代码正常,并且在发送时没有终止它的问题。

答案 1 :(得分:7)

据我所知,你不能从模拟器发送邮件.. MFMailComposeViewController使用iPhone的Mail应用程序中配置的邮箱发送邮件。模拟器没有Mail应用程序。

答案 2 :(得分: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.