-1

i have this snippet code to send an email but every time i excute it ,i get this exception

The wait operation period expired

public static void CreateTimeoutTestMessage(string server)
    {
        string to = "touilhaythem1@gmail.com";
        string from = "raddaouirami@gmail.com";
        string subject = "Using the new SMTP client.";
        string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
        MailMessage message = new MailMessage(from, to, subject, body);
        SmtpClient client = new SmtpClient(server, 587);
        client.EnableSsl = true;
        client.Credentials=new NetworkCredential("raddaouirami@gmail.com", "XXXXXXXXX");
        Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
        client.Timeout = 100;
        // Credentials are necessary if the server requires the client 
        // to authenticate before it will send e-mail on the client's behalf.
        //client.Credentials = CredentialCache.DefaultNetworkCredentials;

        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in CreateTimeoutTestMessage(): {0}",
                  ex.ToString());
        }

        Console.ReadLine();
    }
Rami Raddaoui
  • 75
  • 1
  • 5
  • 13
  • 6
    Hint: You're attempting to send via Gmail's SMTP server without supplying credentials for the account you're trying to send from. You also haven't specified the correct port for GMail's SMTP server (its `587` IIRC).. you should look at similar questions on here for the answers you require. – Simon Whitehead Apr 09 '14 at 12:44
  • Please read the manual: https://support.google.com/mail/answer/78775?hl=en – Uwe Keim Apr 09 '14 at 12:45
  • i added credentials for the account and the port for gmail and still got the same error. – Rami Raddaoui Apr 09 '14 at 13:14
  • possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Joe Apr 09 '14 at 15:17
  • I tried the code in the link below now i get this exception: 5.5.1 Authentication Required – Rami Raddaoui Apr 09 '14 at 16:21

2 Answers2

0

Of course you get a timeout - you specified 100ms for the timeout. That's pretty short. Contacting the server and sending the mail probably takes more than 100ms. Try something like 10000ms for ten seconds.

I know it's in the MSDN sample you copied and pasted from, but its way too short. It's best to remove the following lines:

Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
client.Timeout = 100;

Please try this (got the idea from here):

    ...
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server, 587);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false; // <--- NEW
    client.Credentials = new NetworkCredential("raddaouirami@gmail.com", "XXXXXXXXX");

It might wait for authentication.

Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

Where you have:

SmtpClient client = new SmtpClient(server, 587);

Break it up into:

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;

Because I don't see where you have defined server, and if you're trying to use gmail you can just declare it as the host like that to see if this gets you one step closer to your goal.

Also a side note, gmail smtp does not allow you to change the send from address, to prevent phishing, so if you did plan on allowing input to change the from variable it won't work, gmail defaults to the email provided in the credentials

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
Joe W
  • 1,567
  • 6
  • 23
  • 36
  • when i the CreateTimeoutTestMessage function i set the string server="smtp.gmail.com" i see no use in breaking the client into two parts. – Rami Raddaoui Apr 09 '14 at 13:53
  • like i said I did not see the definition of server, so I had to make sure the host was in fact being set. – Joe W Apr 09 '14 at 14:00