9

I've put mail settings in app.config and can successfully pull them into a mailSettingsSectionGroup object. However, I'm not sure how to send a message using these settings.

This is what I have so far:

System.Configuration.Configuration config =     
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

MailSettingsSectionGroup mailSettings  = 
config.GetSectionGroup("system.net/mailSettings") as 
System.Net.Configuration.MailSettingsSectionGroup;

What do I need to do next to use the mailSettings object?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Caveatrob
  • 12,667
  • 32
  • 107
  • 187

2 Answers2

21

System.Net.Mail.SmtpClient

Specifically, the Send(...) method. SmtpClient will automatically pull the details from your app/web.config file. You don't need to do anything to use the configuration, it's all handled automatically.

Edit to add SMTP Web.Config Example:

<system.net>
    <mailSettings>
        <smtp from="foo@bar.com">
            <network host="yoursmtpserver.com" />
        </smtp>
    </mailSettings>
</system.net>
ChadT
  • 7,598
  • 2
  • 40
  • 57
  • 1
    Can you please provide an example web.config file or specify the schema that SmtpClient will read? – Ryan Gates Aug 12 '15 at 16:14
  • 1
    How the 'from' is pulled automatically? – mynkow Apr 19 '16 at 08:36
  • so I specify an address in "from" but when I use another address in the code the address in the code overwrites the one from config. and if I specify nothing in the code (null, empty string) an error is thrown. – Greg Nov 04 '16 at 14:31
5

I have a custom Class:

    using System;
    using System.Configuration;
    using System.Net;
    using System.Net.Configuration;
    using System.Net.Mail;

    namespace MyNameSpace
    {
        internal static class SMTPMailer
        {
            public static void SendMail(string to, string subject, string body)
            {
                Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var mailSettings = oConfig.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

                if (mailSettings != null)
                {
                    int port = mailSettings.Smtp.Network.Port;
                    string from = mailSettings.Smtp.From;
                    string host = mailSettings.Smtp.Network.Host;
                    string pwd = mailSettings.Smtp.Network.Password;
                    string uid = mailSettings.Smtp.Network.UserName;

                    var message = new MailMessage
                        {
                            From = new MailAddress(@from)
                        };
                    message.To.Add(new MailAddress(to));
                    message.CC.Add(new MailAddress(from));
                    message.Subject = subject;
                    message.IsBodyHtml = true;
                    message.Body = body;

                    var client = new SmtpClient
                        {
                            Host = host,
                            Port = port,
                            Credentials = new NetworkCredential(uid, pwd),
                            EnableSsl = true
                        };

                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
         }
     }

And this pulls from my app.conf file just fine.

Randy
  • 1,137
  • 16
  • 49