This way can be so helpfull, but, some people (like me) are afreak of repeat code, and like you are showin us, it means that I need to create a contact controller with the same code on each proyect that we have, so, I thing that this can be helpfull too
This is my class, that can be on a DLL or whatever
public class Emails
{
public static void SendHtmlEmail(string receiverEmail, string subject, string body, bool Ssl = false)
{
//Those are read it from webconfig or appconfig
var client = new SmtpClient(ConfigurationManager.AppSettings["MailServer"], Convert.ToInt16
(ConfigurationManager.AppSettings["MailPort"]))
{
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MailSender"], ConfigurationManager.AppSettings["MailSenderPassword"]),
EnableSsl = Ssl
};
MailMessage message = new MailMessage();
message.From = new MailAddress(ConfigurationManager.AppSettings["MailSender"]);
message.To.Add(receiverEmail);
// message.To.Add("[email protected]");
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
client.Send(message);
}
}
like you see it will read from the webconfig, so, we need to configured it, this configuration is for Gmail, but, every host have their own configuration
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="AdminUser" value="[email protected]" />
<add key="AdminPassWord" value="123456789" />
<add key="SMTPName" value="smtp.gmail.com" />
<add key="SMTPPort" value="587" />
</appSettings>