代码之家  ›  专栏  ›  技术社区  ›  Joseph Tura

MFMailComposeViewController不发送邮件

  •  0
  • Joseph Tura  · 技术社区  · 14 年前

    我正在尝试使用MFMailComposeViewController发送邮件。一切正常,除了邮件不被发送,我总是得到MFMailComposeResultFailed。

    有什么建议吗?我没有使用模拟器,从我的设备发送邮件也可以。我确实有一个连接(通过可达性进行测试),[MFMailComposeViewController canSendMail]返回YES。

    项目中没有编译器警告,没有崩溃。。。

    3 回复  |  直到 12 年前
        1
  •  2
  •   Joseph Tura    12 年前

    这是IOS4中的一个bug。

        2
  •  1
  •   Abdo    12 年前

    有些读者可能会面临这个问题:

    一定要执行 <MFMailComposeViewControllerDelegate>

    // in TestViewController.h
    @interface TestViewController : UIViewController<MFMailComposeViewControllerDelegate>
    @end
    
    // in TestViewController.m
    @interface TestViewController ()
    @end
    
    @implementation
    - (void) compose {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
    
        [picker setSubject:@"Hello there"];
    
        [picker setToRecipients:@[]];
    
        // Fill out the email body text
        NSString *emailBody = @"Hello, sending a message from my app";
    
        [picker setMessageBody:emailBody isHTML:NO];
    
        // use this function. presentModalViewController:... is deprecated
        [self presentViewController:picker animated:YES completion:nil];
    }
    
    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end
    
        3
  •  0
  •   Massimo Cafaro    14 年前

    如果看不到代码片段就很难判断,但是您应该检查以下内容:

    1) 您已正确设置 MFMailComposeViewController's

    setSubject:

    3) 您已使用设置消息正文 setMessageBody:isHTML:

    并可以选择使用 addAttachmentData:mimeType:fileName:

    [self presentModalViewController:mcvc animated:YES];
    

    希望这有帮助。