Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms email SMTP , +
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!
 
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 30-Apr-13 6:10am
Comments
ProgramFOX at 30-Apr-13 11:34am
   
Is the "username" when you create your NetworkCredential equal to "***@gmail.com" (the From property of your message)?
joshrduncan2012 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
joshrduncan2012 at 30-Apr-13 12:15pm
   
Thanks Dave. The exception I'm getting is a smtp exception (The operation has timed out).
Dave Kreskowiak at 30-Apr-13 14:16pm
   
It's possible that your corporate network is blocking access to GMail.
joshrduncan2012 at 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)

  Print Answers RSS
0 OriginalGriff 381
1 Praneet Nadkar 237
2 Marcin Kozub 225
3 Sergey Alexandrovich Kryukov 195
4 Shweta N Mishra 161
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100