Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email in asp.net via local host server

Tags:

c#

asp.net

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?

like image 253
Hadi Nemati Avatar asked Mar 27 '12 11:03

Hadi Nemati


People also ask

Can I send email using localhost?

If you want to send emails from localhost directly, you need to install a Mail Transport Agent (MTA), or if you like, a SMTP service. IIS provides one. You can otherwise find some others on Google.

How can I send email without SMTP server?

The simplest way to send a message is to use QuickSend method of Smtp class (this method is static, it does not require you to create an instance of Smtp class). QuickSend method allows you to send e-mails out even if you do not have an SMTP relay server.


2 Answers

Here ya go :) 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

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

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

and C# code

  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.

like image 171
walther Avatar answered Oct 17 '22 06:10

walther


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")
like image 23
Ashwini Verma Avatar answered Oct 17 '22 07:10

Ashwini Verma