简体   繁体   中英

error when trying to send email using smtp 'System.Net.Mail.SmtpException' in System.dll

trying to send a password recovery email using the smpt protocol in c# and for some reason I can't seem to get it right. I will highly appreciate it of someone could help. This is what I was relying on . And Here's the code:

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

in addition, this is the line which corresponds to the error

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

]

Since you are struggling with this, you need to at least try to catch the exception , no-one can help you as you are just throwing it away

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

Then when you have an actual exception, please paste (or ask a new question) it so we know whats happening.

You might want to have a look at these too

Exception Handling (C# Programming Guide)

Navigate through code with the Visual Studio debugger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM