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

我是新来的iOS应用程序开发,下面是我用来发送电子邮件的代码。

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

不幸的是委托方法从来没有触发,任何人都可以请build议如何检查我的电子邮件通过模拟器?

你不能通过模拟器发送邮件。

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

模拟器只显示composer php,但不会让你发送邮件。 发送成功只是确认您的代码是好的,发送时没有任何问题终止。

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

您可以使用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]); } 

以下文件复制到您的项目。

在这里输入图像说明

在这里下载一个示例代码。