C# winforms sending email exception - c#

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.

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());
}

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();
}

How to fix the smtp authentication runtime error when sending email using smtp client?

I'm sending simple email messages in my application using smtp client and I was using this code before and it just works fine. Now, when I tried to run my project again from my local host computer and try to send email messages. I got a runtime error that says
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
I don't know what just happened since it was working fine before. Now I can't send email and all I've got is this error. I could hardly troubleshoot what went wrong. How do I resolve this? Here's my code below: Thanks...
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(#"myemailaddress#gmail.com",#"myemailpassword");
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = TextBox2.Text;
message.Body = TextBox3.Text; //body of the message to be sent
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
// message.Subject = "subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
try
{
client.Send(message);
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Mail has been successfully sent!')", true);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.
Also please go to this link and click on Continue Allow access to your Google account
also I edit it little bit :
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}

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.

Exception when sending email in asp.net mvc application

I am trying to test out sending an email in my asp.net mvc application but I keep getting the exception below thrown:
"An attempt was made to access a socket in a way forbidden by its access permissions ipaddress:587"
Any ideas what it could be? I have turned off windows firewall and I still get the exception. I have included the using System.Net.Mail; statement and my code is below:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.Credentials = new System.Net.NetworkCredential("info#mydomain.com", "mypassword");
mail.From = new MailAddress("info#mydomain.com");
mail.To.Add("myemail#gmail.com");
mail.Subject = "Testing application";
mail.Body = "Testing the application email";
SmtpServer.Send(mail);
This is a code that working. Have two extra setting compare it with yours, maybe this is the issue.
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}

Categories