C# Smtpclient Class Not Able to Send Email Using Gmail

C# SmtpClient class not able to send email using gmail

You won't believe what fixed my problem.

The Credentials property

ss.Credentials = new NetworkCredential("username", "pass");

must be declared after

ss.UseDefaultCredentials = false;

So the final working code listing is

SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
ss.Credentials = new NetworkCredential("username", "pass");

MailMessage mm = new MailMessage("donotreply@example.com", "destination@example.com", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);

Is this a bug?

Can't send email through smtp gmail

There is answer somewhere but i do not recall where so i will write you code below. If someone find answer please put in comment and i will point it there.

var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("yourmail@gmail.com", "yourpw")
};

MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = "Subject";

msg.From = new MailAddress("yourmail@gmail.com");

msg.Body = "Body here";
msg.Bcc.Add(li[i].Value);
smtpClient.Send(msg);

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

Sending email in .NET through Gmail

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;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "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);
}

Additionally go to the Google Account > Security page and look at the Signing in to Google > 2-Step Verification setting.

  • If it is enabled, then you have to generate a password allowing .NET to bypass the 2-Step Verification. To do this, click on Signing in to Google > App passwords, select app = Mail, and device = Windows Computer, and finally generate the password. Use the generated password in the fromPassword constant instead of your standard Gmail password.
  • If it is disabled, then you have to turn on Less secure app access, which is not recommended! So better enable the 2-Step verification.

How to send email via SMTP from my corporate gmail address using C#

I just tested with your code on my company G-Suite account.

After allowing users to manage their own settings, you need to make sure that the username provided in the NetworkCredential object has been configured to "Allow less secure apps". You can get to it by:

  1. Enable users to manage their own settings (you've already done this)
  2. Going to (in this case) to the Google Account Page for info@mydomain.com.
  3. Clicking "Sign In and Security"
  4. Scroll to the bottom and make sure the slider is on.

The first step may take some time to propagate to all users in G-Suite.

Note: You will also get this error if the password or username are incorrect.

If you've done all of the above and it still doesn't work, you may need to configure SPF in your DNS settings. See Authorize email senders with SPF.

Gmail email send c#

Try formatting the message as I have here. and use Authenticate instead of NetworkCredential.

using MailKit.Net.Smtp;
using MimeKit;

Console.WriteLine("Hello, World!");

var message = new EmailMessage()
{
From = "xxxxx@gmail.com",
To = "xxx1@gmail.com",
MessageText = "test",
Subject = "test"
};

try
{
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 465, true);
client.Authenticate(message.From, "AppsPassword");
client.Send(message.GetMessage());
client.Disconnect(true);
}


}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

public class EmailMessage
{
public string To { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public string MessageText { get; set; }

public MimeMessage GetMessage()
{
var body = MessageText;
var message = new MimeMessage();
message.From.Add(new MailboxAddress("test", From));
message.To.Add(new MailboxAddress("test", To));
message.Subject = Subject;
message.Body = new TextPart("plain") { Text = body };
return message;
}
}


Related Topics



Leave a reply



Submit