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.