Подтвердить что ты не робот

Отправка электронной почты в asp.net через локальный хост-сервер

Есть ли какой-нибудь пример, который может объяснить мне, чтобы отправлять электронную почту с моего сервера localhost? Я написал этот пример, но он не работает, ошибка "Отказ отправки почты".

 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

И что я должен делать в web.config?

4b9b3361

Ответ 1

Вот ты иди:) localhost-with-aspnet-without-smtp-server

Позвольте мне, пожалуйста, знать, работает ли он для вас так, как вам нужно.


Ссылка выше не работает, поэтому я улучшу ответ.

В целях тестирования мы можем использовать localhost следующим образом: Как протестировать электронную почту без настройки SMTP в ASP.NET

Если ссылка снова опустится, в основном мы должны изменить web.config следующим образом:

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

и код С#

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

Это приведет к выводу файла в наш желаемый каталог.

Ответ 2

Вам нужно указать настройки для вашего SMTP-сервера в web.config. Есть несколько примеров онлайн (например, this)

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

Затем вы можете просто использовать класс 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);

Ответ 3

вот пример:

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("[email protected]", "[email protected]", "[email protected]", "[email protected]", "Sample Subject", "Sample body of text for mail message")

Ответ 4

 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;