I'm trying to send email from Azure Web App using Office365. Later, we'll move it to Azure Web Job or Azure Functions.
The following sample code works in local development machine, as well as in Azure using on-premise SMTP server at same port 587. However, the same credentials as local machine throws the exception from smtp.office365.com in Azure.
It seems like the problem lies between Azure and Office 365. Any help would be greatly appreciated!
Sending email from an Azure App Service using an O365 SMTP server
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("To Email Address", "Benjamin"));
msg.From = new MailAddress("From Email Address", "You");
msg.Subject = "Azure Web App Email using smtp.office365.com";
msg.Body = "Test message using smtp.office365.com on Azure from a Web App";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("O365 UID", "O365 PASS");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg); <-- Line 34
Exception
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 [XXXXX.namprd02.prod.outlook.com] at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.MailCommand.Send(SmtpConnection
conn, Byte[] command, MailAddress from, Boolean allowUnicode) at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,
MailAddressCollection recipients, String deliveryNotify, Boolean
allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message) at line 34
You must modify your DNS records (SPF) on the domain of the expeditor.
For exemple if the expeditor is test#mydomain.com.
You must verify the DNS TXT record of "mydomain.com", it must containt :
v=spf1 include:spf.protection.outlook.com -all
You can check you DNS record with this website :
https://www.whois.com.au/whois/dns.html
Enter your domain and click the button "Do live DNS lookup", the DNS records will appears.
If there is not this TXT, your must modify it...
Look this link :
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_winother-mso_o365b/client-was-not-authenticated-to-send-anonymous/d405bcb0-f40c-42fa-b1b2-477597100123
Related
I am working on an ASP .Net MVC website and I've to send email through Godaddy smtp, Previously my site was developed in classic ASP and it was hosted on godaddy's web hosting (then it was working fine) but now I am hosting this site on IIS,
I am using following code to send email but it is not working
MailMessage msg = new MailMessage();
msg.From = new MailAddress(model.From);
msg.To.Add(model.To);
msg.Body = model.Body;
msg.Subject = model.Subject;
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net",25);
smtp.Credentials = new NetworkCredentials("support#{myCompanyName}.com",{password});
smtp.EnableSsl = False;
smtp.Send(msg);
I have also used dedrelay.secureserver.net instead of relay-hosting.secureserver.net host (as mentioned at https://pk.godaddy.com/help/what-is-my-servers-email-relay-server-16601) but both are not working
GoDaddy does not allow relaying through their server unless you are on one of their hosting plans that includes SMTP.
You can set your credentials in webconfig like (for godaddy)
<system.net>
<mailSettings>
<smtp from="your email address">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
and in c# you can use like
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");
message.To.Add(new MailAddress("your recipient"));
message.Subject = "your subject";
message.Body = "content of your email";
SmtpClient client = new SmtpClient();
client.Send(message);
It will work
Most SMTP servers are quite restrictive nowadays when it comes to outbound email. I recommend testing the parameters with an email client (or telnet, if you're into that kinda thing) before assuming that there is something wrong with the code. That might also give you an error message that helps debugging.
Some things that come to mind:
The server may check the FROM address against it's database, specifically the user account you use to authenticate. While you can put whatever you want in the header of the email, this field must be your the real address of the authenticated account and only that (no descriptive name).
The server may require the use of TLS encryption, regardless of the port.
Port 25 is quite common, but according to the official RFC mail submission should use port 587. Maybe try that.
It is possible that GoDaddy only allows connections from their own (hosting) servers to these SMTP relays.
Unless the connection fails completely (which would point to no. 4) the server should send some kind of error message at some point. As I wrote above, I would recommend testing/logging the communication, that should provide some insight.
Perhaps you should call up to your ISP, here in th Netherlands they mostly block port 25 because of malware and worms that used to send out email. It can be as simple as this. Have you tried telnetting from your local machine to the email server (telnet mailserver.io 25)? If this ends up in a time out you have your answer and the port is either filtered out at your ISP or from their end.
You can try this code
smtp.Host = "relay-hosting.secureserver.net"; smtp.EnableSsl = false; smtp.UseDefaultCredentials = false; smtp.Port = 25;
string From = "[MyGodaddyEMailAddress]"; //eg.info#mango.com
string FromPassword = "[MyGodaddyMailPassword]";
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(From);
msg.To.Add("[RecipientEmailAddress]");
msg.Subject = "[MailSubject]";
msg.Body = "[MailBody]";
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.[domain].com", 587); //eg. mail.mango.com
smtp.Credentials = new System.Net.NetworkCredential(From, FromPassword);
smtp.EnableSsl = false;
// Sending the email
smtp.Send(msg);
// destroy the message after sent
msg.Dispose();
Console.WriteLine("Message Sent Successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey();
I have a web page, In CONTACT US tab I have a forms user can only enter their Name, Email Id, Subject and message. Once they click the ok button I want to get those message to my hotmail account.
I tried some code. But it doesn't work.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail.text, txtPassword);
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject = "Demo";
msg.Body = "Hi there..";
string toAddress = "xxx#hotmail.com"; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"no reply \" <from#gmail.com>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try
{
smtp.Send(msg);
}
catch
{
throw;
}
This code I tried. But it's having a password. I want user to send email without password to my xxx#hotmail.com
Here is my form design
That depends on your host. Usually web hosts give you a local SMTP server, then you can use it to send any mail you want, just need to know the configuration settings and use them with the SmtpClient.
If your host doesn't offer smtp (very strange unless you're selfhosting the page) you can:
1-Install a local SMTP server (if you manage the server), this is the preferred solution.
2-Use an external service like google to send the mails, but then you need to create an account on the service and use these credentials, and have in account that Google has a lot of restrictions sending emails (limit per second, marking mails as spam, etc etc).
Preferred way to do this is to not have the mail be sent from the users e-mail, but rather have a dummy e-mail that sends the mails and contains the data the user entered. Not the best solution probably, but it doesn't require user credentials.
I want my application to send e-mail using 'SMTP over SSL' even if TLS is not supported by server.
So far I have tried
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("abc#xyz.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true; //true: sends using TLS, false: sends without security
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
by setting the property called EnableSsl, I can send mail over the servers which support TLS but I am not able to send it through server which only supports SMTP over SSL.
How can I give support for this SMTP/SSL method?
According to the SMTPClient spec:
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
You can try using System.Web.Mail.SmtpMail, which is deprecated, but which supports SSL:
https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail(v=vs.110).aspx
TBH I think you should place a caveat on your service and state that only SMTP servers that use TLS are supported. But at the end of the day, that is up to you.
This link shows one more way that I can send email using SMTP over SSL with the help of Collaboration Data Objects component. This way also supports embedding images to email.
Please change your code..
SmtpServer.EnableSsl = false;
I'm trying to send mail using SMTPClient in C#.
Everything was fine when I was sending mail using my Gmail account
using HOSTNAME: smtp.gmail.com
and PORT NUMBER: 587.
Here is the code which I used:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587)
//mail
mail.From = new MailAddress("someone#mydomain.com","myself");
mail.To.Add("itsme#gmail.com");
mail.Subject = "Test Mail C#";
mail.Body = "Hello";
mail.IsBodyHtml = true;
//smtpclient
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("someone#mydomain.com", "password");
Boolean MailSent=true;
try
{
SmtpServer.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message);
MailSent = false;
}
finally
{
if (MailSent == true)
MessageBox.Show("mail sent");
else
MessageBox.Show("Failed to send mail");
}
Here are the details provided by the hosting service provider:
Secure SSL/TLS Settings (Recommended)
Username: someone#opastonline.com
Password: Use the email account’s password.
Incoming Server: mocha7004.mochahost.com
IMAP Port: 993
POP3 Port: 995
Outgoing Server: mocha7004.mochahost.com
SMTP Port: 465
Authentication is required for IMAP, POP3, and SMTP.
Non-SSL Settings
(This is NOT recommended.)
Username: someone#opastonline.com
Password: Use the email account’s password.
Incoming Server: mail.opastonline.com
IMAP Port: 143
POP3 Port: 110
Outgoing Server: mail.opastonline.com
SMTP Port: 25 --> When raised a ticket they also suggested me to use 2525 or 25
Authentication is required for IMAP, POP3, and SMTP.
But when I replace them using the details provided by my host provider, I am unable to push the mail.
It shows different error messages when I change port numbers (which I was given by the provider):
port 25: The remote certificate is invalid according to the validation procedure.
port 2525: Failure Sending Mail
port 465: Operation timed out
I cross checked every possible thing, but seems I am lost somewhere.
Go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps . So that you will be able to login from all apps.
I'm working on an ASP.NET Web Forms app and I'm trying to programmatically send an email to myself. I'm using Gmail's SMTP client, and all is well except that when I send my message, I get this error:
"System.Net.Mail.SmtpException: 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"
If I go into my gmail account settings and enable an option that allows me to allow access for "less secure apps", everything works fine. I'm wondering how I can send my email with having this option enabled.
protected void sendEmail(object sender, EventArgs e)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new System.Net.NetworkCredential("myusername#gmail.com", "mypassword"),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
MailAddress from = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailAddress to = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailMessage message = new MailMessage(from, to);
message.Subject = "test";
message.Body = "test";
Attachment attachFile = new Attachment(#"pathtofile");
message.Attachments.Add(attachFile);
try { client.Send(message); }
catch (Exception email_exception)
{
System.Diagnostics.Debug.WriteLine(email_exception);
}
}
Gmail port 587 does not support SSL.
I think the following code should work for you.
MailMessage msg = new MailMessage();
msg.From=new MailAddress("yourmail#gmail.com");
msg.To.Add("receiver#receiverdomain.com");
msg.Subject="Your Subject";
msg.Body="Message content is going to be here";
msg.IsBodyHtml=false; //if you are going to send an html content, you have to make this true
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port=587;
NetworkCredential credential=new NetworkCredential("yourmail#gmail.com","your gmail password");
client.UseDefaultCredentials=false;
client.Credentials=credential;
client.Send(msg);
There is a possibility to use Google SMTP servers without 'Allow less secure apps' option, but you cannot use your standard google username and password. See my instructions on other post:
Is there a way to use ASP.NET to send email through a Google Apps acccount without selecting the 'Allow less secure apps' option?