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

10

有没有一个例子可以向我解释如何从我的本地主机服务器发送电子邮件? 我写了这个例子,但它不起作用,错误是"发送邮件失败"。

 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 中应该做什么呢?


1
你的本地主机上配置了SMTP吗? - Habib
你正在使用本地主机并使用雅虎的凭据,我认为这不会起作用。 - Habib
4个回答

24

以下是翻译的内容:

这里有个链接👉localhost-with-aspnet-without-smtp-server,希望对你有用。


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

为了测试目的,我们可以像这样使用 localhost:如何在ASP.NET中测试电子邮件而不配置SMTP

如果链接再次失效,基本上我们需要像这样修改web.config文件:

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

和 C# 代码

  MailMessage mailMessage = new MailMessage();
  MailAddress fromAddress = new MailAddress("mail@mail.com");
  mailMessage.From = fromAddress;
  mailMessage.To.Add("mail@mail.com");
  mailMessage.Body = "This is Testing Email Without Configured SMTP Server";
  mailMessage.IsBodyHtml = true;
  mailMessage.Subject = " Testing Email";
  SmtpClient smtpClient = new SmtpClient();
  smtpClient.Host = "localhost";
  smtpClient.Send(mailMessage);
这将输出一个文件到我们想要的目录。

附注:System.Web.Mail.MailMessage现已弃用。您可以使用System.Net.Mail.MailMessage - rst

2

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

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="test@mydomain.com">
            <network host="smtp.mail.com" userName="name@mydomain.com" password="pwd" port="25"/>
        </smtp>
    </mailSettings>
</system.net>

然后您可以简单地使用SmtpClient类进行发送:

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

你不需要额外的SMTP服务器... ;) - walther
如果它安装在本地主机上,我同意。但是如果您使用外部提供商(例如Rackspace),怎么办? - Strillo

2

这里是示例:

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

我收到了 System.InvalidOperationException: The SMTP host was not specified. 的错误信息。 - mattalxndr

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接