gpt4 book ai didi

c# - 使用 C# 发送消息在本地工作,但在现场静默失败

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:50 26 4
gpt4 key购买 nike

我正在使用以下代码发送预订表格,但它在本地有效,但当我将其上线时却失败了,我不确定为什么:

   protected void Page_Load(object sender, EventArgs e)
{

}
protected void SendMail()
{
var sDate = startDate.Text;
var eDate = endDate.Text;

// Gmail Address from where you send the mail
var fromAddress = "xxx@googlemail.com";
// any address where the email will be sending
var toAddress = "silverhillcattery@hotmail.co.uk";
//Password of your gmail address
const string fromPassword = "xxx";
// Passing the values and make a email formate to display
string subject = YourSubject.Text.ToString();
string body = "From: " + YourName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "Contact Number: " + txtContactNumber.Text + "\n";
body += "Subject: " + YourSubject.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
body += "Pet Name(s): \n" + txtPetName.Text + "\n";
body += "Start Date: \n" + sDate + "\n";
body += "End Date: \n" + eDate + "\n";

// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
//here on button click what will done
SendMail();
DisplayMessage.Text = "Thank you for contacting us we will get back to you shortly. If you would like to talk to us, please call 0208 236 9572";
DisplayMessage.Visible = true;
YourSubject.Text = "";
YourEmail.Text = "";
YourName.Text = "";
Comments.Text = "";
}
catch (Exception) { }
}

它不发送消息或显示“感谢您联系...”

这是网站 www.silverhillcattery.co.uk

如有任何帮助,我们将不胜感激!

更新:

我已按照建议添加了错误消息,但现在出现以下错误:

“SMTP 服务器需要安全连接或客户端未通过身份验证”

我会调查这个问题。

最佳答案

我只能猜测您的凭据在您的本地环境中有效,但在您的实际环境中无效。如果您的代码因发送异常而失败,它将绕过显示代码并直接转到您的“捕获”代码。

为了调试它,我会将发送放在显示之后以查看发生了什么,或者更恰本地修改捕获(异常)以获取错误并显示它。

catch (Exception ex) 
{
DisplayMessage.Text = "Failed to send email. Error = " + ex.message;
DisplayMessage.Visible = true;
}

关于c# - 使用 C# 发送消息在本地工作,但在现场静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776053/

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