I am writing a C# class that needs to send SMTP emails. I found code in this SO question which gave me what I needed to send the email (SO Question)
For convenience, here is the code I am modifying from an answer to the above question:
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
The problem I have is that the code in that question requires the smtp password to be stored in the plain-text C# source file. Problem one is that this code will be checked in to team foundation service so the whole team would have to/get to know the credentials. Problem two is that obfuscation is possible but it doesn't fix problem 1.
The closest to a solution i have found is encryption of an app config as an installation step. This doesn't get around problem 1 but if necessary can be done.
Does this situation come up in production applications, and what best practices are used to store passwords needed to instantiate C# classes?