SoFunction
Updated on 2025-04-13

Simple examples of sending emails and SMS in iOS development


- (void)didClickSendEmailButtonAction{ 
 
    if ([MFMailComposeViewController canSendMail] == YES) { 
         
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; 
// Set up a proxy (unlike the past proxy, it is not "delegate", you must not forget it, there are 3 steps for proxy)
        = self; 
// recipient
        NSArray *sendToPerson = @[@"humingtao2014@"]; 
        [mailVC setToRecipients:sendToPerson]; 
// CC
        NSArray *copyToPerson = @[@"humingtao2013@"]; 
        [mailVC setCcRecipients:copyToPerson]; 
//  Secret delivery
        NSArray *secretToPerson = @[@"563821250@"]; 
        [mailVC setBccRecipients:secretToPerson]; 
// theme
        [mailVC setSubject:@"hello world"]; 
        [self presentViewController:mailVC animated:YES completion:nil]; 
[mailVC setMessageBody:@"Chigeng, hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
    }else{ 
     
NSLog(@"This device does not support email sending");
     
    } 
 

 
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ 
 
    switch (result) { 
        case MFMailComposeResultCancelled: 
NSLog(@"Cancel Send");
            break; 
        case MFMailComposeResultFailed: 
NSLog(@"Send failed");
            break; 
        case MFMailComposeResultSaved: 
NSLog(@"Save Draft File");
            break; 
        case MFMailComposeResultSent: 
NSLog(@"Send successfully");
            break; 
        default: 
            break; 
    } 
     
    [self dismissViewControllerAnimated:YES completion:nil]; 
}  
 
//  The system sends, the simulator does not support it, you need to use the real machine to test it.
- (void)didClickSendSystemEmailButtonAction{ 
 
    NSURL *url = [NSURL URLWithString:@"humingtao2014@"]; 
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) { 
         
        [[UIApplication sharedApplication] openURL:url];  
      
    }else{ 
     
NSLog(@"This device does not support");
    }