Example #1
0
        /// <summary>
        /// Sends an email with a MailMessage and a set of settings.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="settings"></param>
        /// <returns>true if succeeded, false if sending was skipped (i.e missing settings). Could throw exceptions for other reasons.</returns>
        public static async Task <bool> SendMailAsync(MailMessage message, IMailServerSettings settings)
        {
            var sender = GetSenderForSettings(settings);

            if (sender == null)
            {
                return(false);
            }

            return(await sender.SendMailAsync(message, settings));
        }
Example #2
0
 public static ISender GetSenderForSettings(IMailServerSettings settings)
 {
     if (settings is SmtpMailServerSettings)
     {
         return(new Senders.SMTP());
     }
     else if (settings is MailgunMailServerSettings)
     {
         return(new Senders.Mailgun());
     }
     else
     {
         return(null);
     }
 }
Example #3
0
        public static IMailServerSettings GetMailSettings()
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.MailServiceType) &&
                !string.IsNullOrEmpty(Properties.Settings.Default.MailServiceSettings))
            {
                try
                {
                    using (var reader = new StringReader(Properties.Settings.Default.MailServiceSettings))
                    {
                        return(IMailServerSettings.GetSerializer().Deserialize(reader) as IMailServerSettings);
                    }
                }
                catch
                {
                    // Try other settings stores, like those single values
                }
            }

            switch (Properties.Settings.Default.MailServiceType)
            {
            case SmtpMailServerSettings.TYPE:
            {
                var settings = new SmtpMailServerSettings();
                settings.Host                   = Properties.Settings.Default.SmtpServer;
                settings.Port                   = Properties.Settings.Default.SmtpPort;
                settings.RequiresSsl            = Properties.Settings.Default.SmtpSsl;
                settings.RequiresAuthentication = Properties.Settings.Default.SmtpAuthentication;
                settings.Username               = Properties.Settings.Default.SmtpUsername;
                settings.Password               = Properties.Settings.Default.SmtpPassword;
                settings.ConnectionTimeout      = Properties.Settings.Default.SmtpConnectionTimeout;
                return(settings);
            }

            case MailgunMailServerSettings.TYPE:
            {
                var settings = new MailgunMailServerSettings();
                settings.Domain            = Properties.Settings.Default.MailgunDomain;
                settings.ApiKey            = Properties.Settings.Default.MailgunApiKey;
                settings.ConnectionTimeout = Properties.Settings.Default.MailgunTimeout;
                return(settings);
            }
            }

            // Something really empty that won't send anything
            return(new SmtpMailServerSettings());
        }
Example #4
0
        public static void SetMailSettings(IMailServerSettings settings)
        {
            if (settings == null)
            {
                Properties.Settings.Default.MailServiceType = "";
            }
            else
            {
                Properties.Settings.Default.MailServiceType = settings.Type;

                if (settings is SmtpMailServerSettings)
                {
                    var smtpSettings = settings as SmtpMailServerSettings;

                    Properties.Settings.Default.SmtpServer         = smtpSettings.Host;
                    Properties.Settings.Default.SmtpPort           = smtpSettings.Port;
                    Properties.Settings.Default.SmtpSsl            = smtpSettings.RequiresSsl;
                    Properties.Settings.Default.SmtpAuthentication = smtpSettings.RequiresAuthentication;
                    Properties.Settings.Default.SmtpUsername       = smtpSettings.Username;
                    Properties.Settings.Default.SmtpPassword       = smtpSettings.Password;
                }
                else if (settings is MailgunMailServerSettings)
                {
                    var mgSettings = settings as MailgunMailServerSettings;

                    Properties.Settings.Default.MailgunDomain = mgSettings.Domain;
                    Properties.Settings.Default.MailgunApiKey = mgSettings.ApiKey;
                }
                else
                {
                    using (var writer = new StringWriter(new StringBuilder()))
                    {
                        IMailServerSettings.GetSerializer().Serialize(writer, settings);
                        Properties.Settings.Default.MailServiceSettings = writer.GetStringBuilder().ToString();
                    }
                }
            }

            Properties.Settings.Default.Save();
        }
