【问题标题】:Not able to send email from an app with MFMailComposeViewController无法使用 MFMailComposeViewController 从应用程序发送电子邮件
【发布时间】:2010-11-26 13:17:29
【问题描述】:

我在尝试从我的应用程序发送电子邮件时遇到了一些困难。 我从 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]) { //设置主题、收件人和邮件正文。 [mail setToRecipients:[NSArray arrayWithObjects:@"myEmail@email.com",nil]]; [mail setSubject:@"邮件主题"]; [mail setMessageBody:@"邮件的邮件" isHTML:NO]; //显示邮件视图控制器 [自我presentModalViewController:邮件动画:是]; } //释放邮件 【邮件发布】; } //这是处理成功或失败的委托方法之一 //并关闭邮件 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result 错误:(NSError*)error { [自我dismissModalViewControllerAnimated:是]; 如果(结果 == MFMailComposeResultFailed){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息失败!" message:@"您的邮件发送失败" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [警报显示]; [警报发布]; } }

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

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

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

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

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

【问题讨论】:

    标签: iphone cocoa-touch email mfmailcomposeviewcontroller


    【解决方案1】:

    使用此代码肯定会起作用:

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

    【讨论】:

    • 代码看起来很乱,但我已经在我的许多应用程序中实现了邮件编写器,这工作得很好
    • 在代码看起来很乱的情况下,您可能在未显示为代码的行之前没有必要的 4 个空格。
    • 感谢 Chintan Patel - stackoverflow.com/q/1494626/189804 - 提供原始代码。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      谢谢

      【讨论】:

      • 是的,我正在我的设备上测试它。它说电子邮件已发送,但它不在我的收件箱中......
      【解决方案4】:

      Swift 4 版本

          let myController:MFMailComposeViewController = MFMailComposeViewController()
          if MFMailComposeViewController.canSendMail() {
              myController.mailComposeDelegate = self
              myController.setToRecipients(["example@example.com"])
              self.present(myController, animated: true, completion: nil)
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        • 1970-01-01
        • 2015-03-26
        • 2014-08-11
        • 2016-02-01
        • 2012-09-17
        相关资源
        最近更新 更多