tags:

views:

12914

answers:

10

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?

+9  A: 

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.

Adam Haile
Site has been updated to www.systemnetmail.com for .NET 2.0 and above.
greg7gkb
+48  A: 

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);
}
Domenic
When constructing the NetworkCredential, use fromAddress.Address, not .ToString()
SLaks
Note that this method could have the email being marked as spam, due to SPF (if it's implemented at the receiver).
Noon Silk
Could you explain more, silky, and perhaps suggest a fix?
Domenic
You can still get user not logged in errors if Google just suddenly decides you have sent too many in the past xx number of minutes. You should always add a trySend, if it errors sleep a while, and then attempt again.
Jason Short
Interesting note: If you swap 'UseDefaultCredentials = false,' and 'Credentials = ...' it won't authenticate.
md5sum
There are no problems with SPF using this method. Every email client can be configured to do exactly this. You just may get problems if you use your own server (i.e. something else than `smtp.gmail.com`) with `[email protected]` as sender. Btw: `smtp.gmail.com` automatically overwrites the sender address if it's not yours.
Meinersbur
Gmail checks SPF for regular incoming mail (that are destined to `[email protected]`; the smtp server for this is `aspmx.l.google.com` port 25). Although Gmail implements and checks SPF for incoming mail, it currently seems that it does not use it to detect spam (afaik due to user complaints since it breaks forwarding).
Meinersbur
+5  A: 

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);
            }
Donny V.
Just so you know, your response is what the comment system is for. Adding another answer just adds confusion.
Richard Szalay
Interesting; it works on my machine (TM). But since this seems plausible, I'll add it to my answer.
Domenic
Hmm my guess is that SmtpDeliveryMethod.Network is the default, but maybe the default gets changed when running in IIS---was that what you were doing?
Domenic
I ran it from a desktop app.
Donny V.
+1  A: 

http://code.msdn.microsoft.com/CSharpGmail

A: 
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.

bolton
A: 

I always get System.Net.Sockets.SocketException it can't connect to remote computer

Kamil Çakır
You should start a new thread if you are having a problem.
Mike Wills
+4  A: 

I found this interesting link: http://code.msdn.microsoft.com/CSharpGmail.

Icono123
+1 for searching.
David Lively
+1  A: 

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

thedrs
A: 

Here IS My Version: http://www.techiespider.com/?p=7

tehie