Preparations:

joinMessageUI.framework: The header file:<MessageUI/MessageUI.h>

#import <MessageUI/MessageUI.h>
Copy the code

Protocol: MFMailComposeViewControllerDelegate

<UIAlertViewDelegate,MFMailComposeViewControllerDelegate>
Copy the code


Confirm sending prompt UIAlertView:

UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"发到邮箱" message:@"请输入邮箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];//“输入文本”类型

//获取输入文本框
UITextField * textNameTF = [alertV textFieldAtIndex:0];
textNameTF.keyboardType = UIKeyboardTypeEmailAddress; //邮箱键盘
textNameTF.placeholder = @"请输入邮箱地址";
//看本地之前是否已经存储 (NSUserDefaults)
textNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"]?[[NSUserDefaults standardUserDefaults] objectForKey:@"email"]:@"";
textNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;

[alertV show];
Copy the code

UIAlertView proxy:

#pragma mark - UIAlertViewDelegate -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  { //NSLog(@"%ld",buttonIndex); If (buttonIndex == 1) {UITextField * toEmialTF = [alertView textFieldAtIndex:0]; The static nsstrings * const Regex_Email = @ "[A - Z0-9 a-z. + - _ %] + @ [A - Za - Z0-9. -] + \ \. [A Za - z] {2, 4}"; // Predicate = [predicateWithFormat:@"SELF MATCHES %@",Regex_Email]; // predicateWithFormat:@"SELF MATCHES %@",Regex_Email]; If ([predicate evaluateWithObject: toEmialTF. Text]) {/ / mail/self sendByEmailWith: toEmialTF. Text andTitle: @ ""]. Else {MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeText; Hud. Label. text = @" Please enter a correctly formatted mailbox "; hud.margin = 10.f; hud.offset = CGPointMake(0, 0.f); hud.removeFromSuperViewOnHide = YES; [hud hideAnimated: YES afterDelay: 1.5 f]; }}}Copy the code

Sending an email:

#pragma mark - send to mailbox -(void)sendByEmailWith:(NSString *)toEmailStr andTitle:(NSString *)titleStr {// mail server MFMailComposeViewController * mailCompose = [[MFMailComposeViewController alloc] init]; [mailCompose setMailComposeDelegate:self]; // proxy [mailCompose setSubject:@" mail subject "]; // Set the email theme [mailCompose setToRecipients:@[toEmailStr]]; MailCompose setCcRecipients:@[]]; MailCompose setBccRecipients:@[]]; // Set BCC (array) /** Set the body of the message */ NSString * emailContent = @" message content "; / / whether to HTML format [mailCompose setMessageBody: emailContent isHTML: NO]; //[mailCompose setMessageBody:@"< HTML ><body><p>Hello</p><p>World! </p></body></ HTML >" isHTML:YES]; /** Add attachment: file ➡️ NSData */ /** A. */ UIImage * image = [UIImage imageNamed:@"chx.jpg"]; NSData *imageData = UIImagePNGRepresentation(image); / / picture (high quality) / / larger NSData * imageData = UIImageJPEGRepresentation (image, 1.0); / small/picture (low quality) [mailCompose addAttachmentData: imageData mimeType: @ "" fileName: @" custom. JPG "]. //NSString * pathStr = [NSHomeDirectory()) stringByAppendingPathComponent:@"Documents/localFile"]; / / nsstrings * fileStr = [pathStr stringByAppendingPathComponent: @ "bluetooth devices use iOS SDK documentation - 3. PDF"]. //NSData * data = [NSData dataWithContentsOfFile:fileStr]; //[mailCompose addAttachmentData:data mimeType:@"" fileName:@" "bluetooth device iOS SDK usage document.pdf "]; If (mailCompose) {// If no mail account is configured, MailController is nil [self presentViewController: mailCompose animated: YES completion: nil]; } } #pragma mark - MFMailComposeViewControllerDelegate - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { switch(result) { case MFMailComposeResultCancelled: {/ / users cancel editing NSLog (@ "Mail send canceled..." ); }break; Case MFMailComposeResultSaved: {/ / the user to save the Mail NSLog (@ "Mail saved..." ); }break; Case MFMailComposeResultSent:{// NSLog(@"Mail sent... ); }break; Case MFMailComposeResultFailed: {/ / user tries to save or send Mail failure NSLog (@ "Mail send errored: % @..." , [error localizedDescription]); MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.label.text = [error localizedDescription]; hud.margin = 10.f; hud.offset = CGPointMake(0, 0.f); hud.removeFromSuperViewOnHide = YES; [hud hideAnimated: YES afterDelay: 1.5 f]; }break; } / / off mailing view [self dismissViewControllerAnimated: YES completion: nil]; }Copy the code

Operation:

  • 1. Enter an email address

Possible condition: No system email account is displayed

  • 2. Add or open an email account

Add an email account and enable the email function

3. Send emails

Drag image files into the project:

Edit email content:

4. After clicking “Send”, the console output:

5. The email is sent successfully and received:

Mail Received:

Code address: Demo portal




(2017.08.23)

goyohol’s essay