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);
-------------------------------------------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);
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.
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"
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
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.
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);
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
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;
}
You can also set the smtp server in the system.net section of your web.config
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
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"
is working on a reply...