简体   繁体   中英

SMTP Mail client settings in app.config file C#

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?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM