Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone!

I am creating a forgot password screen for my application for users to reset their passwords. The program is then supposed to send emails with the updated password to the email account associated with the username. The current status is is that the program freezes when it reaches the smtp.send(message) command and eventually times out. I've tried this with the gmail server information and my smtp server here at my work. No email is being sent at all. Does anyone know if this has to do with the code or with an email setting?

Thanks everyone!

C#
MailMessage message = new MailMessage();
message.To.Add(userEmail);
message.From = new MailAddress("***@gmail.com");
message.Subject = "New test mail";
message.Body = "Hello test message succeed";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.ASCII;
message.Priority = System.Net.Mail.MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new NetworkCredential("username", "password");

try
{
     smtp.Send(message);
}
catch (Exception ex)
{
     throw ex;
}
Posted
Comments
Thomas Daniels 30-Apr-13 11:34am    
Is the "username" when you create your NetworkCredential equal to "***@gmail.com" (the From property of your message)?
joshrduncan2012 30-Apr-13 11:41am    
No, the username just has to match the email address associated with the account, otherwise, the reset password will fail.
Dave Kreskowiak 30-Apr-13 14:15pm    
He's saying that the "username" and "password" should be gmail account that is going to send the email, not the users account that you're changing the password for.

1 solution

The code LOOKS correct, but without knowing the exception that's thrown, it's difficult to say.

The network credentials must be a valid GMail login name.

Your work server cannot lbokc access to the GMail servers (most corporate networks do!)

You cannot use your GMail email address in the From field if you're going to use your corporate SMTP server. Chances are really good that you must use an address that is valid in your corporate email system.
 
Share this answer
 
v2
Comments
joshrduncan2012 30-Apr-13 12:15pm    
Thanks Dave. The exception I'm getting is a smtp exception (The operation has timed out).
Dave Kreskowiak 30-Apr-13 14:16pm    
It's possible that your corporate network is blocking access to GMail.
joshrduncan2012 30-Apr-13 14:19pm    
Thanks Dave. I actually got it to work with a gmail address in the from field and my personal credentials in the networkcredential section for testing purposes. Now comes the fun part of deciding what to use since I don't want to send out this program with my personal credentials coded into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900