C# 通过本地主机服务器在asp.net中发送电子邮件

C# 通过本地主机服务器在asp.net中发送电子邮件,c#,asp.net,C#,Asp.net,有没有任何例子可以解释我从本地主机服务器发送电子邮件? 我已经编写了这个示例,但它不起作用。错误是“发送邮件失败”。 SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = "localhost"; smtpClient.Port = 25; smtpClient.EnableSsl = false; smtpClient.Credentials = new Netw

有没有任何例子可以解释我从本地主机服务器发送电子邮件? 我已经编写了这个示例,但它不起作用。错误是“发送邮件失败”。

 SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "localhost";
        smtpClient.Port = 25;
        smtpClient.EnableSsl = false;
        smtpClient.Credentials = new NetworkCredential("mostang1970@yahoo.com", "secret");
        smtpClient.Send("mostang1970@yahoo.com", "hadinematipartow@yahoo.com", "Let’s eat lunch!", "Lunch at the Steak House?");//here is the error
在web.config中我应该做什么?

给你:)

请让我知道它是否适合您需要的方式


上面的链接不起作用,所以我会改进答案

出于测试目的,我们可以使用如下localhost:

如果链接再次关闭,基本上我们必须修改web.config,如下所示:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>
      </smtp>
    </mailSettings>
  </system.net>

这将输出一个文件到所需目录。

您需要在web.config中指定SMTP服务器的设置。网上有几个例子(例如)

以下是示例:

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
    // Instantiate a new instance of MailMessage
    MailMessage mMailMessage = new MailMessage();

    // Set the sender address of the mail message
    mMailMessage.From = new MailAddress(from);
    // Set the recepient address of the mail message
    mMailMessage.To.Add(new MailAddress(to));

    // Check if the bcc value is null or an empty string
    if ((bcc != null) && (bcc != string.Empty))
    {
        // Set the Bcc address of the mail message
        mMailMessage.Bcc.Add(new MailAddress(bcc));
    }      // Check if the cc value is null or an empty value
    if ((cc != null) && (cc != string.Empty))
    {
        // Set the CC address of the mail message
        mMailMessage.CC.Add(new MailAddress(cc));
    }       // Set the subject of the mail message
    mMailMessage.Subject = subject;
    // Set the body of the mail message
    mMailMessage.Body = body;

    // Set the format of the mail message body as HTML
    mMailMessage.IsBodyHtml = true;
    // Set the priority of the mail message to normal
    mMailMessage.Priority = MailPriority.Normal;

    // Instantiate a new instance of SmtpClient
    SmtpClient mSmtpClient = new SmtpClient();
    // Send the mail message
    mSmtpClient.Send(mMailMessage);
}
并调用函数:

SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")

您是否在localhost上配置了SMTP?您正在使用localhost并使用来自yahoo的凭据,我认为这不会起作用您不需要任何额外的SMTP服务器…;)我同意它是否安装在本地主机上。如果您使用的是外部提供程序(如Rackspace),该怎么办?旁注:
System.Web.Mail.MailMessage
现在已被弃用。您可以使用
System.Net.Mail.MailMessage
。我得到
System.invalidoOperationException:未指定SMTP主机。
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;

MailMessage msg = new MailMessage();
msg.To.Add("recipient@email.com");
msg.Subject = "test";
msg.Body = "test body";

smtpClient.Send(msg);
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
    // Instantiate a new instance of MailMessage
    MailMessage mMailMessage = new MailMessage();

    // Set the sender address of the mail message
    mMailMessage.From = new MailAddress(from);
    // Set the recepient address of the mail message
    mMailMessage.To.Add(new MailAddress(to));

    // Check if the bcc value is null or an empty string
    if ((bcc != null) && (bcc != string.Empty))
    {
        // Set the Bcc address of the mail message
        mMailMessage.Bcc.Add(new MailAddress(bcc));
    }      // Check if the cc value is null or an empty value
    if ((cc != null) && (cc != string.Empty))
    {
        // Set the CC address of the mail message
        mMailMessage.CC.Add(new MailAddress(cc));
    }       // Set the subject of the mail message
    mMailMessage.Subject = subject;
    // Set the body of the mail message
    mMailMessage.Body = body;

    // Set the format of the mail message body as HTML
    mMailMessage.IsBodyHtml = true;
    // Set the priority of the mail message to normal
    mMailMessage.Priority = MailPriority.Normal;

    // Instantiate a new instance of SmtpClient
    SmtpClient mSmtpClient = new SmtpClient();
    // Send the mail message
    mSmtpClient.Send(mMailMessage);
}
SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
 bool ret = true;

            try
            {
                string _smtpServer = ConfigurationSettings.AppSettings["appEmailHost"];

                Web.Mail.Mail mail = new Web.Mail.Mail(_smtpServer,         
        System.Web.Mail.MailFormat.Html, System.Web.Mail.MailPriority.Normal);
                mail._From = test@test.com;
                mail._To = Test2@test.com;
                mail._Subject = subject;

                mail._Body = body;
                mail.SendMail();
                ret = true;
            }
            catch(Exception exp)
            {
                _GravaErro(exp);
                ret = false;
            }

            return ret;