iOS - 发送电子邮件

时间:2015-05-08 01:28:18

标签: ios email mfmailcomposeviewcontroller

我发送的电子邮件非常简单

#import <MessageUI/MFMailComposeViewController.h> 
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>

-(void) sendEmailto: (NSArray*)p_recipient withSubject:(NSString*)p_subject body:(NSString*)p_body andAttachment:(NSData*)p_attachment
{
MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init];
    emailComposer.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail] == YES)
    {
        [emailComposer setSubject:p_subject];
        if(p_recipient != nil)
        {
            [emailComposer setToRecipients:p_recipient];
        }
        if (p_body!= nil && [p_body isEqualToString:@""]==NO)
        {
            [emailComposer setMessageBody:p_body isHTML:NO];

        }
        if(p_attachment != nil)
        {
            [emailComposer addAttachmentData:p_attachment mimeType:@"image/jpeg" fileName:@"image.jpg"];
        }

        // Present mail view controller on screen
        [emailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:emailComposer animated:YES];
        self.sentEmailTargetController = p_target;
    }
    else
    {
        NSLog(@"Can't Open Email");
    }

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"e-mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"e-mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"e-mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"e-mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    if (![[self presentedViewController] isBeingDismissed])
    {
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
}

我在界面

中有声明的MFMailComposeViewControllerDelegate委托

在我的设备上运行它,电子邮件控制台打开,我可以写一条消息,点击发送关闭电子邮件窗口,但实际上并不发送电子邮件。方法- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 记录发送的电子邮件,但邮件未到达目的地。尝试了几个电子邮件帐户。

1 个答案:

答案 0 :(得分:1)

  

我可以写一条消息,点击发送会关闭电子邮件窗口,但实际上并没有发送电子邮件

这实际上属于规范,playground

  

使用此界面并不能保证立即发送相应的电子邮件

很多事情都可能出错,缺乏互联网连接是最明显的。当然,即使发送电子邮件,也不能保证它能够与任何人联系;其他事情可能会出错。