简体   繁体   中英

ASP.Net Unable to send Email

I am trying to use the below code to send email from 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;
    }

}
}  

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

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.

EDIT: Depending on your OS you could configure IIS to act as 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.

If you can remove that or any other AV protection you are fine. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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