开发者

Sending email to an MX record through an ASP.NET website using the SmtpClient class

开发者 https://www.devze.com 2023-04-11 04:33 出处:网络
I\'ve recently been having issues sending email from my web application. I keep getting a connection refused exception from the mail relay (and it\'s always the same mail relay). After some thorough d

I've recently been having issues sending email from my web application. I keep getting a connection refused exception from the mail relay (and it's always the same mail relay). After some thorough discussion with the mail team, I've been told that I'm not using the MX record to sent the mail. However, I think I am. The MX-record is mailhub-us.xxx.us.net. Here is the code that I use to send emails (clearly I reference the mailhub address as the server)

    MailMessage msgMail = new MailMessage();
    ****Some code to populate msgMail
    SmtpClient smtpClient = new SmtpClient("mailhub-us.xxx.us.net");
    smtpClient.Send(msgMail);

Yes, I'm aware I'd be better off using <mailsettings> in the web.config (something that I learnt during my research and something I intend to correct). I've checked to e开发者_Go百科nsure that the MX records are set up at the DNS using nslookup and there are 3 servers configured for this entry.

I'm a little confused at this point, because I thought I was using the MX record and hence the failovers should automatically take place. Am I being stupid in saying that or is there something else that I'm missing? Any help in this issue is appreciated.


In your web.config file add this

 <system.net> 
<mailSettings>
  <smtp from="fromemail">
    <network host="hostname" defaultCredentials="false"
    port="xx" userName="xxxx" password="xxx" />
  </smtp>
</mailSettings>

and in your backend code, you could do something like

       var message = new MailMessage();
        message.IsBodyHtml = true;
        message.From = new MailAddress(from);
        message.To.Add(to);
        message.Subject = subject;
        message.Body = msg;
        var client = new SmtpClient();
        client.Send(message);
0

精彩评论

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

关注公众号