/// <summary> /// 连接到邮件服务器 /// </summary> private static void ConnectServer() { if (mail == null) { try { SmtpSection cfg = ConfigurationManager.GetSection(@"system.net/mailSettings/smtp") as SmtpSection; SmtpNetworkElement SmtpConfig = cfg.Network; mail = new MailMessage { From = new MailAddress(cfg.From) }; smtp = new SmtpClient { Host = SmtpConfig.Host, Port = Convert.ToInt32(SmtpConfig.Port), DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(SmtpConfig.UserName, SmtpConfig.Password) }; } catch (Exception ex) { Debug.WriteLine(ex.Message); } } }
/// <summary> /// Sends the email. /// </summary> /// /// <param name="fromEmail">From email.</param> /// <param name="toEmail">To email.</param> /// <param name="subject">The subject.</param> /// <param name="body">The body.</param> /// <param name="attachments">(Optional) File attatchments.</param> /// <param name="config">(Optional) Configuration.</param> public static void SendEmail(string fromEmail, string[] toEmail, string subject, string body, Dictionary <Stream, string> attachments = null, SmtpNetworkElement config = null) { //GET SMTP SETTINGS IF APPLICABLE if (config == null) { config = (ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection).Network; } //CREATE SMTP CLIENT var smtpClient = new SmtpClient(config.Host); smtpClient.EnableSsl = config.EnableSsl; smtpClient.Port = config.Port; //SET CREDENTIALS IF APPLICABLE if (!string.IsNullOrWhiteSpace(config.UserName) && !string.IsNullOrWhiteSpace(config.Password)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(config.UserName, config.Password); } //CREATE MESSAGE var message = new MailMessage() { From = new MailAddress(fromEmail), Subject = subject, Body = body, IsBodyHtml = true }; //DETERMINE EMAIL ADDRESSES TO SEND TO foreach (var item in toEmail) { message.To.Add(item); } //ATTACH DOCUMENT FROM LIBRARY IF APPLICABLE if (attachments != null && attachments.Count > 0) { foreach (var item in attachments) { message.Attachments.Add(new Attachment(item.Key, item.Value)); } } //SEND EMAIL smtpClient.Send(message); }
/// <summary> /// Uses the <c>system.net/mailSettings</c> section of the /// provided <see cref="Castle.MonoRail.Framework.Configuration"/> to populate this /// <see cref="SmtpConfig"/> /// </summary> /// <param name="configuration">The configuration.</param> public void ConfigureFromConfig(Configuration configuration) { if (configuration == null) { throw new ArgumentNullException("configuration"); } MailSettingsSectionGroup mailSettings = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; if (mailSettings != null && mailSettings.Smtp != null && mailSettings.Smtp.Network != null) { SmtpNetworkElement network = mailSettings.Smtp.Network; Host = network.Host; Password = network.Password; Username = network.UserName; Port = network.Port; } else { throw new ConfigurationErrorsException("Could not find the system.net/mailSettings/smtp/network element in the application configuration"); } }
// public static void SendEmail(string title, double p1, double p2) { SmtpSection cfg = ConfigurationManager.GetSection(@"system.net/mailSettings/smtp") as SmtpSection; SmtpNetworkElement smtElement = cfg.Network; string _FromEmail = "*****@*****.**"; string _ToEmail = "*****@*****.**"; //实例化一个邮件消息对象 MailMessage email; string displayName = ""; //email中的显示名 if (string.IsNullOrEmpty(displayName)) { email = new MailMessage(_FromEmail, _ToEmail); } else { MailAddress fromMail = new MailAddress(_FromEmail, displayName, System.Text.Encoding.UTF8); MailAddress toMail = new MailAddress(_ToEmail); email = new MailMessage(fromMail, toMail); } email.IsBodyHtml = true; email.Body = "最新低价:" + p1 + ",上次低价:" + p2; //email.Subject = "linecat提醒:您关注的商品【" + title + "】达到了历史最低价格,降价幅度:" + Math.Round((p2 - p1) / p2 * 100) + "%"; email.Subject = "《" + title + "》新低价:" + p1 + ",上次低价:" + p2 + ",降幅:" + Math.Round((p2 - p1) / p2 * 100) + "%"; email.BodyEncoding = System.Text.Encoding.UTF8; //实例化smtp客服端对象,用来发送电子邮件 System.Net.Mail.SmtpClient stmp = new SmtpClient(smtElement.Host); //设置是否需要发送是否需要身份验证,如果不需要下面的credentials是不需要的 if (smtElement.DefaultCredentials) { stmp.UseDefaultCredentials = true; stmp.Credentials = new System.Net.NetworkCredential(smtElement.UserName, smtElement.Password); } //发送邮件 stmp.Send(email); }