I have a console application, through which I need to send out email notifications. I am able to login into mailbox using the same credentials in the browser.
From the exception thrown, it seems like I am not able to open connect with Office OWA through C# code and getting an error message: "Failure Sending mail" - Inner exception - "Unable to connect to remote server".
I have noticed that while login into mailbox from the browser, it 1st make me land on company-specific page based on email address domain and then ask to enter password to login into the mailbox.
Also, network credentials passed to open smtpClient connect need to have some admin privileges or elevated permissions in Office 365, or it can be a regular office 365 user with a mailbox.
Reference blog:
https://weblogs.asp.net/sreejukg/send-email-using-office-365-account-and-c?__r=8d721ba733714a1
https://learn.microsoft.com/en-us/Exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3?redirectSourcePath=%252farticle%252fHow-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4#HowtoconfigSMTPCS
try
{
String userName = "SenderEmailAddress#myCompanyDomain.com";
String password = "passwordgoeshere";
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("john.smith#myCompanyDomain.com"));
msg.From = new MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
using (SmtpClient client = new SmtpClient
{
Host = "smtp.office365.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 587,
EnableSsl = true,
})
{
client.Send(msg);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadKey();
}
Goal:
I should be able to send out emails from a shared mailbox if I open the connection using shared mailbox credentials.
Please add UseDefaultCredentials = false, otherwise it will select system or authenticated user credential for sending email
using (SmtpClient client = new SmtpClient
{
Host = "smtp.office365.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false
})
{
client.Send(msg);
}
or recommend to set like this
using (SmtpClient client = new SmtpClient
{
Host = "smtp.office365.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 587,
EnableSsl = true
})
{
client.UseDefaultCredentials = false;
client.Send(msg);
}
Related
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.
I need to send email using NTLM, currently, I'm using the following code to send email
SmtpClient objSmtpClient;
System.Net.NetworkCredential objNetworkCredential;
objSmtpClient = new SmtpClient("10.xxx.xxx.xxx", 587);
objSmtpClient.EnableSsl = true;
objNetworkCredential = new System.Net.NetworkCredential(userName, password);
try
{
string to = txtto.Text;
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress("from#email.com", "sendername");
objMailMessage.To.Add(new MailAddress("to#email.com"));
objMailMessage.Subject = "subject";
objMailMessage.Body = "body";
objMailMessage.IsBodyHtml = true;
objSmtpClient.EnableSsl = true;
objSmtpClient.UseDefaultCredentials = true;
objSmtpClient.Credentials = objNetworkCredential;
objSmtpClient.Send(objMailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " INNER EXCEPTION > "+ex.InnerException +" DATA > "+ex.Data);
}
The Above code works if I try to change the port to 25 and EnableSSL to false, But when I try to send it using 587 and setting EnableSSL to true it doesn't work.
I'm getting the following error, sometimes I get an Invalid Certificate error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.
I am also getting this error
I think the problem is with Authentication, how can I force to use NTLM
I talked with the IT team they installed a tool on my pc to check email, using that tool email was sent successfully.
The following are the setting which he applied in that tool
Can someone please help
I was testing, I had the same problem, I fixed it by disabling SSL security
SC.EnableSsl = false;
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
Can we use gmail account to send email in asp.net website from *localhost * (local machine) ? I am trying but badly unsuccessful. It works fine on hosting but donot work on my machine.
I have windows server 2003 on my machine, I have added port 587 and 465 in firewall in exceptions. In my gmail account I also have enabled POP and IMAP. Some people suggest to use port 465 and others say port 587 should be used. I tried both and below was my result:
Using port 465 it take time and finally give message that the opration has timed out. falure
Using port 587 it dont take time, show message "failuer sending email" with an inner expection "No connection could be made because the target machine actively refused it 72.14.213.109:587"
Below is my code, please guide me where I am wrong or what I should do.
thanks
public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Port = 587;
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
I use smtp.yandex.com. May be need change settings in your gmail account.
Try this firs: add:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
If that does not help, your mailaddress might not be complete, use
var fromAddress = new MailAddress("from#gmail.com", "From Name");
and in the credentials use
Credentials = new NetworkCredential(fromAddress.Address, ....
Hope that helps
This SmtpClient has always worked for me:
new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("noreply#my_domain.com", "password")
};
And it matches yours.
Have you been able to contact smtp.gmail.com on port 587 through any other app? Telnet on that port maybe? Am thinking it's probably a network issue, although you did state that your firewall was wide open it's not the only hop between you and google.
I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147