SoFunction
Updated on 2025-04-03

Summary of several methods of sending e-mail in IOS development

Two methods of sending emails provided by the iOS system framework

1. Use openURL to realize the function of sending emails:

NSString *url = [NSString stringWithString: @"mailto:foo@example.
com?cc=bar@&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]; 
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 

The disadvantages are obvious. Such a process will cause the program to temporarily exit, and even if iOS supports multitasking, such a process will still make people feel that it is not very convenient.

2. Use MFMailComposeViewController to implement the function of sending emails. It is in which you need to add this framework to the project and import the header file in the file you are using.

#import <MessageUI/>; 
 
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
 = self; 
[controller setSubject:@"My Subject"]; 
[controller setMessageBody:@"Hello there." isHTML:NO]; 
[self presentModalViewController:controller animated:YES]; 
[controller release]; 
//The most common method is to use this method to implement sending Email. This method has the corresponding MFMailComposeViewControllerDelegate event: 
- (void)mailComposeController:(MFMailComposeViewController*)controller 
   didFinishWithResult:(MFMailComposeResult)result 
      error:(NSError*)error; 
{ 
 if (result == MFMailComposeResultSent) { 
 NSLog(@"It's away!"); 
 } 
 [self dismissModalViewControllerAnimated:YES]; 
} 
//There are some relevant data structure definitions that are described in the header file: 
enum MFMailComposeResult { 
 MFMailComposeResultCancelled,//User cancels editing email MFMailComposeResultSaved,//The user saves the email successfully MFMailComposeResultSent,//The user clicks to send and put the email in the queue MFMailComposeResultFailed//User failed to try to save or send email}; 
typedef enum MFMailComposeResult MFMailComposeResult; //Valid above iOS 3.0//Part of the methods of MFMailComposeViewController in the header file are mentioned by the way: 
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); 
//If the user does not set up an email account, it will return NO. You can use the traditional method of using MFMailComposeViewController or mailto:// based on the return value, or you can choose the skpsmtpmssage mentioned above to implement the function of sending email.- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename; 
//Naturally there is no need to say more about NSData type attachment. There is a link /assignments/media-types/ in the official document. All types listed here should be supported.  Regarding the use of mimeType, you need to rely on search engines more =]

The disadvantages of the second method are also very obvious. The iOS system provides us with a UI in mail, but we are completely unable to align and customize, which will discourage those apps that customize into their own style, because it is undoubtedly too abrupt to use in this way.

3. We can customize the corresponding views according to our UI design needs to adapt to the overall design. It can be implemented using the more famous open source SMTP protocol.

In the SKPSMTPMessage class, there is no requirement for the view. It provides data-level processing. You need to define the corresponding sending requirements to realize email sending. As for how to obtain this information, you can determine the interaction method and view style according to the needs of the software.

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; 
   = @"test@"; 
   =@"to@"; 
   = @""; 
   = YES; 
   = @"test@"; 
   = @"test"; 
   = [NSString stringWithCString:"test" encoding:NSUTF8StringEncoding]; 
   = @"bcc@"; 
   = YES; //  doesn't work without TLS! 
 
  // Only do this for self-signed certs! 
  //  = NO; 
   = self; 
 
  NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, 
         [NSString stringWithCString:"Test Text" encoding:NSUTF8StringEncoding],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; 
 
   NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"]; 
   NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath]; 
 
   NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"\"",kSKPSMTPPartContentTypeKey, 
          @"attachment;\r\n\tfilename=\"\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; 
 
   = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; 
 
  [testMsg send]; 
//This class also provides corresponding Delegate methods to enable you to better understand the sending status. 
-(void)messageSent:(SKPSMTPMessage *)message; 
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error; 

Thank you for reading, I hope it can help you. Thank you for your support for this site!