gpt4 book ai didi

c# - 在 C# .net 中发送邮件失败

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:23 25 4
gpt4 key购买 nike

我发送电子邮件的代码在 psat 两周内正常工作。但是现在它显示了类似

的错误

Failure Sending Mail.

我不知道为什么会出现这个问题。上周放了一些新的防火墙策略,是不是因为这个问题才出现?我该怎么办?

这是我的代码:

 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("XXXXXXX");
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("XXXXXX", "XXXXXXX");

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);
}

}

最佳答案

发送邮件需要开启587端口。就此与您的网络团队核实并确保它已打开

关于c# - 在 C# .net 中发送邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com