How to send mail using code from Office365 when TLS is enabled? - c#

My client has an account in Office365 and he recently added this account in the website he is using. Now he says that he cannot send mails using this account, whilst other accounts are OK. He says that TLS is enabled in the account. Anyhow, I am able to login to the mail account. I checked the settings and found that the SMTP settings are as follows:
Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS
This is a sample code that I am using to check.
String userName = "365useraccount";
String password = "password";
MailMessage msg = new MailMessage();
msg.To.Add(new System.Net.Mail.MailAddress("test#test.com"));
msg.From = new System.Net.Mail.MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.office365.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Port = 587;
client.EnableSsl = true;
try
{
client.Send(msg);
}
catch
{
}
I am getting an exception with message:
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 [BMXPR01CA0043.INDPRD01.PROD.OUTLOOK.COM]
However, I am able to send mails using other Office365 accounts.
Any help of idea would be greatly appreciated

Related

Access denied when sending a mail from C# programm

I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.

Office 365 SMTP sending mails failed for some account

I tried 2 Office 365 accounts for SMTP mail sending. One is ok, the other one failed. I am pretty sure the account name and passwords are right.
Error message:
Message = "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
[DM3PR11CA0008.namprd11.prod.outlook.com]"
C# code:
int port = 587;
int.TryParse(configuration["smtp:port"], out port);
string host = configuration["smtp:server"] ?? "smtp.office365.com";
SmtpClient client = new SmtpClient(host, port);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(configuration["smtp:username"], configuration["smtp:password"]);
await client.SendMailAsync(mailMessage);

Not allow to send email on behalf of my client on office365

I have tried to send email to my client's customers with my client from address(info#myclient.com).
and my client has already configured my smtp server via SPF to their domain.
Following is C# code written to send the email.
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("info#mycient.com");
mail.To.Add("krishna.menan#gmail.com");
mail.Subject = "This is an email";
mail.Body = "this is a sample body with html in it. <b>This is bold</b>
<font color=#336699>This is blue</font>";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
System.Net.NetworkCredential networkCredentialRFE = new
System.Net.NetworkCredential("kirshna#mycomapny.com", "Password123");
smtp.Credentials = networkCredentialRFE;
smtp.Host = "outlook.office365.com";
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Send(mail);
Following is error I am getting:
Mailbox unavailable. The server response was: 5.7.60 SMTP; Client does not have permissions to send as this sender
Make sure that the taskuser you are using has the proper "send as" permissions, see "Give mailbox permissions to another user in Office 365 - Admin Help".

Can't send email using SMTP

I'm trying to send an email via .net smtp. I am receiving this message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("server", 587);
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.EnableSsl = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
mail.From = new MailAddress(FromMail);
mail.To.Add("emailaddress");
mail.Bcc.Add("emailaddress");
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
The issue I believe is that the servers are located in the DMZ...anyone have any insight on how to fix this? If we assign the user with domain admin rights, the emails work...due to security reasons we don't want to go that route.
Issue resolved...the network team didn't have port 587 available...once they added it, emails started working for anonymous sender. I no longer need to use credentials. Thanks to all who replied.

The SMTP server requires a secure connection when i try to send an email

Hi guys I create a mail class to send emails.
public void SendEmail(string subject, string messageBody, string toAddress)
{
MailMessage mail = new MailMessage();
mail.To.Add(toAddress);
//mail.To.Add("amit_jain_online#yahoo.com");
mail.From = new MailAddress("noreply3ncra#gmail.com");
mail.Subject = subject;
string Body = messageBody;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("noreply3ncra#gmail.com", "********");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
But I got this error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
So I have to sign into Gmail and enter the captcha code and after this every thing is going to be ok.
What should I do?
Try this:
You should enable application to access gmail account.
Try this link:
Gmail allow access
For more info refer this link:
Stackoverflow link
Add this also:
Add port no also to your code as:
smtp.Port = 587;
For more info refer this link:
Codeproject link

Categories