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

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

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

如果它很重要,当我开始输入“收件人:电子邮件”时,我会在调试器控制台上收到此消息

DA|Could not open the lock file at /tmp/DAAccountsLoading.lock. We'll load the accounts anyway, but bad things may happen

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

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

最佳答案

使用这个代码肯定会起作用:

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

    }

关于iphone - 无法使用 MFMailComposeViewController 从应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285597/

相关文章:

iphone - 我是否正确使用 CocosDenshion?

android - 用于移动应用程序的 PhoneGap

iphone - 如何使用 +fontWithName :size: and specify both the font family name and the specific style information for the font? 创建字体

ios - 使用不同的起点条目快速混合两个音频文件

java - 如何使用 commons mail 1.3 在正文中嵌入图像

iphone - 如何获取接口(interface)中定义的对象数组的计数?

cocoa-touch - NSUserDefaults 问题

iOS - 增加 AFHTTPRequestOperationManager 的超时

用于按线程将电子邮件分组在一起的 Java 库?

javascript - 联系表单显示成功消息但不发送电子邮件