ASP.Net无法发送电子邮件 [英] ASP.Net Unable to send Email

查看:56
本文介绍了ASP.Net无法发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}
}  

当我尝试执行时说

试图以一种禁止其访问的方式访问套接字访问权限127.0.0.1:25

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.

提前谢谢

推荐答案

为使代码正常工作,您需要在本地计算机上运行SMTP服务器,该服务器接受127.0.0.1上的连接,但异常表示这不是情况或特权和/或配置存在一些问题.

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.

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

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).

这篇关于ASP.Net无法发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