GMail SMTP via C# .Net errors on all ports
I tried your code, and it works prefectly with port 587, but not with 465.
Have you checked the fire wall? Try from the command line "Telnet smtp.gmail.com 587"
If you get "220 mx.google.com ESMTP...." back, then the port is open. If not, it is something that blocks you call.
Daniel
Sending email through Gmail SMTP server with C#
CVertex, make sure to review your code, and, if that doesn't reveal anything, post it. I was just enabling this on a test ASP.NET site I was working on, and it works.
Actually, at some point I had an issue on my code. I didn't spot it until I had a simpler version on a console program and saw it was working (no change on the Gmail side as you were worried about). The below code works just like the samples you referred to:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
}
}
I also got it working using a combination of web.config, http://msdn.microsoft.com/en-us/library/w355a94k.aspx and code (because there is no matching EnableSsl
in the configuration file :( ).
2021 Update
By default you will also need to enable access for "less secure apps" in your gmail settings page: google.com/settings/security/lesssecureapps. This is necessary if you are getting the exception "`The server response was: 5.5.1 Authentication Required. – thanks to @Ravendarksky
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
This generally happens when you try login from different time zone or IP Address Computer. Your production server and the mail id you have used both are in different time zone. Choose either of these two solutions:
1) Log in to production server via remote access, and sign in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.
Or 2) log in gmail to your local computer, Follow this Link and choose review this activity and take proper actions.
Sending email via Gmail SMTP hangs indefinitely without error
OP here. As some answers allude, there was nothing wrong with my code. I'm not sure which of the below I had changed without retesting, but this is what you must have to use Gmail SMTP:
- Use TLS port 587
- Set
SmtpClient.EnableSsl
totrue
- Enable MFA for the Google account and use an app password for the
SmtpClient.Credentials
. I needed to enable MFA to create an app password.
Related Topics
What Exactly Is an Assembly in C# or .Net
How to Set Up Httpcontent for My Httpclient Postasync Second Parameter
How to Handle Add to List Event
How to See What My Reactive Extensions Query Is Doing
Assign Format of Datetime with Data Annotations
Single Click Edit in Wpf Datagrid
How to Create a More User-Friendly String.Format Syntax
Get a List of Distinct Values in List
Convert Wpf (Xaml) Control to Xps Document
What Is a Good Unique Pc Identifier
Observablecollection and Item Propertychanged
Microsoft.Office.Interop.Excel Really Slow
Httpwebrequest Not Passing Credentials
Expression.Lambda and Query Generation at Runtime, Simplest "Where" Example
Get All Registered Routes in ASP.NET Core
Specifying Generic Collection Type Param at Runtime
My C# Application Is Returning 0Xe0434352 to Windows Task Scheduler But It Is Not Crashing