I have an asp.net 4.0 web site that generates email alerts based on several different events throughout the site. I use the following code in a Utility.cs class that lives in my App_Code folder. Subject, body, and mailto are parameters being passed into the method.
try
{
var mailmessage = new MailMessage();
mailmessage.From("[email protected]");
mailmessage.To.Add(new MailMessage(mailTo));
mailmessage.ReplyTo("[email protected]");
mailmessage.Subject = subject;
mailmessage.Body = body;
mailmessage.IsBodyHtml = true;
var smtpClient = new SmtpClient();
smtpClient.Send(mailmessage);
}
catch (Exception smtpEx)
{// write to windows event log}
And my web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network host="xxx.xxx.xxx.xxx" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
These emails are never recieved. However interestingly when I use the "Forgot Password" functionality I do get my emails. That functionality is achieved using the PasswordRecovery control, and it is used as follows.
<asp:PasswordRecovery ID="xxx" Visible="false" runat="server"></asp:PasswordRecovery>
I am not setting any of the MailDefinition properties at all which makes me assume it is using my my web.config settings, so why can this control use those settings but my c# code cannot. I'm guessing it's my mail message maybe??
Appreciate any help you guys can offer.
Thanks!