Hi Dan,
I know this code does the job:
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("[email protected]",
"mypassword");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
MailAddress maFrom = new MailAddress("[email protected]", "Sender's
Name", Encoding.UTF8);
MailAddress maTo = new MailAddress("[email protected]", "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);
On Wed, Sep 14, 2011 at 9:37 PM, DanH <[email protected]> wrote:
> 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("
> [email protected]", "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("[email protected]");
> message.To.Add("[email protected]");
> 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="[email protected]">
> <network host="smtp.gmail.com" userName="[email protected]"
> 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
>
>
> --
> 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
> [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-apps-mgmt-apis?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Google Apps Domain Information and Management APIs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-apps-mgmt-apis?hl=en.