c# automated email not working [duplicate] - c#

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.

Related

cannot send email using office 365 credentials using C#

I am trying to send email with my office 365 credentials using C#. But It is failing to send and getting exception.
How to fix this? Is it possible to send from office 365?
foreach (var filename in files)
{
String userName = "sri#MyOffice365domain.com";
String password = "password";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.office365.com");
mail.From = new MailAddress("sri#MyOffice365domain.com");
mail.To.Add("senderaddress#senderdomain.com");
mail.Subject = "Fwd: for " + filename;
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filename);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.
How many emails are you going to send simultaneously?
foreach (var filename in files)
All Outlook APIs accessed via https://outlook.office.com/api or https://outlook.office365.com/api have a limit is 60 requests per minute, per user (or group), per app ID. So, for now, developers only can make limited APIs calls from the app. Read through the Microsoft Blog Post for more details on REST API call limitations.
Try to use the following code instead:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone#somedomain.com", "SomeOne"));
msg.From = new MailAddress("you#yourdomain.com", "You");
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("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = 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

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.

SMTP mail errors

I am developing a C# application to send mail using the SMTP server of our company mail. The following is the code.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("10.203.195.48");
mail.From = new MailAddress("");
mail.To.Add("");
mail.Subject = "filename";
mail.Body = "Report";
SmtpServer.Host = "ip address fo smtp mail server.";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "");
SmtpServer.Send(mail);
But I get this error:
mailbox unavailable.unable to relay.The system doesn't have internet connection.
The following code is for Gmail SMTP using port 587. You just change your port and SMTP.
Add Namespace
using system.net
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.From = new MailAddress("emailid");
MyMailMessage.To.Add("To");
MyMailMessage.Subject = "Feedback Form";
MyMailMessage.Body = "This is the test message";
MyMailMessage.IsBodyHtml = true;
SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("Username","password");
SMTPServer.EnableSsl = true;
try
{
SMTPServer.Send(MyMailMessage);
}
catch (Exception ex)
{
ex.message("error");
}
From Error: Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email):
Generally it occurs when you have a mail server (e.g. mailserver.com) from one domain, and the addresses are from other domains. Either the From or the To address need to belong to the domain (myname#mailserver.com or yourname#mailserver.com). If neither of the addresses belong to a domain that the mail server 'owns', then you are relaying, which is a spam technique and is generally not allowed nowadays."
See also blog post Mailbox unavailable. The server response was: 5.7.1 Unable to relay sendername.

Categories