[英]ASP.Net Unable to send Email
我正在嘗試使用以下代碼從 asp.net(C#) 發送 email。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using MovieReviews.Utils;
/// <summary>
/// Summary description for EmailUtil
/// </summary>
public class EmailUtil
{
public static void SendEmail(string to, string name, string from, string body)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(from, name);
smtpClient.Host = "localhost";
//Default port will be 25
smtpClient.Port = 25;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add(to);
message.Subject = "Feedback";
message.IsBodyHtml = false;
// Message body content
message.Body = body;
// Send SMTP mail
smtpClient.Send(message);
}
catch (Exception ex)
{
Logger.LogError(ex);
throw ex;
}
}
}
當我嘗試執行它說
試圖以訪問權限 127.0.0.1:25 禁止的方式訪問套接字
請建議我該怎么做。 我嘗試按照論壇中的一些答案關閉防火牆,但沒有運氣。
先感謝您
為了使您的代碼正常工作,您需要在本地計算機上運行 SMTP 服務器,該服務器接受 127.0.0.1 上的連接,異常意味着情況並非如此,或者存在特權和/或配置問題。
編輯:根據您的操作系統,您可以配置 IIS 作為 SMTP 服務器。 If you are on Windows 2008 then you need to use IIS 6 (contains SMTP server) additionally to IIS 7 (no SMTP server).
我遇到過同樣的問題。
由於 McAfee -> 訪問保護 -> 防病毒標准保護 -> 阻止群發郵件蠕蟲發送郵件,此端口被阻止。
如果您可以刪除該或任何其他 AV 保護,那么您就可以了。 如果這是對組織的保護策略,請嘗試切換端口。
我在 Smtp4dev 和 C# 應用程序中將端口更改為 81 並且它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.