Shr*_*har 2 c# asp.net email smtp
protected void Button2_Click(object sender, EventArgs e) {
//string vv;
//vv = (string)Session["FID"];
DateTime sdt = DateTime.Today;
SqlConnection cn1 = new SqlConnection();
SqlCommand cmd4 = new SqlCommand();
cn1.ConnectionString = @"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True";
String test = DateTime.Now.ToString("dd.MM.yyy");
for (int i = 0; i <= GridView1.Rows.Count - 1; i++) {
string toemail = GridView1.Rows[i].Cells[2].Text;
string FID1 = GridView1.Rows[i].Cells[0].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
if (Ckbox.Checked == true) {
sendMail(toemail);
//ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email send Succesfully')</script>");
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email sent on " + test + "')</script>");
cn1.Open();
//cmd4.CommandText = "Insert into TrackingFaculty_det (EmailsentDate) values (@EmailsentDate) WHERE FID=@FID";
cmd4.CommandText = "update TrackingFaculty_det SET EmailsentDate=@Email WHERE FID=@FID ";
cmd4.CommandType = CommandType.Text;
cmd4.Connection = cn1;
cmd4.Parameters.Clear();
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
cmd4.Parameters["@Email"].Value = sdt;
cmd4.Parameters.Add("@FID", SqlDbType.VarChar, 10);
cmd4.Parameters["@FID"].Value = FID1;
cmd4.ExecuteNonQuery();
cn1.Close();
}
}
}
public void sendMail(String toemail) {
try {
MailMessage mail = new MailMessage();
mail.To.Add(toemail);
mail.From = new MailAddress("manipal.mcis1@gmail.com");
mail.Subject = "Remember Mail";
// string Body = "Please update profile";
//mail.Body = Body;
mail.Body = " Dear Sir/Madam \n\n\n Please update your profile. . \n\n\n Thanks & Regards \n\n\n MCIS,Manipal.";
//mail.Body = "<html><body> <h2" + "align=center>Dear Sir/Madam" + "</h2> Please update ur profile</body></html>";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("manipal.mcis1@gmail.com", "manipal15");
smtp.EnableSsl = true;
smtp.Send(mail);
} catch (Exception ex) {
//System.ArgumentException argx = new System.ArgumentException("There is some problem in sending mail please try again later");
//throw argx;
//Console.WriteLine("There is some problem in sending mail please try again later", ex.Message);
Response.Write(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
这些代码行可以发送一个mail使用smtp端口,它的工作正常.从另一个角度exceptions来看,可能存在某些可能存在输入的情况Dummy email,abc@gmail.com或者可能存在某些server issues邮件无法在该特定时间发送或者可能存在其他异常的情况,在那些实例中,异常处理函数必须来进入行为并应显示弹出窗口或某些错误消息,如邮件无法发送,有可能吗?
在SMTP服务器级别的问题应该用处理SmtpException.
因此,您可以按如下方式修改catch块
catch (SmtpException ex)
{
Response.Write(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
但是,有时无法通过SmtpException处理邮箱问题.
为此,您可以使用SmtpFailedRecipientException用于包装从单个邮箱报告的错误.此异常包含StatusCodeenum类型的属性,该属性将告诉我们错误的确切原因.
请参考以下catch块
catch (SmtpFailedRecipientException ex)
{
SmtpStatusCode statusCode = ex.StatusCode;
if (statusCode == SmtpStatusCode.MailboxBusy ||
statusCode == SmtpStatusCode.MailboxUnavailable ||
statusCode == SmtpStatusCode.TransactionFailed)
{
// Display message like 'Mail box is busy', 'Mailbox is unavailable' or 'Transaction is failed'
}
else
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此处理个别错误
| 归档时间: |
|
| 查看次数: |
5721 次 |
| 最近记录: |