tags:

views:

267

answers:

5

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");
    }
}
+8  A: 

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.

Steven Sudit
+7  A: 

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);
p.campbell
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.
Skeolan
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.
Steven Sudit
+1  A: 

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");
Justin Niessner
+1  A: 

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.

Reed Copsey
A: 

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