用MFMailComposeViewController从应用程

用MFMailComposeViewController从应用程

本文介绍了使用MFMailComposeViewController从应用程序发送电子邮件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从我的应用程式传送电子邮件时遇到了一些困难。
我试过这个代码从iCodeBlog(http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/)

I'm having some hard time trying to send an email from my app.I tried this code from 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:@"[email protected]",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];
    }
}

它说它发送电子邮件,没有错误,我的收件箱中的电子邮件。
我试图将它们发送到不同的电子邮件帐户,并尝试从不同的帐户发送,也没有错误发生,但我从来没有收到电子邮件。
任何想法?

It says it send the email and no error occurs but I never get the email in my inbox.I tried sending them to different email accounts and tried sending them from different accounts as well, no error occurs but I never get the email.Any ideas?

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

If it's important, I get this message on the Debugger Console when I start typing the To: email

============== ===================== EDIT ========================== ============
我刚刚意识到,所有这些电子邮件都发送到我的发件箱Mail.app。当我点击发送时,他们不是自动发送的吗?如果没有,那么当用户按下MFMailComposeView上的发送按钮时,我可以做什么来发送它们?

===================================== EDIT ======================================I just realized that all those emails were sent to my Outbox on Mail.app. Aren't they sent automatically when I click send? If not, then what can I do to make them be sent when the user presses the Send button on MFMailComposeView? Or perhaps call the Mail.app and make those emails be sent.

推荐答案

使用这段代码肯定会有效:

Use this code this will definitely work:

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

    }

这篇关于使用MFMailComposeViewController从应用程序发送电子邮件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:05