Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Pushpendra Singh 61 posts 116 karma points
    May 16, 2014 @ 13:36
    Pushpendra Singh
    0

    Sent email using localhost in c#

    Follow below steps checkpoint (which is the sollution of  error "Can not get iis pickup directory")  first:

    1) Is Smtp Installed ?

    Your web server might not even have the SMTP service installed, so get into the server manager (start>All program >Administrative tools>Server manager>features ) and make sure that’s in the feature list (In install  list SMTP Server should be there).  If it’s not, you’ll need to add it.

    2) Is SMTP configured?

     Just having SMTP installed isn’t enough.  For starters, you want to make sure the service is set to start automatically when your server boots up (I once made some nice coin troubleshooting a client setup where mail used to send but wasn’t anymore – they’d rebooted and the mail service stayed down, was all.)  Go into services (start>All program >Administrative tools>Services  or command Prompt type (services.msc)) and make sure that the service is in there, it’s already started, and has a startup type of automatic.

    C# Code below which is working for console apps (but not working in hosted Asp.net web application) :

                   var msgMail = new MailMessage(from mail, tomail, subject, body);

                    msgMail.IsBodyHtml = true;

                    var server = new SmtpClient("localhost");

                    server.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

                    server.UseDefaultCredentials = true;

     

                    server.Send(msgMail);

    -------------------------------------------code start for Asp.net Hosted application----------------------------------------------------------------------------

    C# Code below code working in hosted Asp.net web application :

       var msgMail = new MailMessage(from mail, tomail, subject, body);

                    msgMail.IsBodyHtml = false;

                    var server = new SmtpClient("localhost");

                    server.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

                    senderMail.PickupDirectoryLocation = @"C:\inetpub\mailroot\Pickup";               

                    server.UseDefaultCredentials = false;

    ----------------------------------------------------------------End of code---------------------------------------

    Cheers!!!

    Pushpendra singh

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 19, 2014 @ 12:47
    Dave Woestenborghs
    1

    We use smtp4dev (http://smtp4dev.codeplex.com/) for testing of sending e-mails on localhost. You just set your mail server in the web.config to 127.0.0.1 and you don't need any code hacks.

  • Pushpendra Singh 61 posts 116 karma points
    May 19, 2014 @ 13:12
    Pushpendra Singh
    0

    Hi Dawoe,

    Thanks for your reply.

    Yes your solution also working now.

    So,I am going with your soltion it's minimal line of code and efficient soltion as well.

    var msgMail = new MailMessage(from mail, tomail, subject, body);

                    msgMail.IsBodyHtml = false;

                    var server = new SmtpClient("127.0.0.1"); 

                     server.Send(msgMail);

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 19, 2014 @ 13:13
    Dave Woestenborghs
    0

    Actually you don't need to specify the IP adres when you initiate an new SmptClient object. It will look at the settings defined in the web.config

  • Pushpendra Singh 61 posts 116 karma points
    May 19, 2014 @ 13:21
    Pushpendra Singh
    0

    Thanks Dawoe.

    Complete Asp.net (c#) code to sent email using localhost:

               try

                {

                     var mail = new MailMessage();

                    MailAddress FromAddress = new MailAddress(ConfigurationManager.AppSettings["FromAddress"]);

                    mail.From = FromAddress;

                    mail.To.Add(ConfigurationManager.AppSettings["ToAddress"].ToString())

                    mail.Sender = FromAddress;

                    mail.Subject = "Custom Email Notification-Test mail";

                    mail.Body =

                        @"<html><head>

                            </head>

                            <body style='font-family: Trebuchet MS, arial, sans-serif; font-color: black;'><br/>Hi

                            " + "Custom Email Notification from localhost-test mail " +

                         "</body></html>";

                    var senderMail = new SmtpClient(ConfigurationManager.AppSettings["SMTPServerAddress"].ToString());// 127.0.0.1

                    senderMail.Send(mail);

                    errorlbl.Text = "Custom Email Notification from localhost Sent suceesfully";

                }

                catch (Exception ex)

                {

                    errorlbl.Text = ex.Message + ex.StackTrace;

     

                }

     

     

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 19, 2014 @ 13:24
    Dave Woestenborghs
    0

    You can also set the smtp server in the system.net section of your web.config

    <system.net>
        <mailSettings>
          <smtp>
            <network host="127.0.0.1" userName="username" password="password" />
          </smtp>
        </mailSettings>
      </system.net>

     

    So no need for creating a app setting for it. Here you can find the MSDN documentation for it : http://msdn.microsoft.com/en-us/library/ms164240(v=vs.110).aspx

    Dave

     

  • Pushpendra Singh 61 posts 116 karma points
    May 19, 2014 @ 13:38
    Pushpendra Singh
    0

    Hi Dawoe,

    above code working for me.For me not required username,password only i need the localhost. Either i can use 127.0.0.1 or "localhost" both is working fine for me i have tested.

    var senderMail = new SmtpClient(ConfigurationManager.AppSettings["SMTPServerAddress"].ToString());// 127.0.0.1 or "localhost"

Please Sign in or register to post replies

Write your reply to:

Draft