我已經創建了一個應用程序,因爲我已經給出了發送郵件的選項。它一直工作到昨天。但是,今天我收到了一個錯誤,發送郵件如「發送郵件失敗。」。爲此,我已在下面給出了我的代碼,請幫我解決它。C#發送郵件失敗
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox1.Text == "")
{
string alertmessage = "";
alertmessage = "Email ID. cannot be blank ";
this.CreateMessageAlert(this, alertmessage, "alertKey");
TextBox1.Focus();
}
else if (TextBox2.Text == "")
{
string alertmessage = "";
alertmessage = "CC To cannot be blank ";
this.CreateMessageAlert(this, alertmessage, "alertKey");
TextBox2.Focus();
}
else if (TextBox3.Text == "")
{
string alertmessage = "";
alertmessage = "Subject cannot be blank ";
this.CreateMessageAlert(this, alertmessage, "alertKey");
TextBox3.Focus();
}
else if (TextBox4.Text == "")
{
string alertmessage = "";
alertmessage = "Message Body cannot be blank ";
this.CreateMessageAlert(this, alertmessage, "alertKey");
TextBox4.Focus();
}
//else if (upSignature.FileName == "")
//{
// ctr = 1;
// string alertmessage = "";
// alertmessage = "Attachment Missing...";
// this.CreateMessageAlert(this, alertmessage, "alertKey");
// upSignature.Focus();
//}
else
{
string photo = "Enquiry" + Session["MRNO"].ToString() + FileUpload1.FileName;
string strpath = Request.MapPath("~/");
FileUpload1.SaveAs(strpath + "/Enquiry/" + photo);
try
{
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(TextBox1.Text.Trim()));
mail.From = new MailAddress("[email protected]");
mail.Subject = "Enquiry for MRNO " + " " + " " + Session["MRNO"].ToString() + " " + " " + "Reg.";
mail.CC.Add(TextBox2.Text.Trim());
mail.Body = TextBox4.Text.Trim();
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
//Attachment attach = new Attachment(strpath + "/Enquiry/" + photo);
//mail.Attachments.Add(attach);
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("[email protected]", "xxxxx");
//smtp.Credentials = new NetworkCredential("purchaseoeg", "xxxxx");
smtp.Send(mail);
string alertmessage = "";
alertmessage = "Mail Has Been Sent";
this.CreateMessageAlert(this, alertmessage, "alertKey");
// Page.RegisterStartupScript("close", "<script language=javascript>self.close();</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
catch (Exception ex1)
{
Response.Write(ex1.Message);
}
}
提供您掛在嘴邊的一句話的的InnerException,並且如果這些是你的實際憑據,請更改他們。 – Dominik 2012-08-13 11:08:09
準確地在哪條線上出現錯誤?當您在此處發佈您的代碼時,包含您的密碼也不是一個好主意。 – 2012-08-13 11:08:15
如果這是您示例代碼中的真實密碼,那麼您可能需要編輯該代碼... – drch 2012-08-13 11:08:21