尝试使用C#中的SMPT协议发送密码恢复电子邮件,出于某种原因,我似乎无法正确处理。我会非常感谢有人可以提供帮助。这就是我所依赖的。这是代码:
enter cpublic void sendEmailWithPass( string username , string email , string password)
{
try
{
var fromAddress = new MailAddress("xxx", "xxx");
var toAddress = new MailAddress(email, "CLIENT!");
const string fromPassword = "xxx";
string subject = "recover password";
string body = "heloo! n according to your request your password is: n " + password;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{ Subject = subject, Body = body })
{ smtp.Send(message); }
SendMessage("&answerForgotRequest&true!");
}
catch
{
SendMessage("&answerForgotRequest&failed!");
}
}
此外,这是对应于错误
的行 public void answerServer(string message)
{
string ans = message.Split('&')[2];
if (ans.StartsWith("failed!"))
{
MessageBox.Show("an error was occured while trying sending the mail");
}
]
由于您正在努力挣扎,因此您至少需要尝试抓住exception
,没有人可以帮助您,因为您只是将其扔掉:
try
{
// email code
}
catch(Exception ex)
{
// breakpoint this line (yes look up online how to do it)
MessageBox.Show(Ex.ToString());
// write it to file if you need to
//File.WriteAllText("someFileName.Txt",Ex.ToString() );
}
然后,当您有实际例外时,请粘贴它(或提出一个新问题),以便我们知道发生了什么。
您可能也想看看这些:
异常处理(C#编程指南)
使用Visual Studio调试器浏览代码