SMTP client passing the users logged in domain credentials

  • Thread starter Thread starter morganlomas
  • Start date Start date
M

morganlomas

Guest
The following code works great providing the right information; however, how can I pass the logged in credentials instead of hard coding the credentials.

For example instead of this

client.Credentials = new System.Net.NetworkCredential(userName, password);

I would like to do something like this

client.Credentials = new System.Net.NetworkCredential(logged in credentials);


// My code:

String userName = "user";
String password = "password";
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("user@email.com"));
msg.From = new MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "myhost";
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Port = 587;
client.EnableSsl = true;
client.Send(msg);

Continue reading...
 
Back
Top