![](/img/trans.png)
[英]MailMessage System.Net.Mail.SmtpException, Error when trying to send email via C# form
[英]error when trying to send email using smtp 'System.Net.Mail.SmtpException' in System.dll
嘗試使用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() );
}
然后,當您遇到實際異常時,請粘貼(或提出新問題),以便我們知道發生了什么。
您可能也想看看這些
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.