Exception thrown while trying to send mail through gmail SMTP - c#

The below exception is thrown while trying to send mail by below code:
SMPT server requires a secure connection or the client was not authenticated. The server response was 5.5.1. Authentication required
C#
string E_mail_ID = "hidden";
try
{
SmtpClient gmail_client = new SmtpClient("smtp.gmail.com");
gmail_client.Port = 587;
gmail_client.EnableSsl = true;
gmail_client.Timeout = 100000;
gmail_client.DeliveryMethod = SmtpDeliveryMethod.Network;
gmail_client.UseDefaultCredentials = false;
gmail_client.Credentials = new NetworkCredential("hidden", "hidden");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("hidden");
msg.To.Add(E_mail_ID.Trim());
msg.Body = "Request for quotation from Jeet fly ash products, a unit of Vidya shakti niyas";
msg.Attachments.Add(new Att![enter image description here][1]achment(pdfFile));
gmail_client.Send(msg);
MessageBox.Show("RFQ sent to vendor successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Try the port: 465 - This is ssl for gmail.
Take a look: Configure Gmail-Account Outlook

Related

Send mail from office 365 for business in C#

I tried sending mail in my ASP.NET CORE project by C#. I ran code in emulation (IIS Express) but I get an error:
The SMTP server requires a secure connection or the client was not authenticated.
Please can you help me?
My code:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("formail#mail.com", "John Doe"));
msg.From = new MailAddress("frommail#mail.com", "Jane Doe");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("frommail#mail.com", "Password");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
return Content("Message sent successfully");
}
catch (Exception ex)
{
return Content(ex.ToString());
}
Full error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM3PR07CA0113.eurprd07.prod.outlook.com]
Thank you very much.
EDIT:
I used code from this question. But I get error
System.Net.Sockets.SocketException (11001): No such host is known.
My mail adress is me#gjb-spgs.cz.
My new code:
var sClient = new SmtpClient("gjbspgs-cz.mail.protection.outlook.com");
var message = new MailMessage();
sClient.Port = 25;
sClient.EnableSsl = true;
sClient.Credentials = new NetworkCredential("me#gjb-spgs.cz", "Password");
sClient.UseDefaultCredentials = false;
message.Body = "Test";
message.From = new MailAddress("me#gjb-spgs.cz");
message.Subject = "Test";
message.To.Add(new MailAddress("someone#gmail.com"));
sClient.Send(message);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}

send email with Restricted Gmail SMTP server

i am trying to send a email with google's Restricted Gmail SMTP server
this is my code:
try
{
var mail = new MailMessage();
mail.From = new MailAddress("from#email.com");
mail.To.Add("myemail#gmail.com");
mail.Subject = "subject";
mail.Body= "body";
SmtpClient SmtpServer = new SmtpClient("aspmx.l.google.com");
SmtpServer.Port = 25;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
What am I doing wrong?
this is the output of my catch:
System.Net.Mail.SmtpException: Error sending e-mail. ---> System .net. WebException: Cannot connect to the remote server--->
Help please

Send email with an attachment

Good day guys,
I have a code that is used to send an email with attachment which I had referred from Finding the exact cause for the exception - System.Net.Sockets.SocketException
Here is the code:
namespace SendEmail
{
class Email
{
public static void Main(string[] args)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
try
{
mail.From = new MailAddress("harish.1138#gmail.com");
mail.To.Add("harish_1138#yahoo.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138#gmail.com", "SamplePWD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
When I execute this, I get an exception as this:
Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
How do I fix the authentication required exception. Does this means something is blocking me from sending out the email?
Thanks a lot
I had make it to work...
Referred here: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?
as suggested by #ymonad.
namespace SendEmail
{
class Email
{
public static void Main(string[] args)
{
try
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("harish.1138#gmail.com");
mail.To.Add("harish_1138#yahoo.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
mail.Attachments.Add(attachment);
using (SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587))
{
SmtpServer.UseDefaultCredentials = false; //Need to overwrite this
SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138#gmail.com", "SamplePWD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
}
MessageBox.Show("Mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
And have to Turn on less secure apps as here:
https://stackoverflow.com/a/38024407/5266708
And it works perfectly...
This exception is thrown, when you do not have access to the SMTP server.
Usually you log in to email with your password and username, when it comes to SMTP server requests, you send your IP as login credential.
If your IP is not authorized, you will not be allowed to use it and will be blocked.
You need to provide credentials, to override the IP authentication:
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer .Host = "mail.youroutgoingsmtpserver.com";
SmtpServer Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
If it does not allow you, then google is blocking your access.
Use this for reference:
Google mail api

Unable to connect to local server in C# mail program

I am unable to connect to the frontier mail server with the following code. I get the message "Unable to connect to remote server". I am running the program using C# on my local computer.
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.frontier.com");
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx#frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(thisimage);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
Can anyone tell if I have the correct parameters for Frontier mail? I know they use Yahoo but I tried that also with no success. Isn't it possible to run a mail server from my local machine? Any help is appreciated.
just try remove this code SmtpServer.EnableSsl = true;
Does your ISP block SMTP traffic? (this is often the case for non-commercial accounts).
If not... rewrite you code closer to this:
try
{
using (var attachment = new Attachment(thisimage))
using (var mail = new MailMessage())
{
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx#frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
mail.Attachments.Add(attachment);
using (var client = new SmtpClient("smtp.frontier.com"))
{
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
client.EnableSsl = true;
client.Send(mail);
}
}
MessageBox.Show("Mail sent");
}
catch (SmtpException ex)
{
// go read through https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
// go read through https://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception(v=vs.110).aspx
}
#if DEBUG
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
#endif
}
...and run it in a debugger and look at what the SmtpException is reporting. There are myriad reasons why you may fail a connection.
Im unable to comment so I will type my comment as an answer. Are you able to use ImapClient instead of SmtpClient ? With Imap, you can do some authenticating processes. Could be the issue, it only looks like you are signing in. For Imap I do this:
using (var clientTest = new ImapClient())
{
clientTest.Connect("xxxx#frontier.com");
clientTest.AuthenticationMechanisms.Remove("XOAUTH");
clientTest.Authenticate(eMail, pw);
bIsConnected = clientTest.IsConnected;
if (bIsConnected == true)
{
/// Insert Code here
}
}

C# winforms sending email exception

I have a winforms app that sends email with code below:
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.Priority = MailPriority.High;
mail.From = new MailAddress("autoemailer#no-reply", "Auto Email");
mail.Subject = "Test Email";
mail.Body = "Kindly disregard if received";
SmtpClient smtp = new SmtpClient();
smtp.Host = "172.16.224.7";
smtp.Port = 25; //587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("username", "password"); // Sender's user name and password. Note: The username and password here is an example. Real username and password are the same as outlook's
try
{
smtp.Send(mail);
MessageBox.Show("Sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I'm getting the following exception:
Insufficient system storage. The server response was: 4.3.1
Insufficient system resources
The exception message was clear that the server is low on storage/resources. But when I tried sending email thru Outlook, it sent successfully. Is there something wrong with the code? TIA for any help.

Categories