準(zhǔn)備工作:
加入MessageUI.framework:
頭文件:<MessageUI/MessageUI.h>
#import <MessageUI/MessageUI.h>
協(xié)議:MFMailComposeViewControllerDelegate
<UIAlertViewDelegate,MFMailComposeViewControllerDelegate>
確認(rèn)發(fā)送的提示UIAlertView:
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"發(fā)到郵箱" message:@"請輸入郵箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確認(rèn)", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];//“輸入文本”類型
//獲取輸入文本框
UITextField * textNameTF = [alertV textFieldAtIndex:0];
textNameTF.keyboardType = UIKeyboardTypeEmailAddress; //郵箱鍵盤
textNameTF.placeholder = @"請輸入郵箱地址";
//看本地之前是否已經(jīng)存儲 (NSUserDefaults)
textNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"]?[[NSUserDefaults standardUserDefaults] objectForKey:@"email"]:@"";
textNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertV show];
UIAlertView的代理:
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//NSLog(@"%ld",buttonIndex);
if (buttonIndex == 1) { //確認(rèn)按鈕
UITextField * toEmialTF = [alertView textFieldAtIndex:0];
static NSString * const Regex_Email = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; //驗(yàn)證 郵箱號
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex_Email];
if ([predicate evaluateWithObject:toEmialTF.text] ) {
//是郵箱
[self sendByEmailWith:toEmialTF.text andTitle:@""];//可獲取郵箱標(biāo)題
} else {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.label.text = @"請輸入正確格式的郵箱";
hud.margin = 10.f;
hud.offset = CGPointMake(0, 0.f);
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:YES afterDelay:1.5f];
}
}
}
發(fā)送郵件操作:
#pragma mark - 發(fā)送到郵箱
-(void)sendByEmailWith:(NSString *)toEmailStr andTitle:(NSString *)titleStr {
// 郵件服務(wù)器
MFMailComposeViewController * mailCompose = [[MFMailComposeViewController alloc] init];
[mailCompose setMailComposeDelegate:self]; // 代理
[mailCompose setSubject:@"郵件主題"];// 設(shè)置郵件主題
[mailCompose setToRecipients:@[toEmailStr] ];// 設(shè)置收件人 (數(shù)組)
[mailCompose setCcRecipients:@[] ];// 設(shè)置抄送人 (數(shù)組)
[mailCompose setBccRecipients:@[] ];// 設(shè)置密抄送 (數(shù)組)
/** 設(shè)置郵件的正文內(nèi)容 */
NSString * emailContent = @"郵件內(nèi)容";
// 是否為HTML格式
[mailCompose setMessageBody:emailContent isHTML:NO];
// 如使用HTML格式病曾,則為以下代碼
//[mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World熄诡!</p></body></html>" isHTML:YES];
/** 添加附件 :文件 ?? NSData */
/** A.發(fā)送圖片 */
UIImage * image = [UIImage imageNamed:@"chx.jpg"];
NSData *imageData = UIImagePNGRepresentation(image); //圖片較大(畫質(zhì)高)
//NSData *imageData = UIImageJPEGRepresentation(image, 1.0);//圖片較小(畫質(zhì)低)
[mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.jpg"];//郵件顯示的文件名
/** B.發(fā)送文檔 */
//NSString * pathStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"];
//NSString * fileStr = [pathStr stringByAppendingPathComponent:@"藍(lán)牙設(shè)備iOS SDK使用文檔 -3.pdf"];
//NSData * data = [NSData dataWithContentsOfFile:fileStr];//保存的數(shù)據(jù)
//[mailCompose addAttachmentData:data mimeType:@"" fileName:@"藍(lán)牙設(shè)備iOS SDK使用文檔.pdf"];//郵件顯示的文件名
// 彈出郵件發(fā)送界面
if (mailCompose) { //如果沒有設(shè)置郵件帳戶逼裆,mailController為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:{ // 用戶取消編輯
NSLog(@"Mail send canceled...");
}break;
case MFMailComposeResultSaved:{ // 用戶保存郵件
NSLog(@"Mail saved...");
}break;
case MFMailComposeResultSent:{ // 用戶點(diǎn)擊發(fā)送
NSLog(@"Mail sent...");
}break;
case MFMailComposeResultFailed:{ // 用戶嘗試保存或發(fā)送郵件失敗
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.5f];
}break;
}
// 關(guān)閉郵件發(fā)送視圖
[self dismissViewControllerAnimated:YES completion:nil];
}
操作:
- 1.填入發(fā)送至的郵箱
可能出現(xiàn)狀況:提示沒有系統(tǒng)的郵箱賬戶
- 2.添加、打開 系統(tǒng)的郵箱賬戶
添加郵箱賬戶俱病、打開郵件開關(guān)
3.發(fā)送郵件
往工程里,拖入的圖片文件:
編輯郵件內(nèi)容:
4.點(diǎn)擊“發(fā)送”后袱结,控制臺輸出:
5.發(fā)送成功亮隙,接收郵件:
接收到的郵件:
代碼地址:demo傳送門
很久沒寫簡書了,主要是簡書編輯器的bug9讣小R缥恰!9促王!
身為一個(gè)Coder,看到bug噪漾,就渾身難受E鹋椤!P琅稹L夂病!
關(guān)鍵是還解決不了!
自己就跟工作人員交流了一下下诈胜。豹障。
而現(xiàn)如今還是不能使用“
</br>
”: 真心有點(diǎn)傷心 ??
希望能快快恢復(fù)!??????????????????????????
goyohol's essay