How can I send mail from an iPhone application

前端 未结 11 1746
甜味超标
甜味超标 2020-11-22 11:47

I want to send an email from my iPhone application. I have heard that the iOS SDK doesn\'t have an email API. I don\'t want to use the following code because it will exit my

11条回答
  •  囚心锁ツ
    2020-11-22 12:15

    To send an email from iPhone application you need to do below list of task.

    Step 1: Import #import In your controller class where you want to send an email.

    Step 2: Add the delegate to your controller like shown below

     @interface  : UIViewController 
    

    Step 3: Add below method for send email.

     - (void) sendEmail {
     // Check if your app support the email.
     if ([MFMailComposeViewController canSendMail]) {
        // Create an object of mail composer.
        MFMailComposeViewController *mailComposer =      [[MFMailComposeViewController alloc] init];
        // Add delegate to your self.
        mailComposer.mailComposeDelegate = self;
        // Add recipients to mail if you do not want to add default recipient then remove below line.
        [mailComposer setToRecipients:@[]];
        // Write email subject.
        [mailComposer setSubject:@“”];
        // Set your email body and if body contains HTML then Pass “YES” in isHTML.
        [mailComposer setMessageBody:@“” isHTML:NO];
        // Show your mail composer.
        [self presentViewController:mailComposer animated:YES completion:NULL];
     }
     else {
     // Here you can show toast to user about not support to sending email.
    }
    }
    

    Step 4: Implement MFMailComposeViewController Delegate

    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
    [controller dismissViewControllerAnimated:TRUE completion:nil];
    
    
    switch (result) {
       case MFMailComposeResultSaved: {
        // Add code on save mail to draft.
        break;
    }
    case MFMailComposeResultSent: {
        // Add code on sent a mail.
        break;
    }
    case MFMailComposeResultCancelled: {
        // Add code on cancel a mail.
        break;
    }
    case MFMailComposeResultFailed: {
        // Add code on failed to send a mail.
        break;
    }
    default:
        break;
    }
    }
    

提交回复
热议问题