Instead of relying on my host to send email, I was thinking of sending the messages though my gmail account. The emails are personalized emails to the bands I play on my show. Has anyone had success doing this?
http://www.systemwebmail.com/ is probably the most absurdly complete site dedicated to a single .NET namespace...but it has EVERYTHING you could ever want to know about sending mail via .NET, be it ASP.NET or Desktop.
Be sure to use System.Net.Mail
, not the deprecated System.Web.Mail
. Doing SSL with System.Web.Mail
is a gross mess of hacky extensions.
using System.Net.Mail;
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
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);
}
The above answer doesn't work. You have to set "DeliveryMethod = SmtpDeliveryMethod.Network" or it will come back with a "client was not authenticated" error. Also it's always a good idea to put a timeout.
Revised code:
using System.Net.Mail;
using System.Net;
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "password";
const string subject = "test";
const string body = "Hey now!!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
http://csharp.net-informations.com/communications/csharp-smtp-mail.htm
bolton.
I always get System.Net.Sockets.SocketException it can't connect to remote computer
I found this interesting link: http://code.msdn.microsoft.com/CSharpGmail.
can't add a comment so adding an answer (don't know why it wont let me add comments).
it doesn't work for me when EnableSsl=true (only when false). I think the reason is : http://www.systemnetmail.com/faq/5.3.aspx