C# Unable to send email from G Suite Company account - c#

This is how I am sending email:
MailMessage m = new MailMessage();
m.From = new MailAddress("support#big-apps.org", "Big Apps.");
m.To.Add(new MailAddress("faizan003#gmail.com"));
m.Subject = "Test Subject";
m.Body = String.Format("This is Test email");
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential()
{
UserName = "support#big-apps.org",
Password = "mypassword"
};
smtp.EnableSsl = true;
smtp.Send(m);
Is there any setting that I need to enable in G Suite Admin?
I need to send email from support#big-apps.org

The port 465 is the issue. The SmtpClient EnableSsl option is actually using TLS. Gmail's TLS port is 587.
From Microsoft's Documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.enablessl?view=netframework-4.8
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

Related

C#. SMTP The server response was: 5.7.0 Authentication Required

Error: The SMTP server requires a secure connecton or the client was not authenticated. The server
response was: 5.7.0 Authentication Required. Learn more at.
private void SendMail(string email, string firstname)
{
MailAddress from = new MailAddress($"{email}", $"{firstname}");
MailAddress to = new MailAddress($"{email}");
MailMessage m = new MailMessage(from, to);
m.Subject = "TEST";
m.Body = "<h2>TEST</h2>";
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new NetworkCredential("jewralyapp#mail.ru", "password");
smtp.EnableSsl = true;
smtp.Send(m);
}
I want to send a letter to the mail, but the error that I indicated above comes out, here is the code, can you tell me what is wrong?
I see that your email is #mail.ru and the server is gmail, is that correct? I think you need to use your gmail account.
You also need enable weaker credentials on your gmail account. Forgot what they actually call it but it's on gmail security setting.
I had also faced the same problem.
Your SmtpClient is gmail, but you are using "jewralyapp#mail.ru"!!!
Make sure to set "Less secure app access" to ON in your Gmail security setting.
In my case, the problem was solved.

Can SmtpClient also send email directly to the receiver?

SmptClient can be used to send an email via relay server. But is SmtpClient also able to send email directly to the receiver and not via relay server?
EDIT
Any ideas how SmtpCLient needs to be configured to be able to send emails directly to the receiver?
I tried with the following code but I got "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// var credentialUserName = "myAccount#gmail.com";
var sentFrom = "myAccount#gmail.com";
// var pwd = "myPwd";
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("smtp.gmail.com");
client.Port = 587;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
/* System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(credentialUserName, pwd);
*/
client.EnableSsl = true;
// client.Credentials = credentials;
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return client.SendMailAsync(mail);
}
}
SECOND EDIT:
Thanx, it works now. App sent email directly ( and not via myAccount#gmail.com ) to otherAccount#gmail.com. Here's the code:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var sentFrom = "myAccount#gmail.com";
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("gmail-smtp-in.l.google.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return client.SendMailAsync(mail);
}
}
Thank you
Short answer: yes!
A relay server is just a server that is configured to accept all your emails and pass them on to the right destination. You can equally well contact the right destination server directly and delivery the email there.
This, of course, provided there is no firewall issues preventing you from contacting the destination server directly.
EDIT
The server smtp.gmail.com is for gmail users to send (outgoing) emails, i.e. you must authenticate with your gmail username and password in order to be allowed to send an email that way, but if you do that you can send to any recipient, i.e. also non-gmail addresses.
I understood your original question to mean you would like to send emails to (in this case) a gmail-address without using a proxy. In that case your client should behave as any arbitrary email server that is trying to send to a gmail-address, i.e. it should connect to one of official incoming SMTP servers for the domain as given by MX-records in the DNS. E.g. one of gmail's MX-records points to gmail-smtp-in.l.google.com, and if you connect to port 25 of that server you can submit an email to a gmail-address (and you can also completely spoof the sending address, but then spam-filtering might cause your email to not be delivered).
My caveat about firewall issues is to interpreted as this: most ISPs disallow outgoing TCP connections to port 25 to other hosts than their own servers. This is just because of the above mentioned spoofing possibility, i.e. if your ISP allows you to make TCP connections to port 25 of other email servers, you can use that to send spam. Therefore your ISP might not allow you to do that, and instead you should relay your emails via your ISP so they can take appropriate measures if you try to spam people.

How to send a Secure e-mail using SMTP

I am currently using Google Apps to send SMTP e-mails. If my project deploys some of the information that i am going to be sending will be confidential and i would like to make sure the transmission is secure. Can anyone please let me know what i need to do to ensure that i send a safe e-mail using smtp through the google apps smtp server? smtp.google.com.
Any help greatly appreciated.
From what I have been told i need to force Https and have a SSL cert in order to do this. I don't know if this is true?
You Can Use 'smtp.EnableSsl = true' For Enable SSL for security.
MailMessage mail = new MailMessage();
mail.To.Add("" + to + "");
mail.From = new MailAddress("" + from + "");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"";
mail.Body = Body;
mail.IsBodyHtml = true;
Attachment at = new Attachment(Server.MapPath("~/ExcelFile/TestCSV.csv"));
mail.Attachments.Add(at);
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential(""+ username +"", ""+ password +"");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Send(mail);
To enforce Network Security you have to use SSL. to enforce security of the data going from your webserver to mail server you need to send your mail over SSL. and to secure the HTTP request that triggers the mail action you need to enforce SSL over HTTP.
But the question is Security in what context ? If you need network security to ensure a 3rd party cannot eavesdrop or manipulate then SSL is your way to go.

Port for Sending Email for gmail account

Please can some one help me out whether the correct port for sending email from an gmail account is 25 ,465 or 587. I checked on some forms it says that C# classes does not support sending email using SSL it uses TSL and the port used for TSL is 587. IS it correct? I am confused.
Gmail usually works with Port Number 587 in .net Application.
Check the following Example Code sample
System.Net.Mail.SmtpClient SmtpClientObject = new System.Net.Mail.SmtpClient();
SmtpClientObject.UseDefaultCredentials = false;
SmtpClientObject.Credentials = new System.Net.NetworkCredential("MyUserName#gmail.com", "myPassword");
SmtpClientObject.Host = "smtp.gmail.com";
SmtpClientObject.Port = 587;
SmtpClientObject.EnableSsl = true;
SmtpClientObject.Send("MyUserName#gmail.com", "YourUserName#gmail.com", "TestSubject", "MessageBody");

smtp.send method throws exception

I am using .net version 3.5 for sending mails though my web application
I get the following error:
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.7.1 Client was not authenticated
What could be causing this?
Source from here http://www.systemnetmail.com/faq/4.2.aspx
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);
EDIT: This error means what it's written: smtp server needs authentication information (username and password). So, you need set SmtpClient.Credentials.
Try:
smtpclient.EnableSsl = true;
The SMTP server requires a secure
connection or the client was not
authenticated. The server response
was: 5.7.1 Client was not
authenticated
so you need to authenticate by adding credentials
smtpClient.Credentials = new NetworkCredential("username", "password");

Categories