1

我是 iphone 开发的新手。我正在构建一个可以发送电子邮件的应用程序。我正在使用以下代码

- (IBAction)btnsendPressAction:(id)sender {

    if([MFMailComposeViewController canSendMail]){

        MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
        mailer.mailComposeDelegate=self;
        [mailer setSubject:@"A Message for testing"];
      NSArray *toRecipients = [NSArray arrayWithObjects:@"rajshetty2125@gmail.com", @"secondMail@example.com", nil];
        [mailer setToRecipients:toRecipients];

        UIImage *image=[UIImage imageNamed:@"logo.png"];
        NSData *imageData=UIImagePNGRepresentation(image);
        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"logo.png"];
        NSString *emailBody=@"This is test email";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        mailer.modalPresentationStyle = UIModalPresentationPageSheet ;


    }
    else{

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"failure" message:@"Alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

我的问题是 mailComposeController 委托没有被调用,我无法通过我的应用程序发送电子邮件。我经历了许多链接,但每个地方我都找到了相同的方法。谁可以帮我这个事 ?

4

4 回答 4

2

您将无法从模拟器发送电子邮件。您只能使用在邮件应用程序中配置的任何帐户在 iOS 设备上检查此功能。

于 2012-08-28T07:11:01.907 回答
0

在这里使用此链接和您在设备中添加(配置)一个帐户,然后只有您可以发送邮件。

于 2012-08-28T07:04:52.180 回答
0

这个片段对我有用:

if ([MFMailComposeViewController canSendMail]) {
    self.mailController = [[MFMailComposeViewController alloc] init];
    [self.mailController setSubject:@"Invitation"];
    [self.mailController setMessageBody:@"Message" isHTML:NO];
    self.mailController.mailComposeDelegate = self;

    [self presentModalViewController:self.mailController animated:YES];
}

但请确保您在接口中实现委托

@interface MyClass() <MFMailComposeViewControllerDelegate>

并检查您的回调方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
于 2012-08-28T07:09:55.137 回答
-1

嘿,我不确定,但可能有帮助,替换你的最后一行

mailer.modalPresentationStyle = UIModalPresentationPageSheet ;

[self presentModalViewController: mailer animated:YES];
于 2012-08-28T07:09:17.057 回答