0

I have the following code but I am getting an exception that a smtp host is not defined. If I am running this and testing on my local machine from visual studio, what do I need to do to be able to send email from my machine. Do I have to turn on some Windows service?

private void SendMailToAdminToApprove(string email_, string name_)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[email protected]", "Person's Name");
    msg.To.Add(new MailAddress("[email protected]", "Adam"));
    msg.Subject    = "Message Subject";
    msg.Body       = "Mail body content";
    msg.IsBodyHtml = true;
    msg.Priority   = MailPriority.High;
    try
    {
        SmtpClient c = new SmtpClient();
        c.Send(msg);
    }
    catch (Exception ex)
    {
        Console.Write("T");
    }
}
1
  • Make sure you dispose the MailMessage when you're finished. Commented Jul 31, 2009 at 16:42

5 Answers 5

8

You need to set the SMTP host to point to an actual SMTP server. One option is to run the SMTP service on your own machine, but you could also point to your ISP's server.

edit

As pcampbell and Skeolan mentioned, the actual value should go into app.config. I'm not sure if localhost would be an exception: it would depend on whether you want the option of not running a local server.

Sign up to request clarification or add additional context in comments.

Comments

7

You'll need to specify the SMTP host here:

string smtpHost = "localhost";
//or go to your config file
smtpHost = ConfigurationManager.AppSettings["MySmtpHost"].ToString();

SmtpClient c = new SmtpClient(smtpHost);

2 Comments

better yet, pull out the actual stmpHost string into an app.config or web.config so that you can modify it for different deployments. Then you could use "localhost" for development (assuming you're running a [virtual?] smtp service on your local machine), but still have the option of specifying an external email server later without needing to recompile.
Definitely put it in the app.config. However, there's no need to pretend it's a connection string. Just make it a setting or use the traditional API.
1

You need to define the SMTP relay:

SmtpClient c = new SmtpClient("relay.yourdomain.com");

or if you're running the relay locally:

SmtpClient c = new SmtpClient("localhost");

Comments

1

You should change this section:

 SmtpClient c = new SmtpClient();
 // Either specify a SMTP server above, or set c.Host
 c.Send(msg);

You need to specify which SMTP server is going to be used for sending this message. If you install a SMTP server locally, this could be localhost - but otherwise, you'll need to have an outgoing mail server set appropriately.

Comments

0

Here is the code I use for sending email with C#. I also commented out code for sending it to a file locally if you need it.

        SmtpClient smtp = new SmtpClient(smtpServer, portNumber);
        // Disable SSL when saving to directory.
        smtp.EnableSsl = true;
        smtp.Credentials = new NetworkCredential(mailFrom, password);

        // Set mail to be delivered to a folder
        //smtp.PickupDirectoryLocation = @"C:\mail\Send";
        //smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.