简体   繁体   English

ASP.Net 无法发送 Email

[英]ASP.Net Unable to send Email

I am trying to use the below code to send email from asp.net(C#).我正在尝试使用以下代码从 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;
    }

}
}  

When I try to execute it says当我尝试执行它说

An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:25试图以访问权限 127.0.0.1:25 禁止的方式访问套接字

Please suggest me what should i do.请建议我该怎么做。 I tried turning off the firewall as per some answers in the forums, but no luck.我尝试按照论坛中的一些答案关闭防火墙,但没有运气。

Thank you in advance先感谢您

for your code to work you need to have an SMTP server running on the local machine which accepts connections on 127.0.0.1, the exception implies that this either not the case or that some problem with priviliges and/or configuration exists.为了使您的代码正常工作,您需要在本地计算机上运行 SMTP 服务器,该服务器接受 127.0.0.1 上的连接,异常意味着情况并非如此,或者存在特权和/或配置问题。

EDIT: Depending on your OS you could configure IIS to act as SMTP server.编辑:根据您的操作系统,您可以配置 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). If you are on Windows 2008 then you need to use IIS 6 (contains SMTP server) additionally to IIS 7 (no SMTP server).

I had the same issue.我遇到过同样的问题。

This port was blocked due to McAfee -> Access Protection -> Anti-virus Standard protection -> Prevent mass mailing worms form sending mail.由于 McAfee -> 访问保护 -> 防病毒标准保护 -> 阻止群发邮件蠕虫发送邮件,此端口被阻止。

If you can remove that or any other AV protection you are fine.如果您可以删除该或任何其他 AV 保护,那么您就可以了。 If it is a protection policy over the organization then try switching port.如果这是对组织的保护策略,请尝试切换端口。

I've change the port to 81 in the Smtp4dev and in the C# application and it worked.我在 Smtp4dev 和 C# 应用程序中将端口更改为 81 并且它工作正常。

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

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