5

我在尝试从我的应用程序发送电子邮件时遇到了一些困难。我从 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 并发送这些电子邮件。

4

4 回答 4

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

    }
于 2010-11-26T13:38:02.393 回答
1

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

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

于 2015-06-06T21:26:40.013 回答
0

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

谢谢

于 2010-11-26T13:32:01.427 回答
0

斯威夫特 4 版本

    let myController:MFMailComposeViewController = MFMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        myController.mailComposeDelegate = self
        myController.setToRecipients(["example@example.com"])
        self.present(myController, animated: true, completion: nil)
    }
于 2018-01-18T07:18:38.577 回答