简体   繁体   English

使用SmtpClient类通过ASP.NET网站将电子邮件发送到MX记录

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

I've recently been having issues sending email from my web application. 我最近在从Web应用程序发送电子邮件时遇到了问题。 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. 与邮件团队进行了深入讨论之后,我被告知我没有使用MX记录发送邮件。 However, I think I am. 但是,我想我是。 The MX-record is mailhub-us.xxx.us.net . MX记录是mailhub-us.xxx.us.net Here is the code that I use to send emails (clearly I reference the mailhub address as the server) 这是我用来发送电子邮件的代码(显然,我引用mailhub地址作为服务器)

    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). 是的,我知道我最好使用web.config中的<mailsettings> (这是我在研究中学到的,而且我打算纠正)。 I've checked to ensure that the MX records are set up at the DNS using nslookup and there are 3 servers configured for this entry. 我已检查以确保使用nslookup在DNS上设置了MX记录,并且为此条目配置了3台服务器。

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. 在这一点上,我有点困惑,因为我以为我正在使用MX记录,因此故障转移应该自动进行。 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 在您的web.config文件中添加此

 <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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM