0

Currently my WPF application has the SMTP set up as followed:

            string MailTo = txtBoxEmail.Text;
            string MailFrom = "datfakeemaildoe@gmail.com "; 
            string Subject = "Cool Subject";
            string Password = "*******";

            SmtpClient smtpmail = new SmtpClient();
            smtpmail.Host = "smtp.gmail.com"; 
            smtpmail.Port = 587; 
            smtpmail.EnableSsl = true;
            smtpmail.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpmail.UseDefaultCredentials = false;
            smtpmail.Credentials = new NetworkCredential(MailFrom, Password); 

            MailMessage message = new MailMessage(MailFrom, MailTo, Subject, MessageTosend);
            message.IsBodyHtml = true;

However, I'd like to manage all of this in the config rather than the .cs if possible.

Currently, I set up my App.Config as followed:

<configuration>  
  <appSettings>
   <add key="host" value="smtp.gmail.com" />
   <add key="port" value="587" />
   <add key="MailFrom" value="datfakeemaildoe@gmail.com" />
   <add key="Password" value="*******" />
  </appSettings>
</configuration>

Problem is, how to I implement it properly now in the .cs

I tried this:

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

...

private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        ...
            string MessageTosend = @"there is html in here, trust me";
            MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);
            string MailTo = txtBoxEmail.Text;
            string Subject = "Cool Subject";
            SmtpClient smtp = new SmtpClient()
            {
                host = ConfigurationManager.AppSettings["host"],
                port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]),
                EnableSsl = true,    
                DeliveryMethod = SmtpDeliveryMethod.Network;
                UseDefaultCredentials = false;

                Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["Password"])

            };

            MailMessage message = new MailMessage(ConfigurationManager.AppSettings["MailFrom, MailTo, Subject, MessageTosend);
            message.IsBodyHtml = true;

...

BUT ConfigurationManager is generating an error that says does not exist in the current context, even though I have...

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

...already. I just want to be able to change SMTP settings in the config rather than having to update the code behind for my application in case things change later.

Not sure if I'm doing it wrong or if I'm just missing something entirely. I referenced this to know how to use the app.config.

justinternio
  • 87
  • 2
  • 10
  • 1
    If you omit all mail settings in your code, i.e skip setting host, port etc of the mail client object, you can use the built in mail settings in the web.config. http://msdn.microsoft.com/en-us/library/vstudio/w355a94k(v=vs.100).aspx – HaukurHaf Jun 11 '14 at 15:31

2 Answers2

0

As stated on Scott Gu's blog, there's already a section in .config specifically for these settings, no need to do it in App Settings. The .NET framework automatically reads these settings in without the need to write C# to do it. Here's the MSDN documentation.

Example:

<system.net>
    <mailSettings>
      <smtp from="test@foo.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

No need to specify any code for loading of the configuration.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Would it be different for WPF applications? I don't see `web.config` in my Solution Explorer. I know it is there for ASP.Net – justinternio Jun 11 '14 at 15:37
  • @justinternio `web.config` is specific to ASP.NET. For WPF, it'd be `app.config`. – mason Jun 11 '14 at 15:39
  • Cool. So when I need to call this in the `.cs` how would I do so? Something, like this: `mail.from = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["Email1"]); ` ? Seems excessive. – justinternio Jun 11 '14 at 15:49
  • @justinternio No, not at all. You do NOT need to do anything in the C# to read the configuration settings. It automatically handles that for you. – mason Jun 11 '14 at 17:47
  • Thanks for clarifying! Noticed that as well. – justinternio Jun 11 '14 at 18:05
0

Figured out the ConfigurationManager problem.

While <system.net><mailSettings> ... works, so does my original <appSettings>. The reason why ConfigurationManager was running into a issue was because System.Configuration.dll was missing in my references (source)

Adding this, I was able to call the keys I added in my App.config

Then I changed the way it was used in the .cs

                SmtpClient smtpmail = new SmtpClient();

                smtpmail.Host = ConfigurationManager.AppSettings["host"];
                smtpmail.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
                smtpmail.EnableSsl = true;    
                smtpmail.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpmail.UseDefaultCredentials = false;

                smtpmail.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["Password"]);

                MailMessage message = new MailMessage(ConfigurationManager.AppSettings["MailFrom"], MailTo, Subject, MessageTosend);
                message.IsBodyHtml = true;

Worked like a charm. :)

Community
  • 1
  • 1
justinternio
  • 87
  • 2
  • 10