Example #5
0
        public void WriteXml(XmlWriter writer)
        {
            if (this != null)
            {
                writer.WriteStartElement("MailMessage");
                writer.WriteAttributeString("Priority", this.Priority.ToString());
                writer.WriteAttributeString("IsBodyHtml", this.IsBodyHtml.ToString());
                if (this.BodyEncoding != null)
                {
                    writer.WriteAttributeString("BodyEncoding", this.BodyEncoding.CodePage.ToString());
                }
                if (this.HeadersEncoding != null)
                {
                    writer.WriteAttributeString("HeadersEncoding", this.HeadersEncoding.CodePage.ToString());
                }
                if (this.SubjectEncoding != null)
                {
                    writer.WriteAttributeString("SubjectEncoding", this.SubjectEncoding.CodePage.ToString());
                }
                writer.WriteAttributeString("DeliveryNotificationOptions", this.DeliveryNotificationOptions.ToString());

                if (this.From != null)
                {
                    writer.WriteStartElement("From");
                    if (!string.IsNullOrEmpty(this.From.DisplayName))
                    {
                        writer.WriteAttributeString("DisplayName", this.From.DisplayName);
                    }
                    writer.WriteRaw(this.From.Address);
                    writer.WriteEndElement();
                }

                if (this.Sender != null)
                {
                    writer.WriteStartElement("Sender");
                    if (!string.IsNullOrEmpty(this.Sender.DisplayName))
                    {
                        writer.WriteAttributeString("DisplayName", this.Sender.DisplayName);
                    }
                    writer.WriteRaw(this.Sender.Address);
                    writer.WriteEndElement();
                }

                if (this.To.Count > 0)
                {
                    writer.WriteStartElement("To");
                    writer.WriteStartElement("Addresses");
                    foreach (MailAddress address in this.To)
                    {
                        writer.WriteStartElement("Address");
                        if (!string.IsNullOrEmpty(address.DisplayName))
                        {
                            writer.WriteAttributeString("DisplayName", address.DisplayName);
                        }
                        writer.WriteRaw(address.Address);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                if (this.ReplyToList.Count > 0)
                {
                    writer.WriteStartElement("ReplyToList");
                    writer.WriteStartElement("Addresses");
                    foreach (MailAddress address in this.ReplyToList)
                    {
                        writer.WriteStartElement("Address");
                        if (!string.IsNullOrEmpty(address.DisplayName))
                        {
                            writer.WriteAttributeString("DisplayName", address.DisplayName);
                        }
                        writer.WriteRaw(address.Address);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                if (this.CC.Count > 0)
                {
                    writer.WriteStartElement("CC");
                    writer.WriteStartElement("Addresses");
                    foreach (MailAddress address in this.CC)
                    {
                        writer.WriteStartElement("Address");
                        if (!string.IsNullOrEmpty(address.DisplayName))
                        {
                            writer.WriteAttributeString("DisplayName", address.DisplayName);
                        }
                        writer.WriteRaw(address.Address);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                if (this.Bcc.Count > 0)
                {
                    writer.WriteStartElement("Bcc");
                    writer.WriteStartElement("Addresses");
                    foreach (MailAddress address in this.Bcc)
                    {
                        writer.WriteStartElement("Address");
                        if (!string.IsNullOrEmpty(address.DisplayName))
                        {
                            writer.WriteAttributeString("DisplayName", address.DisplayName);
                        }
                        writer.WriteRaw(address.Address);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                if (this.Headers.Count > 0)
                {
                    writer.WriteStartElement("Headers");
                    foreach (string key in this.Headers.AllKeys)
                    {
                        writer.WriteStartElement("Header");
                        writer.WriteAttributeString("Key", key);
                        writer.WriteRaw(this.Headers[key]);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }

                if (this.Attachments.Count > 0)
                {
                    writer.WriteStartElement("Attachments");
                    foreach (var attachment in this.Attachments)
                    {
                        var stream = attachment.ContentStream as FileStream;
                        if (stream == null)
                        {
                            return;
                        }

                        writer.WriteStartElement("Attachment");

                        if (stream.Name != null)
                        {
                            writer.WriteAttributeString("FileName", stream.Name);
                        }

                        if (attachment.Name != null)
                        {
                            writer.WriteAttributeString("Name", attachment.Name);
                        }

                        if (attachment.NameEncoding != null)
                        {
                            writer.WriteAttributeString("NameEncoding", attachment.NameEncoding.EncodingName);
                        }

                        if (attachment.ContentId != null)
                        {
                            writer.WriteAttributeString("ContentId", attachment.ContentId);
                        }

                        if (attachment.ContentType != null)
                        {
                            writer.WriteAttributeString("ContentType", attachment.ContentType.MediaType);
                        }

                        writer.WriteAttributeString("TransferEncoding", attachment.TransferEncoding.ToString());

                        if (attachment.ContentDisposition != null)
                        {
                            writer.WriteStartElement("ContentDisposition");
                            {
                                writer.WriteAttributeString("DispositionType", attachment.ContentDisposition.DispositionType);

                                writer.WriteStartElement("Parameters");
                                {
                                    foreach (string key in attachment.ContentDisposition.Parameters.Keys)
                                    {
                                        writer.WriteAttributeString(key, attachment.ContentDisposition.Parameters[key]);
                                    }
                                }
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                        }

                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }

                writer.WriteStartElement("Subject");
                writer.WriteRaw(this.Subject);
                writer.WriteEndElement();

                writer.WriteStartElement("Body");
                writer.WriteCData(Body);
                writer.WriteEndElement();

                writer.WriteEndElement();

                if (MailSettings != null)
                {
                    writer.WriteStartElement("MailSettings");

                    var s = IMailServerSettings.GetSerializer();
                    s.Serialize(writer, MailSettings);

                    writer.WriteEndElement();
                }
            }
        }
Example #6
0
        public void ReadXml(XmlReader reader)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(reader);

            XmlNode rootNode = XmlUtil.GetConfigNode(xml, "MailMessage");

            this.IsBodyHtml = Convert.ToBoolean(rootNode.Attributes["IsBodyHtml"].Value);
            this.Priority   = (MailPriority)Enum.Parse(typeof(MailPriority), rootNode.Attributes["Priority"].Value);
            if (rootNode.Attributes["BodyEncoding"] != null)
            {
                this.BodyEncoding = Encoding.GetEncoding(Convert.ToInt32(rootNode.Attributes["BodyEncoding"].Value));
            }
            if (rootNode.Attributes["HeadersEncoding"] != null)
            {
                this.HeadersEncoding = Encoding.GetEncoding(Convert.ToInt32(rootNode.Attributes["HeadersEncoding"].Value));
            }
            if (rootNode.Attributes["SubjectEncoding"] != null)
            {
                this.SubjectEncoding = Encoding.GetEncoding(Convert.ToInt32(rootNode.Attributes["SubjectEncoding"].Value));
            }
            this.DeliveryNotificationOptions = (DeliveryNotificationOptions)Enum.Parse(typeof(DeliveryNotificationOptions), rootNode.Attributes["DeliveryNotificationOptions"].Value);

            string      displayName;
            MailAddress address;

            XmlNode smtpNode = XmlUtil.GetConfigNode(xml, "MailSettings");

            if (smtpNode != null)
            {
                using (var strReader = new StringReader(smtpNode.InnerXml))
                {
                    var s = IMailServerSettings.GetSerializer();
                    this.MailSettings = s.Deserialize(strReader) as IMailServerSettings;
                }
            }

            XmlNode fromNode = XmlUtil.GetConfigNode(xml, "MailMessage/From");

            if (fromNode != null)
            {
                displayName = string.Empty;
                if (fromNode.Attributes["DisplayName"] != null)
                {
                    displayName = fromNode.Attributes["DisplayName"].Value;
                }
                address   = new MailAddress(fromNode.InnerText, displayName);
                this.From = address;
            }

            XmlNode senderNode = XmlUtil.GetConfigNode(xml, "MailMessage/Sender");

            if (senderNode != null)
            {
                displayName = string.Empty;
                if (senderNode.Attributes["DisplayName"] != null)
                {
                    displayName = senderNode.Attributes["DisplayName"].Value;
                }
                address     = new MailAddress(senderNode.InnerText, displayName);
                this.Sender = address;
            }

            XmlNode toNode = XmlUtil.GetConfigNode(xml, "MailMessage/To/Addresses");

            if (toNode != null)
            {
                foreach (XmlNode node in toNode.ChildNodes)
                {
                    displayName = string.Empty;
                    if (node.Attributes["DisplayName"] != null)
                    {
                        displayName = node.Attributes["DisplayName"].Value;
                    }
                    address = new MailAddress(node.InnerText, displayName);
                    this.To.Add(address);
                }
            }

            XmlNode replyToListNode = XmlUtil.GetConfigNode(xml, "MailMessage/ReplyToList/Addresses");

            if (replyToListNode != null)
            {
                foreach (XmlNode node in replyToListNode.ChildNodes)
                {
                    displayName = string.Empty;
                    if (node.Attributes["DisplayName"] != null)
                    {
                        displayName = node.Attributes["DisplayName"].Value;
                    }
                    address = new MailAddress(node.InnerText, displayName);
                    this.ReplyToList.Add(address);
                }
            }

            XmlNode ccNode = XmlUtil.GetConfigNode(xml, "MailMessage/CC/Addresses");

            if (ccNode != null)
            {
                foreach (XmlNode node in ccNode.ChildNodes)
                {
                    displayName = string.Empty;
                    if (node.Attributes["DisplayName"] != null)
                    {
                        displayName = node.Attributes["DisplayName"].Value;
                    }
                    address = new MailAddress(node.InnerText, displayName);
                    this.CC.Add(address);
                }
            }

            XmlNode bccNode = XmlUtil.GetConfigNode(xml, "MailMessage/Bcc/Addresses");

            if (bccNode != null)
            {
                foreach (XmlNode node in bccNode.ChildNodes)
                {
                    displayName = string.Empty;
                    if (node.Attributes["DisplayName"] != null)
                    {
                        displayName = node.Attributes["DisplayName"].Value;
                    }
                    address = new MailAddress(node.InnerText, displayName);
                    this.Bcc.Add(address);
                }
            }

            XmlNode headersNode = XmlUtil.GetConfigNode(xml, "MailMessage/Headers");

            if (headersNode != null)
            {
                foreach (XmlNode node in headersNode.ChildNodes)
                {
                    this.Headers.Add(node.Attributes["Key"].Value, node.InnerText);
                }
            }

            XmlNode attachmentsNode = XmlUtil.GetConfigNode(xml, "MailMessage/Attachments");

            if (attachmentsNode != null)
            {
                foreach (XmlNode node in attachmentsNode.ChildNodes)
                {
                    var attachment = new Attachment(node.Attributes["FileName"].Value);

                    if (node.Attributes["Name"] != null)
                    {
                        attachment.Name = node.Attributes["Name"].Value;
                    }

                    if (node.Attributes["NameEncoding"] != null)
                    {
                        attachment.NameEncoding = Encoding.GetEncoding(node.Attributes["NameEncoding"].Value);
                    }

                    if (node.Attributes["ContentId"] != null)
                    {
                        attachment.ContentId = node.Attributes["ContentId"].Value;
                    }

                    if (node.Attributes["ContentType"] != null)
                    {
                        attachment.ContentType = new ContentType(node.Attributes["ContentType"].Value);
                    }

                    if (node.Attributes["TransferEncoding"] != null)
                    {
                        attachment.TransferEncoding = (TransferEncoding)Enum.Parse(typeof(TransferEncoding), node.Attributes["TransferEncoding"].Value);
                    }

                    var cdNode = node.SelectSingleNode("ContentDisposition");
                    if (cdNode != null)
                    {
                        if (cdNode.Attributes["DispositionType"] != null)
                        {
                            attachment.ContentDisposition.DispositionType = cdNode.Attributes["DispositionType"].Value;
                        }

                        var pNode = cdNode.SelectSingleNode("Parameters");
                        if (pNode != null)
                        {
                            for (var pi = 0; pi < pNode.Attributes.Count; pi++)
                            {
                                var attr = pNode.Attributes[pi];
                                attachment.ContentDisposition.Parameters[attr.Name] = attr.Value;
                            }
                        }
                    }

                    this.Attachments.Add(attachment);
                }
            }

            XmlNode subjectNode = XmlUtil.GetConfigNode(xml, "MailMessage/Subject");

            this.Subject = subjectNode.InnerText;

            XmlNode bodyNode = XmlUtil.GetConfigNode(xml, "MailMessage/Body");

            this.Body = bodyNode.InnerText;
        }
Example #7
0
        public void QueueMessageWithMailSettings(SerializableMailMessage mailMessage, IMailServerSettings mailSettings)
        {
            if (mailMessage == null)
            {
                return;
            }

            mailMessage.MailSettings = mailSettings;

            Coordinator.SharedInstance.AddMail(mailMessage);
        }
Example #8
0
 public void SetMailSettings(IMailServerSettings settings)
 {
     SettingsController.SetMailSettings(settings);
 }