private async void OnBtnMail(object sender, RoutedEventArgs e) { ITrackingManager trackingManager = Ioc.Resolve <ITrackingManager>(); IMessageBoxService messageBoxService = Ioc.Resolve <IMessageBoxService>(); try { var email = new EmailMessage { Subject = "2Day Logs", Body = "Logs are available as attachments" }; await MailHelper.AddAttachment(email, "2day.db", "2day.db"); await MailHelper.AddAttachment(email, LogService.Filename, "logs.txt"); await EmailManager.ShowComposeNewEmailAsync(email); } catch (Exception ex) { string message = "Error while sending logs by email: " + ex; trackingManager.Exception(ex, message); messageBoxService.ShowAsync(StringResources.General_LabelError, message); } }
/// <summary> /// 异步发送邮件 /// </summary> /// <param name="isSimple">是否只发送一条</param> /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param> /// <param name="isReuse">是否重用SmtpClient</param> private void SendMessageAsync(MailHelper mail, bool isSimple, string shiyan, string msgCount, bool autoReleaseSmtp, bool isReuse) { LogHelper.Info(String.Format("{0}:{1}\"异步\"邮件开始。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine)); if (!isReuse || !mail.ExistsSmtpClient()) { SmtpClient client = new SmtpHelper(EmailConfig.TestEmailType, false, EmailConfig.TestUserName, EmailConfig.TestPassword).SmtpClient; mail.AsycUserState = String.Format("{0}:{1}\"异步\"邮件", shiyan, msgCount); client.SendCompleted += (send, args) => { AsyncCompletedEventArgs arg = args; if (arg.Error == null) { // 需要注意的事使用 MailHelper 发送异步邮件,其UserState是 MailUserState 类型 LogHelper.Info(((MailUserState)args.UserState).UserState.ToString() + "已发送完成." + watch.ElapsedMilliseconds + Environment.NewLine); } else { LogHelper.Info(String.Format("{0} 异常:{1}{2}{3}" , ((MailUserState)args.UserState).UserState.ToString() + "发送失败." , (arg.Error.InnerException == null ? arg.Error.Message : arg.Error.Message + arg.Error.InnerException.Message) , watch.ElapsedMilliseconds, Environment.NewLine)); // 标识异常已处理,否则若有异常,会抛出异常 ((MailUserState)args.UserState).IsErrorHandle = true; } }; mail.SetSmtpClient(client, autoReleaseSmtp); } else { mail.AsycUserState = String.Format("{0}:{1}\"异步\"邮件已发送完成。", shiyan, msgCount); } mail.From = EmailConfig.TestFromAddress; mail.FromDisplayName = EmailConfig.GetAddressName(EmailConfig.TestFromAddress); string to = m_to; string cc = m_cc; string bcc = m_bcc; if (to.Length > 0) { mail.AddReceive(EmailAddrType.To, to, EmailConfig.GetAddressName(to)); } if (cc.Length > 0) { mail.AddReceive(EmailAddrType.CC, cc, EmailConfig.GetAddressName(cc)); } if (bcc.Length > 0) { mail.AddReceive(EmailAddrType.Bcc, bcc, EmailConfig.GetAddressName(bcc)); } mail.Subject = m_Subject; // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件 mail.Body = m_Body + "<div style='display:none'>" + Guid.NewGuid() + "</div>"; mail.IsBodyHtml = true; if (filePaths != null && filePaths.Count > 0) { foreach (string filePath in FilePaths) { mail.AddAttachment(filePath); } } Dictionary <MailInfoType, string> dic = mail.CheckSendMail(); if (dic.Count > 0 && MailInfoHelper.ExistsError(dic)) { // 反馈“错误+提示”信息 LogHelper.Info(MailInfoHelper.GetMailInfoStr(dic)); } else { string msg = String.Empty; if (dic.Count > 0) { // 反馈“提示”信息 msg = MailInfoHelper.GetMailInfoStr(dic); } try { // 发送 if (isSimple) { mail.SendOneMail(); } else { mail.SendBatchMail(); } } catch (Exception ex) { // 反馈异常信息 LogHelper.Info(String.Format("{0}\"异步\"异常:({1}){2}{3}", msgCount, watch.ElapsedMilliseconds, ex.Message, Environment.NewLine)); } finally { // 输出到界面 if (msg.Length > 0) { LogHelper.Info(msg + Environment.NewLine); } } } mail.Reset(); }
/// <summary> /// 同步发送邮件 /// </summary> /// <param name="isSimple">是否只发送一条</param> /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param> /// <param name="isReuse">是否重用SmtpClient</param> private void SendMessage(MailHelper mail, bool isSimple, ArrayList list, Mail_Test.Model.maillist mailinfo, bool autoReleaseSmtp, bool isReuse) { mail.IsBodyHtml = true; string bcc = "";//密送 if (mailinfo.mto.Length > 0) { mail.AddReceive(EmailAddrType.To, mailinfo.mto, mailinfo.toname); } if (mailinfo.mcc.Length > 0) { mail.AddReceive(EmailAddrType.CC, mailinfo.mcc, mailinfo.ccname); } if (bcc.Length > 0) { mail.AddReceive(EmailAddrType.Bcc, bcc, Config.GetAddressName(bcc)); } mail.Subject = mailinfo.title; // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件 mail.Body = mailinfo.mailcontent; mail.From = mailinfo.mfrom; mail.FromDisplayName = mailinfo.fromname; if (!isReuse || !mail.ExistsSmtpClient()) { mail.SetSmtpClient( new SmtpHelper(Config.TestEmailType, false, Config.TestUserName, Config.TestPassword).SmtpClient , autoReleaseSmtp ); } if (list != null && list.Count > 0) { foreach (string filePath in list) { mail.AddAttachment(filePath); } } Dictionary <MailInfoType, string> dic = mail.CheckSendMail(); if (dic.Count > 0 && MailInfoHelper.ExistsError(dic)) { // 反馈“错误+提示”信息 ViewBag.msg += MailInfoHelper.GetMailInfoStr(dic); } else { string msg = String.Empty; if (dic.Count > 0) { // 反馈“提示”信息 ViewBag.msg = MailInfoHelper.GetMailInfoStr(dic); } try { if (isSimple) { mail.SendOneMail(); } else { // 发送 mail.SendBatchMail(); } } catch (Exception ex) { // 反馈异常信息 ViewBag.msg += (ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message) + Environment.NewLine; } finally { // 输出到界面 if (msg.Length > 0) { ViewBag.msg += Environment.NewLine; } } } mail.Reset(); }
/// <summary> /// 同步发送邮件 /// </summary> /// <param name="isSimple">是否只发送一条</param> /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param> /// <param name="isReuse">是否重用SmtpClient</param> private void SendMessage(MailHelper mail, bool isSimple, string shiyan, string msgCount, bool autoReleaseSmtp, bool isReuse) { AppendReplyMsg(String.Format("{0}:{1}\"同步\"邮件开始。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine)); if (!isReuse || !mail.ExistsSmtpClient()) { mail.SetSmtpClient( new SmtpHelper(Config.TestEmailType, false, Config.TestUserName, Config.TestPassword).SmtpClient , autoReleaseSmtp ); } mail.From = Config.TestFromAddress; mail.FromDisplayName = Config.GetAddressName(Config.TestFromAddress); string to = m_to; string cc = m_cc; string bcc = m_bcc; if (to.Length > 0) { mail.AddReceive(EmailAddrType.To, to, Config.GetAddressName(to)); } if (cc.Length > 0) { mail.AddReceive(EmailAddrType.CC, cc, Config.GetAddressName(cc)); } if (bcc.Length > 0) { mail.AddReceive(EmailAddrType.Bcc, bcc, Config.GetAddressName(bcc)); } mail.Subject = m_Subject; // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件 mail.Body = m_Body + Guid.NewGuid(); mail.IsBodyHtml = true; if (filePaths != null && filePaths.Count > 0) { foreach (string filePath in FilePaths) { mail.AddAttachment(filePath); } } Dictionary <MailInfoType, string> dic = mail.CheckSendMail(); if (dic.Count > 0 && MailInfoHelper.ExistsError(dic)) { // 反馈“错误+提示”信息 AppendReplyMsg(MailInfoHelper.GetMailInfoStr(dic)); } else { string msg = String.Empty; if (dic.Count > 0) { // 反馈“提示”信息 msg = MailInfoHelper.GetMailInfoStr(dic); } try { if (isSimple) { mail.SendOneMail(); } else { // 发送 mail.SendBatchMail(); } AppendReplyMsg(String.Format("{0}:{1}\"同步\"邮件已发送完成。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine)); } catch (Exception ex) { // 反馈异常信息 msg = msg + (ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message) + Environment.NewLine; } finally { // 输出到界面 if (msg.Length > 0) { AppendReplyMsg(msg + Environment.NewLine); } } } mail.Reset(); }