C# Mail Sending Error in smtp.office365.com SMTP Server - c#

I am using below Code for SMTP Configuration.
SmtpClient smtp = new SmtpClient
{
Host = data.SMTPServer, // smtp server address here...
Port = data.PortNo,
EnableSsl = data.SSL,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
smtp.Send(message);
One client has configured SMTP Server - smtp.office365.com.
SMTP Server throws back below Error - Without SSL :-
Error in processing. The server response was: 5.7.3 STARTTLS is required to send mail [XXX.XXX.PROD.OUTLOOK.COM]
With SSL :-
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. [XXXX.XXXX.PROD.OUTLOOK.COM]
Above SMTP Code works perfectly for rest of Clients.
Any inputs what needs to be done to fix this?

Make sure that the points below are correct:
credentials are correct
use the right port number (587 or 25)
application must be able to use TLS 1.2 or higher ( you can test this by adding the following code before calling the service: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12)

Related

C# - Sending an email, via Gmail or Other?

Server: VDS
OS: Windows Server 2008 R2
Application: None
Library (DLL used by an application): Yes, C#
I am trying to send mail via C# using from what I read, gmail service. Basically just a test email to myself would be a start to know it works. If you have to ask, the information is stored in config.json file rather than directly in the code, hence the "AccountRecovery.AccountRecoveryConfig".
I cannot seem to get it to work! When using certain ports I get different errors!
PORT 465 - With Credentials
ERROR:
2016-02-05 02:52:33 - Command: ERROR: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
PORT 587 - With Credentials
ERROR:
2016-02-05 02:55:50 - Command: 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
I have no idea what to do. Am I doing something wrong?
public static void SendEmail(string email)
{
MailMessage mail = new MailMessage(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, email);
SmtpClient client = new SmtpClient();
client.Timeout = 30000;
client.Host = AccountRecovery.AccountRecoveryConfig.HostSMTPServer;
client.Port = AccountRecovery.AccountRecoveryConfig.HostPort;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, AccountRecovery.AccountRecoveryConfig.ServerEmailPassword);
client.EnableSsl = true;
//client.ServicePoint.MaxIdleTime = 1;
mail.Subject = AccountRecovery.AccountRecoveryConfig.EmailSubjectLine;
mail.Body = AccountRecovery.AccountRecoveryConfig.EmailBodyLine;
mail.IsBodyHtml = false;
client.Send(mail);
}
The correct port is 587 for google, this error:
2016-02-05 02:55:50 - Command: 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
You should give access to less secure applications. Here is the LINK where you can do it for your current logged google account.

Send email using smtpClient and smtpserver other than smtp.gmail.com

I searched and came across many codes demonstrating mail sending using gmail's smtp with c#
Here is my smtpclient configured to send email.
SmtpClient smtp = new SmtpClient
{
Host = MailSettings.SMTP, // smtp server address here...
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = credentials,
Timeout = 30000,
UseDefaultCredentials = false
};
This code works very fine with host as smtp.gmail.com
But when it comes to other Hosts, it fails.
It doesnt send mails.
Can anyone help me with this.
Can you give more details about the error you are getting?
Several things can cause this:
Server Configuration: are the ports correct? the server does not require authentication? the server supports ssl?
SSL Certificate: the certificate is self-signed? If so you should look here;
Regards,

SmtpClient wont authenticate over SSL/TLS (not pointing to gmail)

