/// dynamic factory pattern alos can be implemented here private Type Find(MailContentType contentType) { if (_registeredImplementation.ContainsKey(contentType)) { return(_registeredImplementation[contentType]); } var assembly = Assembly.GetAssembly(typeof(IMailProvider)); var types = from type in assembly.GetTypes() where Attribute.IsDefined(type, typeof(ContentTypeAttribute)) select type; var providerType = types.Select(p => p).FirstOrDefault(t => t .GetCustomAttributes(typeof(ContentTypeAttribute), false) .Any(att => ((ContentTypeAttribute)att).Match(contentType))); if (providerType == null) { throw new HandledErrorException( "Could not find a IMailProvider implementation for given MailContentType"); } if (!_registeredImplementation.ContainsKey(contentType)) { _registeredImplementation.Add(contentType, providerType); } return(providerType); }
/// <summary> /// Returns matched MailContentType using the contentType /// </summary> /// <param name="contentType"></param> /// <returns></returns> public virtual bool Match(MailContentType contentType) { if (_contentTypes != null) { return(Array.IndexOf(_contentTypes, contentType) != -1); } return(false); }
/// <summary> /// Returns the instance of a MailContent using the given parameters /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="contentType"></param> public MailContent(string from, string to, string subject, string body, MailContentType contentType) { From = from; To = to; Subject = subject; Body = body; ContentType = contentType; }
/// <summary> /// Returns the instance of a MailContent using the given parameters /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="contentType"></param> /// <param name="scheduleAt"></param> public MailContent(string from, string to, string subject, string body, MailContentType contentType, DateTime?scheduleAt) { From = from; To = to; Subject = subject; Body = body; ContentType = contentType; ScheduleAt = scheduleAt; }
/// <summary> /// 用于转换数据类型(实际使用的) /// </summary> /// <param name="configure"></param> public virtual void Convert(Configure <T1> configure) { this.sender = configure.sender; this.receivers = configure.receivers; this.license = configure.license; this.database = configure.database; string[] noticeTimeData = configure.noticetime.Split('-'); try { this.noticeTime = new TimeSpan(int.Parse(noticeTimeData[0]), int.Parse(noticeTimeData[1]), 0); } catch (Exception ex) { ATLog.Error(ex.Message + "\n" + ex.StackTrace); } this.isDebug = configure.debug; this.isDebugSend = false;//初始化为false this.noticeRate = (NoticeRate)Enum.Parse(typeof(NoticeRate), configure.noticerate.ToUpper()); this.mailType = (MailContentType)Enum.Parse(typeof(MailContentType), configure.mailcontenttype.ToUpper()); }
/// <summary> /// 获取Github主题邮件内容 /// </summary> /// <param name="theme">关注的Github主题</param> /// <param name="type">邮件内容格式</param> /// <returns></returns> public static string GetThemeContents(string theme, MailContentType type) { ATLog.Info("获取Github关注的话题"); List <ThemeRepo> repos = JsonParserGithub.GetThemeRepos(theme); string content = ""; switch (type) { case MailContentType.TEXT: content = MailTextTemplate.CreateMailByThemeTemplate(repos); break; case MailContentType.HTML: content = MailHTMLTemplate.GetHTMLContentByTheme(repos); break; } //File.WriteAllText(Path.Combine(System.Environment.CurrentDirectory, "B.html"), content); return(content); }
/// <summary> /// 获取Github趋势邮件内容 /// </summary> /// <param name="fllowLanguage">关注何种语言趋势</param> /// <param name="type">邮件内容格式</param> /// <returns></returns> public static string GetFollowContents(string fllowLanguage, MailContentType type) { ATLog.Info("获取Github每日趋势"); List <TrendingRepo> repos = HTMLParserGitHub.Trending("daily", fllowLanguage).Result; string content = ""; switch (type) { case MailContentType.TEXT: ATLog.Info("创建Text格式的邮件模板"); content = MailTextTemplate.CreateMailTemplate(repos); break; case MailContentType.HTML: ATLog.Info("创建HTML格式的邮件模板"); content = MailHTMLTemplate.GetHTMLContentByLanguage(repos); break; } //File.WriteAllText(Path.Combine(System.Environment.CurrentDirectory, "A.html"),content); return(content); }
/// <summary> /// 使用 SendGrid 发送邮件 (Google Cloud 支持的服务之一) /// </summary> /// <param name="apiKey">Sendgrid API key.</param> /// <param name="from">From.</param> /// <param name="to"></param> /// <param name="subject">Subject.</param> /// <param name="content">Content.</param> /// <param name="contentType">Content type.</param> public static async Task MailAsync(string apiKey, string from, IEnumerable <string> to, string subject, string content, MailContentType contentType) { var sendgrid = new SendGridClient(apiKey); var message = new SendGridMessage { From = new EmailAddress(from), Personalizations = new List <Personalization> { new Personalization { Tos = to.Select(mailAddr => new EmailAddress(mailAddr)).ToList() } }, Subject = subject }; if (contentType == MailContentType.Plain) { message.PlainTextContent = content; } else { message.HtmlContent = content; } try { await sendgrid.SendEmailAsync(message); } catch (Exception ex) { ExceptionlessUtil.Warn(ex, "邮件发送失败,请检查配置"); } }
public static async Task MailAsync(string from, IEnumerable <string> to, string subject, string content, MailContentType contentType, string smtpHost, int smtpPort, string userName, string password) { var message = new MimeMessage(); message.From.Add(new MailboxAddress(from)); to.ToList().ForEach(addr => message.To.Add(new MailboxAddress(addr))); message.Subject = subject; var builder = new BodyBuilder(); if (contentType == MailContentType.Plain) { builder.TextBody = content; } else { builder.HtmlBody = content; } message.Body = builder.ToMessageBody(); try { using (var client = new SmtpClient()) { await client.ConnectAsync(smtpHost, smtpPort, true); await client.AuthenticateAsync(userName, password); await client.SendAsync(message); await client.DisconnectAsync(true); } } catch (Exception ex) { ExceptionlessUtil.Warn(ex, "邮件发送失败,请检查配置"); } }
/// <summary> /// 发送Email(强制启用SSL) /// </summary> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <param name="subject">Subject.</param> /// <param name="content">Content.</param> /// <param name="contentType">Content type.</param> /// <param name="smtpHost">Smtp host.</param> /// <param name="smtpPort">Smtp port.</param> /// <param name="userName">User name.</param> /// <param name="password">Password.</param> public static async Task MailAsync(string from, string to, string subject, string content, MailContentType contentType, string smtpHost, int smtpPort, string userName, string password) { await MailAsync(from, new List <string> { to }, subject, content, contentType, smtpHost, smtpPort, userName, password); }
public IMailProvider GetProvider(MailContentType contentType) { var providerType = Find(contentType); return(_scope.ResolveNamed <IMailProvider>(providerType.Name)); }