I am getting a error while compiling my program, this program is written to send a automatic mail after 10 seconds. But an error occurs and that says smtp server cant be connected...please help me with correct response.. and the following is the code which i wrote
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;// for backgroundworker class
using System.Net;
using System.Net.Mail;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
BackgroundWorker bw;
protected void Page_Load(object sender, EventArgs e)
{
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = false;
}
public void SendMail()
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Body = "Testing the automatic mail";
msg.IsBodyHtml = true;
msg.Subject = "Movie Data";
SmtpClient smt = new SmtpClient("smtp.gmail.com");
smt.Port = 587;
smt.Credentials = new NetworkCredential("[email protected]", "zzzzzzzz");
smt.EnableSsl = true;
smt.Send(msg);
string script = "";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "sendMail", script);
}
public void bw_DoWork(object sender, DoWorkEventArgs e)
{
SendMail();
}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime current_time = DateTime.Now;
current_time = current_time.AddSeconds(10);
Thread.Sleep(10000);
if (current_time == DateTime.Now)
{
bw.RunWorkerAsync();
}
}
}
please reply as soon as possible to my email id,...
Thanks in advance....
Loading
Abhishek HottePosted Jan 26, 2015, 4:43 AM
Hi,
try this code
MailMessage objMail = new MailMessage();
Pradeep ShetPosted Jan 21, 2015, 1:44 AM
Code seems to be correct, It happens most of the time when yo use gmail smtp. Try using port 465. 1 point would like to suggest you, that you have shown alert directly after calling Send(). Actually there is one Completed event which you can use to show message, it will assure you that email is sent.
Mark it as answered if it helped you.