开发者

SMTP Email from ASP.NET

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 Mail开发者_如何学GoMessage();
    mailmessage.From("admin@xxxxxx.com");
    mailmessage.To.Add(new MailMessage(mailTo));
    mailmessage.ReplyTo("no-reply@xxxxxx.com");
    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="no-reply@xxxxxx.com">
        <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!


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.


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?


There's a possibility that your email server doesn't like the "From" address of "admin@xxxxxx.com" (specified in your code), but it does like the "From" address of "no-reply@xxxxxx.com" (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