ASP Email sending error
I have found this site very useful for all my previously faced problems, However i couldnt get help with the following.
I have developed a website which is able to send emails. On localhost this works absolutely fine. when i say localhost, i am able to recive the emails sent, but when i upload onto server i face this error when it starts the process of sending emails.
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection fail开发者_C百科ed because connected host has failed to respond IPADDRESS:PORT"
Tried ping on the adrress for the port and ping is working.
Here is the code
string strFrm = ConfigurationManager.AppSettings["FromAddress3"].ToString();
string[] receive = {"emailaddress1","emailaddress2","emailaddress3","emailaddress4"};
string subject = "New registration";
string body = "<html><head><title>Registered Candidates</title></head><body>bla bla bla</body></html>";
//I however have put reg exp validator on the form
if(txtEmail.Text.Contains("@") && txtEmail.Text.Contains("."))
{
for (int i = 0; i <= receive.Length - 1; i++)
{
MailMessage msg = new MailMessage(strFrm, receive[i], subject, body);
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(msg);
}
Response.Redirect("Thankyou.html");
Web.config
<mailSettings>
<smtp from="from address">
<network host="server" port="25"
userName="username" password="password" />
</smtp>
</mailSettings>
Please help. I upload onto my server via precompilation of the site and upload the files.
Make sure you're pointing to an SMTP service on your production server, it may not work on "localhost" as it does on your development machine. And pinging the server doesn't really tell you if it has SMTP enabled.
I generally prefer setting up SMTP for my sites in web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="you@yourdomain.com">
<network host="localhost" port="25" userName="user" password="pass" />
</smtp>
</mailSettings>
</system.net>
UPDATE:
If your code is working on your development machine, and it fails on the server with the same configuration, then there's probably something blocking. I would suggest trying to play around with a simple implementation that does nothing but test the servers SMTP configuration. You may want to try the <smtp deliveryMethod="SpecifiedPickupDirectory">
, it's quite helpful when testing code that sends out emails. See the SmtpDeliveryMethod Enumeration on MSDN.
Solved.. :D
My hosting server was godaddy and my hosting plan supported only age old system.web.mail i.e CDOSYS concept.
Here is the code.
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress@domainname";
oMail.To = "emailaddress@domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thanks Jakob for participating actively! :)
精彩评论