无法使用MFMailComposeViewController从应用程序发送电子邮件

时间:2010-11-26 13:17:29

标签: iphone cocoa-touch email mfmailcomposeviewcontroller

我正在努力尝试从我的应用发送电子邮件。 我从iCodeBlog(http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/

尝试了这段代码
-(void)sendEmail:(id)sender
{
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    mail.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail]) {
            //Setting up the Subject, recipients, and message body.
        [mail setToRecipients:[NSArray arrayWithObjects:@"myEmail@email.com",nil]];
        [mail setSubject:@"Subject of Email"];
        [mail setMessageBody:@"Message of email" isHTML:NO];
            //Present the mail view controller
        [self presentModalViewController:mail animated:YES];
    }
        //release the mail
    [mail release];
}
    //This is one of the delegate methods that handles success or failure
    //and dismisses the mail
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    [self dismissModalViewControllerAnimated:YES];
    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Failed!" message:@"Your email has failed to send" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

它说它发送电子邮件并且没有发生错误但我从未在收件箱中收到电子邮件。 我尝试将它们发送到不同的电子邮件帐户并尝试从不同的帐户发送它们,没有发生错误但我从未收到电子邮件。 有什么想法吗?

如果这很重要,当我开始输入To:email

时,我会在调试器控制台上收到此消息
  

DA |无法在/tmp/DAAccountsLoading.lock中打开锁定文件。我们无论如何都会加载帐户,但可能会发生不好的事情

=====编辑======

我刚刚意识到所有这些电子邮件都是通过Mail.app发送到我的发件箱的。点击发送时是不是会自动发送?如果没有,那么当用户按下MFMailComposeView上的发送按钮时,我该怎么做才能发送它们?或者也许打电话给Mail.app并发送这些电子邮件。

4 个答案:

答案 0 :(得分:6)

使用此代码肯定会有效:

    -(IBAction)send{

        [self callMailComposer];
    }

    -(void)callMailComposer{

        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
        {
        // We must always check whether the current device is configured for sending emails
            if ([mailClass canSendMail])
                [self displayComposerSheet];
            else
                [self launchMailAppOnDevice];
        }

        else
        {
            [self launchMailAppOnDevice];
        }
    }


    #pragma mark -
    #pragma mark Compose Mail
    #pragma mark 

    // Displays an email composition interface inside the application. Populates all the Mail fields. 
    -(void)displayComposerSheet{

        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];


        picker.mailComposeDelegate = self;
        NSString *tosubject =@"";
        [picker setSubject:tosubject];


        // Set up recipients
        [picker setCcRecipients:nil];   
        [picker setBccRecipients:nil];

        [picker setToRecipients:nil];



        [picker setMessageBody:strNewsLink isHTML:NO];

        [self presentModalViewController:picker animated:YES];

        if(picker) [picker release];
        if(picker) picker=nil;

    }


    // Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.

        - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{    
  NSString* alertMessage;
  // message.hidden = NO;
  // Notifies users about errors associated with the interface
  switch (result)
  {
    case MFMailComposeResultCancelled:
      alertMessage = @"Email composition cancelled";
      break;
    case MFMailComposeResultSaved:
      alertMessage = @"Your e-mail has been saved successfully";

      break;
    case MFMailComposeResultSent:
      alertMessage = @"Your email has been sent successfully";

      break;
    case MFMailComposeResultFailed:
      alertMessage = @"Failed to send email";

      break;
    default:
      alertMessage = @"Email Not Sent";

      break;
  }

  UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"My app name" 
                                                      message:alertMessage
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
  [alertView show];
  [self dismissModalViewControllerAnimated:YES];
}


    #pragma mark 
    #pragma mark Workaround
    #pragma mark
    // Launches the Mail application on the device.

        -(void)launchMailAppOnDevice{

        NSString *recipients = @"mailto:?cc=&subject=";
        NSString *body = @"&body=";
        NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
        email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

    }

答案 1 :(得分:1)

在这里挖掘旧线程...也许我可以在处理不发送电子邮件的MFMAilComposerViewController时节省未来的挫败感。

我的应用会在我的5个测试设备中的4个发送电子邮件,我无法理解5日的差异。问题是Gmail帐户设置不正确。 MFMailComposerViewController错误陷阱方法从未返回任何错误,它只是没有发送电子邮件。问题是电子邮件地址或电子邮件密码不正确。我通过向设备用户询问他的电子邮件登录信息来发现这一点,然后当我尝试登录他的Gmail帐户时出现错误警报。假设,是的,我的不好,是canSendMail会检查有效的电子邮件帐户......

答案 2 :(得分:0)

顺便说一下,我希望您在设备中测试此功能,就像您尝试在Simulator中运行此代码一样,它会显示已成功发送的电子邮件,但电子邮件永远不会发送。

由于

答案 3 :(得分:0)

Swift 4版

h1   h2  h3  h4  h5  h6  h7  h8

U   U   NULL    U   Y   NULL    Y   X

U   NULL    U   U   Y   Y   X   X

U   U   U   NULL    U   NULL    Y   NULL

NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

X   V   U   U   Y   NULL    Z   X

Y   X   NULL        X   Y   Z   U

X   NULL    U   NULL    NULL    U   Z   Y

NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL