[英]How to configure SMTP settings in web.config
我正在嘗試修復繼承網站的電子郵件問題,但無權訪問代碼(即僅編譯文件)。 此站點需要托管在具有不同 SMTP 服務器的新 Web 服務器上。
在對代碼進行反編譯后,我可以看到電子郵件是使用如下代碼片段中的方法發送的,並且 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
我在 web.config 中添加了此部分,但這並沒有什么區別
<system.net>
<mailSettings>
<smtp>
<network host="relay.tagadab.com" port="25" />
</smtp>
</mailSettings>
</system.net>
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>
我沒有足夠的代表來回答 ClintEastwood,並且接受的答案對於 Web.config 文件是正確的。 添加這個是為了代碼差異。
在 Web.config 上設置 mailSettings 后,除了新建 SmtpClient 和 .Send 之外,您無需執行任何其他操作。 它無需引用即可找到連接本身。 您將從此更改 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);
對此:
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(msgMail);
設置 IIS 以將您的郵件轉發到遠程服務器。 具體取決於 IIS 的版本。 對於 IIS 7.5:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.