Send email with an attachment - c#

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

Related

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

c# automated email not working [duplicate]

This question already has answers here:
Sending email in .NET through Gmail
(26 answers)
Closed 5 years ago.
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("senttoemailaddress#gmail.com");
mail.To.Add("senttoemailaddress#gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("emailaddress.test#gmail.com", "passW0ord");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I'm trying to run the above code to automatically send emails, the code executes up until it reaches smtpServer.send(mail) then it just stops, the email address that I use is valid and the password is valid.
you need to do some configuration in the gmail account before you can send email via gmail
activate 2 steps authentication
enter this link and allow your email to be as SMTP server
i think this will solve the issue .
U have problem with Login\password. I've try your code and get an error:
internal class Program
{
private static void Main(string[] args)
{
try
{
var mail = new MailMessage();
var SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("senttoemailaddress#gmail.com");
mail.To.Add("senttoemailaddress#gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential("emailaddress.test#gmail.com", "passW0ord");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Console.Write("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
...5.5.1 Authentication Required. Learn more at...
Then i just change login\password to my and it works:
mail Send
Try with SMTP port: 465
Notice :
Google limits the amount of mail a user can send, via its portable SMTP server. This limit restricts the number of messages sent per day to 99 emails; and the restriction is automatically removed within 24 hours after the limit was reached.

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.

Exception thrown while trying to send mail through gmail SMTP

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

Categories