Connecting to gmail from ASP.NET app

27 views
Skip to first unread message

DanH

unread,
Sep 14, 2011, 12:07:09 PM9/14/11
to google-app...@googlegroups.com, dhur...@stersol.com
Hi,
I am trying to send mail via gmail from an ASP.NET app. It seems like this should be easy, and many code snippets on the web show how to do it.  However, I cannot get it to work.  When I use port 465, I get a timeout exception. When I use port 587, I get an "Authentication Required exception".  Here is my code.  I have tried all sorts of variations on the syntax, but to no avail:
 
SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
client.EnableSsl = true;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myuse...@mydomain.com", "password");
client.Credentials = cred;  // null if assign Credentials in one line of code
client.UseDefaultCredentials = false;
client.Timeout = 5000;
MailMessage message = new MailMessage();
message.From = new MailAddress("m...@gmail.com");
message.To.Add("some...@gmail.com");
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Subject = "The Subject";
message.Body = "this is the body";
try
{
  client.Send(message);
}
catch (Exception ex) { //  do something }
Here is the matching code from web.config. This may be redundant, but it doesn't seem to matter if I set the port & credentials here or in C#.
 
<system.net>
  <mailSettings>
    <smtp from="
m...@gmail.com">
    <network host="smtp.gmail.com" userName="
myuse...@mydomain.com" password="password"  />
    </smtp>
  </mailSettings>
</system.net>
 
I am quite sure my actual username and password are correct because I can manually log into gmail using those credentials. 
 
Help!  I am stuck here.
 
Thanks.
 
Dan
 

Claudio Cherubino

unread,
Sep 14, 2011, 10:36:23 PM9/14/11
to google-app...@googlegroups.com, dhur...@stersol.com
Hi Dan,

I know this code does the job:


SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("user...@mydomain.com", "mypassword");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;

MailAddress maFrom = new MailAddress("user...@mydomain.com", "Sender's Name", Encoding.UTF8);
MailAddress maTo = new MailAddress("reci...@otherdomain.com", "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom, maTo);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.BodyEncoding = Encoding.UTF8;
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;

client.Send(mmsg);


--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-apps-mgmt-apis/-/NIkmC_yOvyAJ.
To post to this group, send email to google-app...@googlegroups.com.
To unsubscribe from this group, send email to google-apps-mgmt...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

DanH

unread,
Sep 15, 2011, 9:32:41 AM9/15/11
to google-app...@googlegroups.com, dhur...@stersol.com
Finally figured it out, with help from another forum.  First, the correct port is 587.  Second, omit the line which sets the client.UseDefaultCredentials property.
 

The default value of SmtpClient.UseDefaultCredentials is false. But if that value is explicitly set, the request generates an exception. Omitting the line solves the problem.  Go figure.

Dan

Mally Mclane

unread,
Sep 15, 2011, 9:53:38 AM9/15/11
to google-app...@googlegroups.com, dhur...@stersol.com
On Thu, Sep 15, 2011 at 2:32 PM, DanH <dma...@parentsinapinch.com> wrote:
> Finally figured it out, with help from another forum.  First, the correct
> port is 587.  Second, omit the line which sets the
> client.UseDefaultCredentials property.


465 will also work, depending on your security choices:

https://mail.google.com/support/bin/answer.py?answer=13287

m

Reply all
Reply to author
Forward
0 new messages