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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9888649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:05:17  来源:igfitidea点击:

Sending email in asp.net via local host server

c#asp.net

提问by Hadi Nemati

Is there any example that can explain me to send email from my localhost server ? I've written this example but it doesn't work the error is "Failure sending mail".

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

 SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "localhost";
        smtpClient.Port = 25;
        smtpClient.EnableSsl = false;
        smtpClient.Credentials = new NetworkCredential("[email protected]", "secret");
        smtpClient.Send("[email protected]", "[email protected]", "Let's eat lunch!", "Lunch at the Steak House?");//here is the error

And what should i do in web.config?

我应该在 web.config 中做什么?

回答by walther

Here ya go :) localhost-with-aspnet-without-smtp-server

给你:) localhost-with-aspnet-without-smtp-server

Let me please know if it works for you the way you need it to.

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



Link above doesn't work, so I'll improve the answer.

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

For testing purposes we can use localhost like this: How to Test Email Without Configure SMTP in ASP.NET

出于测试目的,我们可以像这样使用 localhost:How to Test Email without Configure SMTP in ASP.NET

In case the link goes down again, basically we have to modify web.config like this:

如果链接再次断开,基本上我们必须像这样修改 web.config:

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

and C# code

和 C# 代码

  MailMessage mailMessage = new MailMessage();
  MailAddress fromAddress = new MailAddress("[email protected]");
  mailMessage.From = fromAddress;
  mailMessage.To.Add("[email protected]");
  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);

This will output a file to our desired directory.

这将输出一个文件到我们想要的目录。

回答by Strillo

You need to specify the settings for your SMTP server in web.config. There are several example online (e.g. this)

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

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
            <network host="smtp.mail.com" userName="[email protected]" password="pwd" port="25"/>
        </smtp>
    </mailSettings>
</system.net>

Then you can simply use the SmtpClient class to send:

然后你可以简单地使用 SmtpClient 类来发送:

SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;

MailMessage msg = new MailMessage();
msg.To.Add("[email protected]");
msg.Subject = "test";
msg.Body = "test body";

smtpClient.Send(msg);

回答by Ashwini Verma

here is the sample:

这是示例:

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

And call the function:

并调用函数:

SendMailMessage("[email protected]", "[email protected]", "[email protected]", "[email protected]", "Sample Subject", "Sample body of text for mail message")

回答by Gabriel Scavassa

 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 = [email protected];
                mail._To = [email protected];
                mail._Subject = subject;

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

            return ret;