I have an ssl/tls server (nodejs) that acts as a proxy to postfix/sendmail to perform some pre-processing/data aquisition on outgoing mail.
From C#, I can manually connect and authenticate with the following code:
var sslStream = new SslStream(tcpClient.GetStream(), false,
new RemoteCertificateValidationCallback(CertificateValidation),
new LocalCertificateSelectionCallback(CertificateSelectionCallback));
string fn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cert.pem");
X509Certificate c = X509Certificate.CreateFromCertFile(fn);
var certs = new X509CertificateCollection();
certs.Add(c);
sslStream.AuthenticateAsClient(System.Environment.MachineName, certs , SslProtocols.Default, false);
However, I can not get the SmtpClient to connect. The top level error is a timeout, but I have debugged into SmtpClient/SmtpConnection and the underlying error is that the stream is not readable, presumably because it never authenticated (I cant hit a break-point in my ssl/tls proxy server with the SmtpClient code, but the manual sslConnection above works just fine).
It would be great if there was a way to manually supply the underlying communication stream to SmtpClient but I cant find a way to do it.
Anyone have an idea as to why the code below wont authenticate?
Here is the test app I have been using to try and connect with SmtpClient without success:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidation;
// Using Ssl3 rather than Tls here makes no difference
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
var client = new SmtpClient {
Timeout = 10000,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
UseDefaultCredentials = false,
Host = "192.168.15.71",
Port = 10126
};
client.ClientCertificates.Add(c);
client.Credentials = new NetworkCredential("username", "password");
//times out here, except the real exception that doesn't bubble up is the stream
//isnt readable because it never authenticated (when it trys to read the status
//returned by the smtp server, eg: "220 smtp2.example.com ESMTP Postfix")
client.send(email);
Solved a while back, forgot to post the answer.
The .NET smtp client, along with most smtp client libraries, does not support initiating communications via ssl/tls. It requires that the smtp server support initiating communications unencrypted, and then transitioning to an encrypted connection using the "upgrade" command (this is almost always handled behind the scenes by the SMTP client library).
At the time, I was connecting to a semi-custom smtp server that was proxying postfix, and this custom solution only supported connecting initially via ssl/tls. Have since started using Haraka for my SMTP server, which supports all SMTP standards as well as providing me with the plugin capability I needed.

C# SmtpClient error: not local host?

I'm sending a simple mail with attachment using SmtpClient but I get this error:
Mailbox unavailable. The server response was: not local host
example.com, not a gateway
System.Net.Mail.SmtpFailedRecipientException:
Mailbox unavailable.
The server response was: not local host example.com, not a gateway at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message)
And the code:
public static void CreateMessageWithAttachment(byte[] compressed)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"noreply#example.com",
"recepient#example.com",
"Hello.",
"How are you?");
// Create the file attachment for this e-mail message.
Stream attachStream = new MemoryStream(compressed);
Attachment attachment = new Attachment(attachStream, MediaTypeNames.Application.Octet);
message.Attachments.Add(attachment);
//Send the message.
SmtpClient client = new SmtpClient("123.12.12.123");
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
attachment.Dispose();
}
Of course the domain and IP is valid in the original code. I have tried using both "localhost" and IP but getting same error. Googling returned 3 results of which the helpful one seems to be in chinese and firewall preventing me from translating it.
Thanks in advance.
I've searched for: C# smtpclient error The server response was: not local host example.com, not a gateway and got 27K+ results.
If you are using localhost have a look at this page it says:
This is a relay error. Make sure you can relay through the SmtpMail.SmtpServer
either by your IP address, by your MailMessage.From address, or if you need to
authenticate, check out 3.8 How do I authenticate to send an email?
If SmtpMail.SmtpServer is set to "127.0.0.1" or "localhost", and you are using
the built in IIS SMTP Service, you can allow relaying for 127.0.0.1 by
1) Opening the IIS Admin MMC
2) Right-Clicking on the SMTP Virtual Server and selecting Properties
3) On the Access tab, click the Relay button
4) Grant 127.0.0.1 (or the IP address used by System.Web.Mail) to the
Computers list.
5) Close all dialogs
6) Restarting the SMTP Service

Exchange 2007 Not Allowing Mail To Be Sent From Console App

I am trying to send email using Exchange 2007 from a console app using the following code and I get this error message in the exception that gets thrown on the Send call.
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 message = new MailMessage();
message.From = new MailAddress("from#example.com");
message.To.Add("to#domain.com");
message.Subject = "test";
SmtpClient smtp = new SmtpClient(ConfigurationUtil.SMTPServer);
smtp.Credentials = new System.Net.NetworkCredential("from#example.com", "password");
smtp.Send(message);
This worked on Exchange 2003.
This ended up being an Exchange 2007 issue and had nothing to do with code.
From the error message it seems like you need to connect to Exchange via SSL.
SmtpClient smtp = new SmtpClient(ConfigurationUtil.SMTPServer, 465);
Substitute that port number for the port that your Exchange server's secure connection is listening on.

Categories