-
July 9th, 2009, 09:50 AM
#1
sending emails
I wrote the following piece of code for sending an email. First, I wanted to try it with gmail.
Code:
public class EmailSender
{
public static void SendEmail(
string from,
string to,
string smtpaddress, int port,
string username,
string password,
string title,
string body)
{
try
{
SmtpClient client = new SmtpClient(smtpaddress, port);
client.Credentials = new System.Net.NetworkCredential(username, password);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = title;
mail.Body = body;
mail.IsBodyHtml = true;
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Using this is pretty simple:
Code:
class Program
{
public static void Main(string[] args)
{
EmailSender.SendEmail(
"john.doe@gmail.com",
"jane.doe@gmail.com",
"smtp.gmail.com",
465,
"john.doe",
"password",
"test",
"<p>This is a test email!</p>");
}
}
This doesn't work. I get a SmtpException, "The operation has timed out."
I also tried it like this:
Code:
class Program
{
public static void Main(string[] args)
{
EmailSender.SendEmail(
"<john.doe@gmail.com>",
"<jane.doe@gmail.com>",
"smtp.gmail.com",
465,
"john.doe",
"password",
"test",
"<p>This is a test email!</p>");
}
}
Same problem. If I change the port to 587 (http://mail.google.com/support/bin/a...n&answer=13287), I get the following exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Anyone has suggestions what could be wrong?
Thank you.
-
July 9th, 2009, 10:15 AM
#2
Re: sending emails
I am sure that most of the code you posted leaves off the actual username, however, many places require that the username be the e-mail address. Have you tried the full email address as the username?
-
July 9th, 2009, 10:23 AM
#3
Re: sending emails
Originally Posted by sotoasty
I am sure that most of the code you posted leaves off the actual username, however, many places require that the username be the e-mail address. Have you tried the full email address as the username?
Yes, I did that too.
-
July 9th, 2009, 12:53 PM
#4
Re: sending emails
I got it: the order of these operations is essential:
Code:
client.Credentials = new System.Net.NetworkCredential(username, password);
client.UseDefaultCredentials = false;
it should be the other way around:
Code:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(username, password);
-
July 9th, 2009, 04:12 PM
#5
Re: sending emails
Yikes. Requiring params to be in a certain order isn't too cool.
Glad you got it figured out.
-
July 10th, 2009, 01:24 AM
#6
Re: sending emails
From MSDN :
Some SMTP servers require that the client be authenticated before the server sends e-mail on its behalf. Set this property to true when this SmtpClient object should authenticate using the default credentials of the currently logged on user. If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server.
The thing that is actually strange to me is that the default value is false, in any case, so the way I see it, only this :
Code:
client.Credentials = new System.Net.NetworkCredential(username, password);
Was needed.
-
July 12th, 2009, 06:52 PM
#7
Re: sending emails
This is great code, so I really wanted to try it.
However, when I implement it within a very simple Windows Forms app with a simple Send button, it appears to fail.
"Not responding... vshost.exe has stopped working."
No exception was thrown. (I coded it to generate a message box if any exception was encountered, but none was encountered.)
The code simply appears to hang on: client.Send(mail);
All of my parameters look OK. (I substituted my gmail account credentials and tried sending a test message to my hotmail account.)
My firewall does not appear to be blocking outgoing connections to the specified port.
So, does anybody have any ideas why this might be hanging for me?
Thanks.
-
July 12th, 2009, 08:36 PM
#8
Re: sending emails
The key to finally getting this to work for me was the following:
I was successful when I used port number 587.
(For whatever reason, using port 465 did not work for me.)
Also, btw: I was successful when I used my full gmail username.
(e.g., I used "my_username@gmail.com" rather than simply "my_username".)
In summary: Thank you very much, cilu! This is great!
Last edited by Fortress7; July 12th, 2009 at 08:39 PM.
-
July 14th, 2009, 03:31 PM
#9
Re: sending emails
Right. I forgot to mention that it doesn't work on port 465, only 587.
-
September 14th, 2010, 09:28 AM
#10
Re: sending emails
Originally Posted by cilu
I got it: the order of these operations is essential:
Code:
client.Credentials = new System.Net.NetworkCredential(username, password);
client.UseDefaultCredentials = false;
it should be the other way around:
Code:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(username, password);
you saved my day thanks
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|