Send mail from office 365 for business in C# - 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());
}

Related

How do i send email programmitcaly in c#

I am trying to send email through gmail server but is giving exception.The exception thrown is --> SMTP server requires a secure connection or the client was not authenticated.The server response was 5.7.0.Authentication required.
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("account1#gmail.com");
mail.To.Add("account2#gmail.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment("C:\\file.zip"));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("account1#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

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

ASP.NET C# - Sending Email from Gmail/Yahoo Live SMTP Server not working

I am struggling with attempting to send email from a .NET application.
I have tried everything from making a new account both gmail and yahoo (changed the host for yahoo), as well as, changing the port using and not using mail.To, allowing less secure apps, i also tried enabling 2 step verification and giving an application password for gmail all of which have failed with the same caught error of: {"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required"}
This is my code:
SmtpClient smtpClient = new SmtpClient();
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail#gmail.com", "pw");
smtpClient.Credentials = credentials;
smtpClient.UseDefaultCredentials = false;
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("myemail#gmail.com");
mail.To.Add("myemail#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
try
{
smtpClient.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mail != null)
{
mail.Dispose();
}
}
Solution: as people may have the problem in the future the solution was to remove the line of code: smtpClient.UseDefaultCredentials = false;
smtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail#gmail.com", "pw");
smtpClient.Credentials = credentials;
Use those lines in such order.

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

Error whilst sending an email

I am trying to send email using c# following is my code.
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("kmrizwan.shahid#gmail.com");
msg.To.Add("kmrizwan.shahid#gmail.com");//Text Box for To Address
msg.Subject = "Testinng subject"; //Text Box for subject
msg.IsBodyHtml = true;
msg.Body = "testing comment is here..";//Text Box for body
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("kmrizwan.shahid#gmail.com", "");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = msg;
client.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
Giving following exception
It's common for internet providers to block the port used by SMTP, except to their own outgoing mailserver. The reason is to prevent spamming.
If that is the case for you, you need to use the mail server of your internet provider instead of the GMail server.

Categories