internal SmtpNetworkElementInternal(SmtpNetworkElement element) { this.host = element.Host; this.port = element.Port; if (element.DefaultCredentials) { this.credential = (NetworkCredential)CredentialCache.DefaultCredentials; } else if (element.UserName != null && element.UserName.Length > 0) { this.credential = new NetworkCredential(element.UserName, element.Password); } }
internal SmtpNetworkElementInternal(SmtpNetworkElement element) { this.host = element.Host; this.port = element.Port; this.targetname = element.TargetName; this.clientDomain = element.ClientDomain; this.enableSsl = element.EnableSsl; if (element.DefaultCredentials) { this.credential = (NetworkCredential) CredentialCache.DefaultCredentials; } else if ((element.UserName != null) && (element.UserName.Length > 0)) { this.credential = new NetworkCredential(element.UserName, element.Password); } }
internal SmtpNetworkElementInternal(SmtpNetworkElement element) { this.host = element.Host; this.port = element.Port; this.targetname = element.TargetName; this.clientDomain = element.ClientDomain; this.enableSsl = element.EnableSsl; if (element.DefaultCredentials) { this.credential = (NetworkCredential)CredentialCache.DefaultCredentials; } else if ((element.UserName != null) && (element.UserName.Length > 0)) { this.credential = new NetworkCredential(element.UserName, element.Password); } }
static EmailUtils() { cfg = ConfigurationManager.GetSection(@"system.net/mailSettings/smtp") as SmtpSection; smtElement = cfg.Network; }
public static void Initialize() { _continue = true; _log = LogManager.GetLogger(typeof(Program)); XmlConfigurator.Configure(); _log.Info("Logging initialized"); //Set our thread priority to low Thread.CurrentThread.Priority = ThreadPriority.Lowest; _log.Info("Thread priority adjusted"); SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true); _log.Info("Exit handler initialized"); Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); SmtpSection imap = configFile.GetSection("imap") as SmtpSection; _mailValues = imap.Network; _log.Info("e-mail settings inialized"); EmailManager.Instance.HttpHost = ConfigurationManager.AppSettings["TargetHttpHost"].ToString(); _log.Info("e-mail manager inialized"); _emailClient = new Imap(); _ctxManager = new ObjectContextManager(); Provider.DbCtx = _ctxManager; _log.Info("InsideWord Database object context manager initialized"); _idleTime = new TimeSpan(0, 20, 0); _log.Info("Initializing idle time to "+_idleTime.TotalMinutes+" minutes."); }
/// <summary> /// Method to Send mail with images /// </summary> /// <param name="from">From.</param> /// <param name="friendlyName">Name of the friendly.</param> /// <param name="to">To.</param> /// <param name="subject">The subject.</param> /// <param name="bodyHMTL">The body HMTL.</param> /// <param name="cids">The cids.</param> /// <param name="imagesPath">The images path.</param> /// <param name="smtpNetworkSettings"></param> /// <param name="errorMessage">The error message.</param> /// <returns></returns> public static Boolean SendMailWithImages(String from, String friendlyName, String to, String subject, String bodyHMTL, List<String> cids, List<String> imagesPath, SmtpNetworkElement smtpNetworkSettings, out String errorMessage) { try { var objMail = new MailMessage(); // Add To objMail.To.Add(to); // Add Subject objMail.Subject = subject; // Add Images in Body HTML if (cids.Count > 0) { //then we create the Html part //to embed images, we need to use the prefix 'cid' in the img src value //the cid value will map to the Content-Id of a Linked resource. //thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo' AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyHMTL, null, "text/html"); for (int i = 0; i < cids.Count; i++) { //create the LinkedResource (embedded image) var img = new LinkedResource(imagesPath[i]) { ContentId = cids[i] }; //add the LinkedResource to the appropriate view htmlView.LinkedResources.Add(img); } objMail.AlternateViews.Add(htmlView); } objMail.IsBodyHtml = true; objMail.Body = bodyHMTL; // Information from Web.Config var configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.Url.AbsolutePath);//System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; var port = 25; var host = String.Empty; var password = String.Empty; var userName = String.Empty; var defaultCredentials = false; var enableSsl = false; if (smtpNetworkSettings != null) { port = smtpNetworkSettings.Port; host = smtpNetworkSettings.Host; password = smtpNetworkSettings.Password; userName = smtpNetworkSettings.UserName; enableSsl = smtpNetworkSettings.EnableSsl; } else if (mailSettings != null) { port = mailSettings.Smtp.Network.Port; host = mailSettings.Smtp.Network.Host; password = mailSettings.Smtp.Network.Password; userName = mailSettings.Smtp.Network.UserName; defaultCredentials = mailSettings.Smtp.Network.DefaultCredentials; enableSsl = mailSettings.Smtp.Network.EnableSsl; } // Add From if (String.IsNullOrEmpty(from)) { if (mailSettings != null) { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(mailSettings.Smtp.From, friendlyName) : new MailAddress(mailSettings.Smtp.From); } else if (smtpNetworkSettings != null) { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(smtpNetworkSettings.UserName, friendlyName) : new MailAddress(smtpNetworkSettings.UserName); } } else { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(from, friendlyName) : new MailAddress(from); } var smtp = new SmtpClient { Port = port, Host = host, EnableSsl = enableSsl, UseDefaultCredentials = defaultCredentials, Credentials = new NetworkCredential(userName, password) }; smtp.Send(objMail); errorMessage = "Message send Successfully!"; return true; } catch (SmtpFailedRecipientException ex) { //HttpContext.Current.AddError(ex); // Comentado Devido ao HTTPS do Handler SendMessage > M.L. 14/05/2012 errorMessage = ex.Message; return false; } catch (SmtpException ex) { //HttpContext.Current.AddError(ex); // Comentado Devido ao HTTPS do Handler SendMessage > M.L. 14/05/2012 errorMessage = ex.Message; return false; } }
/// <summary> /// Method to Send Basic Mail /// <seealso cref="http://www.systemnetmail.com/"/> /// </summary> /// <param name="from">De:</param> /// <param name="friendlyName">Joselito Sem ...</param> /// <param name="to">Para:</param> /// <param name="subject">Assunto:</param> /// <param name="bodyHMTL">Corpo em HTML:</param> /// <param name="smtpNetworkSettings"></param> /// <param name="errorMessage">The Error Message</param> /// <returns></returns> public static Boolean SendMail(String from, String friendlyName, String to, String subject, String bodyHMTL, SmtpNetworkElement smtpNetworkSettings, out String errorMessage) { try { var objMail = new MailMessage(); // Add To objMail.To.Add(to); // Add Subject objMail.Subject = subject; //// First we create the Plain Text Part //AlternateView plainView = AlternateView.CreateAlternateViewFromString("Este é um texto sem conteúdo, vistos por aqueles clientes que não suportam html", null, "text/plain"); //objMail.AlternateViews.Add(plainView); objMail.IsBodyHtml = true; objMail.Body = bodyHMTL; // Information from Web.Config var configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.Url.AbsolutePath);//System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; var port = 25; var host = String.Empty; var password = String.Empty; var userName = String.Empty; var defaultCredentials = false; var enableSsl = false; if (smtpNetworkSettings != null && !String.IsNullOrWhiteSpace(smtpNetworkSettings.Host)) { port = smtpNetworkSettings.Port; host = smtpNetworkSettings.Host; password = smtpNetworkSettings.Password; userName = smtpNetworkSettings.UserName; enableSsl = smtpNetworkSettings.EnableSsl; } else if (mailSettings != null) { port = mailSettings.Smtp.Network.Port; host = mailSettings.Smtp.Network.Host; password = mailSettings.Smtp.Network.Password; userName = mailSettings.Smtp.Network.UserName; defaultCredentials = mailSettings.Smtp.Network.DefaultCredentials; enableSsl = mailSettings.Smtp.Network.EnableSsl; } // Add From if (String.IsNullOrEmpty(from)) { if (mailSettings != null) { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(mailSettings.Smtp.From, friendlyName) : new MailAddress(mailSettings.Smtp.From); } else if (smtpNetworkSettings != null) { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(smtpNetworkSettings.UserName, friendlyName) : new MailAddress(smtpNetworkSettings.UserName); } } else { objMail.From = !String.IsNullOrEmpty(friendlyName) ? new MailAddress(from, friendlyName) : new MailAddress(from); } var smtp = new SmtpClient { Port = port, Host = host, EnableSsl = enableSsl, UseDefaultCredentials = defaultCredentials, Credentials = new NetworkCredential(userName, password) }; smtp.Send(objMail); errorMessage = "Message sent Successfully!"; return true; } catch (SmtpException ex) { errorMessage = ex.Message; return false; } }