tags:

views:

47

answers:

4

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!

A: 

What do these emails look like? They may be getting killed as spam, especially since you are sending them as HTML (spam filters give a lot of hit points for links, images, etc). The password recovery control sends plain text by default; have you tried running the above code manually sending a message as plain text instead?

pjabbott
A: 

There's a possibility that your email server doesn't like the "From" address of "[email protected]" (specified in your code), but it does like the "From" address of "[email protected]" (specified in your web.config).

Verify that your email server is setup as a valid email address. Also, a lot of times email servers will verify that the domain of the From address actually originates from the IP address you're sending from. You can setup an SPF record in DNS that will tell email servers that the domain is a valid sender from W.X.Y.Z IP address.

Tim S. Van Haren
A: 

Check this post for possible problems.

System.Net.mail.SmtpClient FQDN Required

Brad
A: 

I figured it out - Sorry to waste your guys time. It was just an oversite and I let intellisence get the best of me.

mailmessage.To.Add(new MailMessage(mailTo));

should read

mailmessage.To.Add(new MailAddress(mailTo));

glad it's resolved, but don't know how I missed that.

ColoradoRockie