gpt4 book ai didi

c# - ASP.Net 无法发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 15:43:20 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从 asp.net(C#) 发送电子邮件。

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

}
}

当我尝试执行它时说

An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:25

请建议我该怎么做。我尝试按照论坛中的一些答案关闭防火墙,但没有成功。

提前致谢

最佳答案

为了让您的代码正常工作,您需要在本地计算机上运行一个 SMTP 服务器,该服务器接受 127.0.0.1 上的连接,异常意味着情况并非如此,或者存在一些权限和/或配置问题。

编辑:根据您的操作系统,您可以将 IIS 配置为充当 SMTP 服务器。如果您使用的是 Windows 2008,那么除了 IIS 7(无 SMTP 服务器)之外,您还需要使用 IIS 6(包含 SMTP 服务器)。

关于c# - ASP.Net 无法发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6755741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com