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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With