在iOS应用中发送电子邮件

pro*_*ner 0 email-integration objective-c ios swift mfmailcomposeviewcontroller

我希望能够构建一个简单的电子邮件客户端来发送电子邮件.

我查看了使用MessageUI框架构建电子邮件客户端的IOS编程101教程,但我想在我的应用程序中实现的内容略有不同.

我想要实现的是,当我按下按钮时,预先组合的电子邮件(将由我编写,例如,像YOLO一样)将直接发送给收件人.

无论如何我能做到这一点吗?

PS:我根本不需要Mail ComposeViewController,只是一个发送预合成邮件的简单按钮.

非常感谢

问候

Mac*_*Dev 10

导入" MessageUI.h"并MFMailComposeViewControllerDelegate在视图控制器中实现" "委托.

#import <MessageUI/MessageUI.h> 

interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate


- (void)sendEmail {
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"Test Subject!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"support@test.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

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

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)