mono中傳送郵件並儲存本次收件人的地址
在ios端mono開發中,傳送郵件可以選擇呼叫ios原生email程式。有兩種方式實現這種功能,一是程式跳轉到ipad中email程式,另外一種是將傳送郵件的介面在自己應用裡彈出。
首先第一種方式的程式碼:
string sendEmail = "mailto:收件人郵箱地址?cc=發件人郵箱地址&subject=主題&body=郵件內容"; UIApplication.SharedApplication.OpenUrl(NSUrl.FromString(sendEmail));
這種方式會直接開啟ipad本地email程式,但這種方式有個缺點是無法傳送附件,並且會離開當前應用。
第二種方式,是可以傳送附件並且可以在本應用中進行,mono中已經有封裝好的類MFMailComposeViewController去呼叫。
如果你ipad未設定郵箱賬戶,或者你的郵箱賬戶在設定裡未顯示啟用狀態,則直接彈出此窗體會報錯。所以在呼叫程式碼的時候,要先判斷MFMailComposeViewController.CanSendMail是否為true,如果為false的話,可以通過用第一種方式把設定郵箱賬戶的介面彈出來。在傳送的時候,通過讀取儲存在xml中的收件人資訊,設定本次收件人的地址,這樣使用者就不需要每次都填寫收件人了。具體程式碼如下:
///<summary> /// 傳送郵件 /// </summary> /// <param name="subject">郵件主題.</param> /// <param name="messageBody">郵件內容</param> protected void SendEmail(string subject, string messageBody) { //首先判斷是否能傳送郵件,如果郵箱裡已經設定郵箱賬戶,並激活則為true//模擬器裡為true if (!MFMailComposeViewController.CanSendMail) { MFMailComposeViewController mf = new MFMailComposeViewController(); mf.MailComposeDelegate = new MailComposeDelegate(); //設定傳送完成後的委託事件 mf.SetSubject(subject);// 設定郵件主題 #region 獲取上次傳送郵件後儲存在xml中的收件人 XmlDocument xmlDoct = new XmlDocument(); string xmlpath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "SysConfiguration.xml"); xmlDoct.Load(xmlpath); if (xmlDoct != null) { XmlNode xmlNodeContinue = xmlDoct.SelectSingleNode("Configuration/MailReceiver"); if (xmlNodeContinue != null) { string strReceiver = xmlNodeContinue.InnerText; mf.SetToRecipients(strReceiver.Split(',')); //設定收件人 } } #endregion; mf.SetMessageBody("說明: 請下載附件進行列印 ", true); //設定郵件內容 #region 設定附件 NSData data = NSData.FromString(messageBody, NSStringEncoding.UTF8); mf.AddAttachmentData(data, @"text/html", subject + ".html"); #endregion //彈出傳送郵件窗體 this.PresentModalViewController(mf, true); } else { Tapku.TKAlertCenter.DefaultCenter.PostAlert("無發件人,請先在系統設定裡新增郵箱賬戶"); //這裡也會呼叫ipad email應用 彈出設定郵箱賬戶的窗體 string sendEmail = "mailto:[email protected]"; UIApplication.SharedApplication.OpenUrl(NSUrl.FromString(sendEmail)); } }
這時候點擊發送或者取消,將會觸發郵件完成事件,在完成的時候,通過遞迴遍歷MFMailComposeViewController 中的控制元件(均繼承自UIView),找到收件人的UITextField,儲存收件人的地址到xml。
遞迴遍歷程式碼:
///設定傳送完成後的委託事件 class MailComposeDelegate:MFMailComposeViewControllerDelegate { static string strReceiver = string.Empty; //儲存收件人的Email地址 void FindUITextField(UIView view) { if (view is UITextField) { //找到發件人的UITextField控制元件 if ((view as UITextField).Text.Contains("@")) { strReceiver = (view as UITextField).Text; } return; } if (view.Subviews.GetCount() > 0) { foreach (UIView subView in view.Subviews) { //進行遞迴遍歷 FindUITextField(subView); } } } ///傳送郵件或取消傳送後的完成事件 public override void Finished(MFMailComposeViewController controller, MFMailComposeResult result, NSError error) { } }
儲存收件人並響應完成傳送具體事件的操作實現程式碼如下:
///傳送郵件或取消傳送後的完成事件 public override void Finished(MFMailComposeViewController controller, MFMailComposeResult result, NSError error) { //如果郵件傳送成功,儲存此次收件人的資訊 if (result == MFMailComposeResult.Sent) { FindUITextField(controller.View);//遞迴得到收件人Email地址 //儲存本次發件人的地址 if (!string.IsNullOrEmpty(strReceiver)) { XmlDocument xmlDoct = new XmlDocument(); string xmlpath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "SysConfiguration.xml"); xmlDoct.Load(xmlpath); if (xmlDoct != null) { XmlNode xmlNodeContinue = xmlDoct.SelectSingleNode("Configuration/MailReceiver"); if (xmlNodeContinue != null) { xmlNodeContinue.InnerText = strReceiver; xmlDoct.Save(xmlpath); } } } } string message = string.Empty; bool flag = false; switch (result) { case MFMailComposeResult.Cancelled: message = "郵件未儲存到草稿箱"; flag = true; break; case MFMailComposeResult.Failed: message = "傳送失敗:" + error.ToString(); break; case MFMailComposeResult.Saved: message = "已儲存到草稿箱"; flag = true; break; case MFMailComposeResult.Sent: message = "傳送成功"; flag = true; break; default: break; } if (!string.IsNullOrEmpty(message)) { Tapku.TKAlertCenter.DefaultCenter.PostAlert(message); } if (flag) { //關掉髮送郵件的窗體 controller.DismissModalViewControllerAnimated(true); } } }
另外在mono中如果想讀取和修改xml,不能講xml檔案放在程式目錄資料夾裡面,要把它copy到系統個人資料夾下面,應該是蘋果許可權控制的比較死的原因。具體操作程式碼如下:每次程式啟動的時候會執行檢測檔案是否存在,不存在則拷貝到personal資料夾下面。
string SystemSetPath=System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"SysConfiguration.xml"); if(!System.IO.File.Exists(SystemSetPath)) { System.IO.File.Copy("SysConfiguration.xml",SystemSetPath); }
本次xml的結構如下:
<?xml version="1.0" encoding="UTF-8" ?> <Configuration> <MailReceiver></MailReceiver> </Configuration>
另外,附件(html格式)傳送到qq郵箱,預覽時會亂碼!在大神(部落格地址)的幫助下,解決了這個問題!解決方案如下:
然後在傳送郵件的時候,附件相關程式碼要這樣改動:
NSData data = NSData.FromString(messageBody, NSStringEncoding.UTF8); //替換為 byte[] messageBytes=System.Text.Encoding.GetEncoding("GB2312").GetBytes(messageBody); NSData data = NSData.FromArray(messageBytes);
喜歡這篇文章就推薦下吧!您的支援是我繼續的最大動力!