Summary of Several Methods of Sending e mail in IOS Development

  • 2021-12-09 10:17:40
  • OfStack

Two methods of sending Email provided by iOS system framework

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


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

The downside is obvious, such a process can cause the program to exit temporarily, and even if iOS 4.x supports multitasking, such a process will still make people feel inconvenient.

2. Use MFMailComposeViewController to realize the function of sending mail. It is in MessageUI. framework. You need to add this framework in the project and import MFMailComposeViewController. h header file in the used file.


#import <MessageUI/MFMailComposeViewController.h>; 
 
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
controller.mailComposeDelegate = self; 
[controller setSubject:@"My Subject"]; 
[controller setMessageBody:@"Hello there." isHTML:NO]; 
[self presentModalViewController:controller animated:YES]; 
[controller release]; 
// Send using this method Email Is the most general method, which has corresponding MFMailComposeViewControllerDelegate Events : 
 
- (void)mailComposeController:(MFMailComposeViewController*)controller 
   didFinishWithResult:(MFMailComposeResult)result 
      error:(NSError*)error; 
{ 
 if (result == MFMailComposeResultSent) { 
 NSLog(@"It's away!"); 
 } 
 [self dismissModalViewControllerAnimated:YES]; 
} 
// Have 1 The definitions of some related data structures are described in detail in the header file:  
 
enum MFMailComposeResult { 
 MFMailComposeResultCancelled,// User cancels editing message  
 MFMailComposeResultSaved,// The user successfully saved the message  
 MFMailComposeResultSent,// The user clicks send to put the mail in the queue  
 MFMailComposeResultFailed// User failed trying to save or send message  
}; 
typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0 Above valid  
// In the header file MFMailComposeViewController Part of the method mentioned by the way:  
 
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); 
// If the user does not set up a mail account, it will return NO You can use the return value to decide whether to use MFMailComposeViewController  Or  mailto:// Or, you can choose the traditional method mentioned above skpsmtpmessage To achieve sending Email Function of.  
- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename; 
//NSData Type of attachment Naturally, there is no need to say much about mimeType Need 1 Point description, given in the official document 1 Links http://www.iana.org/assignments/media-types/  All the types listed here should be supported. About mimeType The use of, more need to rely on search engines  =] 

The disadvantage of the second method is also obvious. The iOS system provides us with an UI in mail, but we can't align and customize it at all, which will discourage those App customized into their own style, because it is undoubtedly too abrupt to use it in this way.

3. We can customize the corresponding view according to our own UI design requirements to adapt to the overall design. It can be realized by using the famous open source SMTP protocol.

In the SKPSMTPMessage class, there are no requirements for views, but all they provide are data-level processing. You need to define the corresponding sending requirements to realize mail sending. As for how to get this information, you can determine the interaction mode and view style according to the requirements of the software.


    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; 
  testMsg.fromEmail = @"test@gmail.com"; 
  testMsg.toEmail =@"to@gmail.com"; 
  testMsg.relayHost = @"smtp.gmail.com"; 
  testMsg.requiresAuth = YES; 
  testMsg.login = @"test@gmail.com"; 
  testMsg.pass = @"test"; 
  testMsg.subject = [NSString stringWithCString:" Test " encoding:NSUTF8StringEncoding]; 
  testMsg.bccEmail = @"bcc@gmail.com"; 
  testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS! 
 
  // Only do this for self-signed certs! 
  // testMsg.validateSSLChain = NO; 
  testMsg.delegate = self; 
 
  NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, 
         [NSString stringWithCString:" Test body " 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=\"test.vcf\"",kSKPSMTPPartContentTypeKey, 
          @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; 
 
  testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; 
 
  [testMsg send]; 
// This class also provides the corresponding Delegate Method to let you know the status of sending better . 
 
-(void)messageSent:(SKPSMTPMessage *)message; 
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error; 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: