简体   繁体   English

如何在 web.config 中配置 SMTP 设置

[英]How to configure SMTP settings in web.config

I'm trying to fix an email issue with an inherited website and don't have access to the code (ie just the compiled files).我正在尝试修复继承网站的电子邮件问题,但无权访问代码(即仅编译文件)。 This site needs to be hosted on a new web server having a different SMTP server.此站点需要托管在具有不同 SMTP 服务器的新 Web 服务器上。

Upon decompiling bits of the code I can see emails are sent using method like below in code snippet and SMTP is set as smtpMail.SmtpServer="localhost" but my new webserver's SMTP server is "relay.tagadab.com" how can we possibly configure this in web.config so that localhost is taken as "relay.tagadab.com"在对代码进行反编译后,我可以看到电子邮件是使用如下代码片段中的方法发送的,并且 SMTP 设置为 smtpMail.SmtpServer="localhost" 但我的新网络服务器的 SMTP 服务器是 "relay.tagadab.com" 我们怎么可能配置这在 web.config 中,以便 localhost 被视为“relay.tagadab.com”

Imports Microsoft.VisualBasic, System.Web.Mail

Shared Sub SendMail(ByVal ToAdd, ByVal FromAdd, ByVal Message, ByVal Subject)

    Dim msgMail As New MailMessage()

    msgMail.To = ToAdd
    msgMail.From = FromAdd
    msgMail.Subject = Subject
    msgMail.Headers.Add("X-Mailer", "ASP.NET")

    msgMail.BodyFormat = MailFormat.Text
    msgMail.Body = Message
    'SmtpMail.SmtpServer = "mail.the-radiator.com"
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(msgMail)

End Sub

I added this section in my web.config but that does not make a difference我在 web.config 中添加了此部分,但这并没有什么区别

<system.net>
    <mailSettings>
        <smtp>
            <network host="relay.tagadab.com" port="25" />
        </smtp>
     </mailSettings>
</system.net>

Web.Config file: Web.Config 文件:

<configuration>
 <system.net>
        <mailSettings>
            <smtp from="yourmail@gmail.com">
                <network host="smtp.gmail.com" 
                 port="587" 
                 userName="yourmail@gmail.com" 
                 password="yourpassword" 
                 enableSsl="true"/>
            </smtp>
        </mailSettings>
</system.net>
</configuration>

I don't have enough rep to answer ClintEastwood, and the accepted answer is correct for the Web.config file.我没有足够的代表来回答 ClintEastwood,并且接受的答案对于 Web.config 文件是正确的。 Adding this in for code difference.添加这个是为了代码差异。

When your mailSettings are set on Web.config, you don't need to do anything other than new up your SmtpClient and .Send.在 Web.config 上设置 mailSettings 后,除了新建 SmtpClient 和 .Send 之外,您无需执行任何其他操作。 It finds the connection itself without needing to be referenced.它无需引用即可找到连接本身。 You would change your C# from this:您将从此更改 C#:

SmtpClient smtpClient = new SmtpClient("smtp.sender.you", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.Credentials = credentials;
smtpClient.Send(msgMail);  

To this:对此:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(msgMail);

Set IIS to forward your mail to the remote server.设置 IIS 以将您的邮件转发到远程服务器。 The specifics vary greatly depending on the version of IIS.具体取决于 IIS 的版本。 For IIS 7.5:对于 IIS 7.5:

  1. Open IIS Manager打开 IIS 管理器
  2. Connect to your server if needed如果需要,连接到您的服务器
  3. Select the server node;选择服务器节点; you should see an SMTP option on the right in the ASP.NET section您应该在 ASP.NET 部分的右侧看到一个 SMTP 选项
  4. Double-click the SMTP icon.双击 SMTP 图标。
  5. Select the "Deliver e-mail to SMTP server" option and enter your server name, credentials, etc.选择“将电子邮件发送到 SMTP 服务器”选项并输入您的服务器名称、凭据等。

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

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