public static EMailSettings GetEMailSettings()
        {
            HttpContext context = HttpContext.Current;

            EMailSettings mailSettings = new EMailSettings();

            //parse web.config as XML because of medium trust issues
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(context.Server.MapPath("~/Web.config"));

            XmlElement xmlMailSettings = xDoc.SelectSingleNode("//system.net/mailSettings/smtp") as XmlElement;

            if (xmlMailSettings != null)
            {
                if (xmlMailSettings.Attributes["from"] != null)
                {
                    mailSettings.ReturnAddress = xmlMailSettings.Attributes["from"].Value;
                }
                if (xmlMailSettings.Attributes["deliveryMethod"] != null && xmlMailSettings.Attributes["deliveryMethod"].Value.ToLowerInvariant() == "network")
                {
                    mailSettings.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (xmlMailSettings.HasChildNodes)
                    {
                        XmlNode xmlNetSettings = xmlMailSettings.SelectSingleNode("//system.net/mailSettings/smtp/network");
                        if (xmlNetSettings != null && xmlNetSettings.Attributes["password"] != null)
                        {
                            mailSettings.MailUserName   = xmlNetSettings.Attributes["userName"].Value;
                            mailSettings.MailPassword   = xmlNetSettings.Attributes["password"].Value;
                            mailSettings.MailDomainName = xmlNetSettings.Attributes["host"].Value;
                        }
                    }
                }
            }

            if (String.IsNullOrEmpty(mailSettings.MailDomainName))
            {
                mailSettings.MailDomainName = context.Request.ServerVariables["SERVER_NAME"];
            }

            if (String.IsNullOrEmpty(mailSettings.ReturnAddress))
            {
                mailSettings.ReturnAddress = "no-reply@" + mailSettings.MailDomainName;
            }

            return(mailSettings);
        }
示例#2
0
        public static EMailSettings GetEMailSettings()
        {
            HttpContext context = HttpContext.Current;

            EMailSettings mailSettings = new EMailSettings();

            //parse web.config as XML because of medium trust issues
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(context.Server.MapPath("~/Web.config"));

            XmlElement xmlMailSettings = xDoc.SelectSingleNode("//system.net/mailSettings/smtp") as XmlElement;

            if (xmlMailSettings != null) {
                if (xmlMailSettings.Attributes["from"] != null) {
                    mailSettings.ReturnAddress = xmlMailSettings.Attributes["from"].Value;
                }
                if (xmlMailSettings.Attributes["deliveryMethod"] != null && xmlMailSettings.Attributes["deliveryMethod"].Value.ToLowerInvariant() == "network") {
                    mailSettings.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (xmlMailSettings.HasChildNodes) {
                        XmlNode xmlNetSettings = xmlMailSettings.SelectSingleNode("//system.net/mailSettings/smtp/network");
                        if (xmlNetSettings != null && xmlNetSettings.Attributes["password"] != null) {
                            mailSettings.MailUserName = xmlNetSettings.Attributes["userName"].Value;
                            mailSettings.MailPassword = xmlNetSettings.Attributes["password"].Value;
                            mailSettings.MailDomainName = xmlNetSettings.Attributes["host"].Value;
                        }
                    }
                }
            }

            if (String.IsNullOrEmpty(mailSettings.MailDomainName)) {
                mailSettings.MailDomainName = context.Request.ServerVariables["SERVER_NAME"];
            }

            if (String.IsNullOrEmpty(mailSettings.ReturnAddress)) {
                mailSettings.ReturnAddress = "no-reply@" + mailSettings.MailDomainName;
            }

            return mailSettings;
        }
示例#3
0
        public static bool SendMail(string fromEmail, List <string> emailTo, List <string> emailCC, string subjectLine,
                                    string bodyText, bool isHTML, List <string> attachments)
        {
            HttpContext   context      = HttpContext.Current;
            EMailSettings mailSettings = EMailSettings.GetEMailSettings();

            if (String.IsNullOrEmpty(fromEmail))
            {
                fromEmail = mailSettings.ReturnAddress;
            }

            if (emailTo != null && emailTo.Any())
            {
                MailMessage message = new MailMessage {
                    From       = new MailAddress(fromEmail),
                    Subject    = subjectLine,
                    Body       = bodyText,
                    IsBodyHtml = isHTML
                };

                message.Headers.Add("X-Computer", Environment.MachineName);
                message.Headers.Add("X-Originating-IP", context.Request.ServerVariables["REMOTE_ADDR"].ToString());
                message.Headers.Add("X-Application", "Carrotware Web " + CurrentDLLVersion);
                message.Headers.Add("User-Agent", "Carrotware Web " + CurrentDLLVersion);
                message.Headers.Add("Message-ID", "<" + Guid.NewGuid().ToString().ToLowerInvariant() + "@" + mailSettings.MailDomainName + ">");

                foreach (var t in emailTo)
                {
                    message.To.Add(new MailAddress(t));
                }

                if (emailCC != null)
                {
                    foreach (var t in emailCC)
                    {
                        message.CC.Add(new MailAddress(t));
                    }
                }

                if (attachments != null)
                {
                    foreach (var f in attachments)
                    {
                        Attachment         a    = new Attachment(f, MediaTypeNames.Application.Octet);
                        ContentDisposition disp = a.ContentDisposition;
                        disp.CreationDate     = System.IO.File.GetCreationTime(f);
                        disp.ModificationDate = System.IO.File.GetLastWriteTime(f);
                        disp.ReadDate         = System.IO.File.GetLastAccessTime(f);
                        message.Attachments.Add(a);
                    }
                }

                SmtpClient client = new SmtpClient();
                if (mailSettings.DeliveryMethod == SmtpDeliveryMethod.Network &&
                    !String.IsNullOrEmpty(mailSettings.MailUserName) &&
                    !String.IsNullOrEmpty(mailSettings.MailPassword))
                {
                    client.Host        = mailSettings.MailDomainName;
                    client.Credentials = new NetworkCredential(mailSettings.MailUserName, mailSettings.MailPassword);
                }
                else
                {
                    client.Credentials = new NetworkCredential();
                }

                client.Send(message);
            }

            return(true);
        }