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

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.

Related

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

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)

SMTP Connection over SSL TLS v1 giving timeout error

I have to connect to the SMTP server of one our clients and that is over SSL using TLS v1
For this I have created a simple console POC using below code:
try
{
MailMessage mail = new MailMessage();
System.Net.Mail.SmtpClient SmtpServer = new System.Net.Mail.SmtpClient("smtpout.xyz.com");
Console.WriteLine("sending mail...");
mail.From = new System.Net.Mail.MailAddress("test#domain.com");
mail.To.Add("test2#domain.com");
mail.Subject = "TEST";
mail.Body = "TEST DATA";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "");
SmtpServer.EnableSsl = True;
SmtpServer.UseDefaultCredentials = False;
SmtpServer.Send(mail);
Console.WriteLine("mail sent successfully");
}
catch(Exception ex)
{
throw ex;
}
But this is giving a very generic error that operation time out. When I am trying with a non-ssl smtp server over port 25 and no network credentials the mail is triggering immediately.
Please note, The client's SMTP server is reachable through a network load balancer not directly. I assumed that this issue can be due to improper handshaking between my machine and SMTP server so I also placed the certificate of SMTP server in my machine's trusted root certificate store but still problem exists.
What extra steps I have to take in configurations in this case of TLS 1.1/1.2 protocols. I have also tried a third party library for using STARTTLS command, but that also gives error which is "A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond".
Please suggest help.

ASP.NET MVC web application unable to send email via SMTP on IIS server hosted by Arvixe

I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
I am developing a web-based sales-tracking application in Visual Studio 2015 (ASP.NET MVC). The site will be hosted on an Arvixe BusinessClass for Windows shared server. The domain of the server is mydomain.com, however this domain is actually hosted by one.com, which also provides mydomain.com email.
One of the functions of the site is to inform the line manager when a user reports a sale. The line manager is to be informed via email. The email account I am trying to send from is provided by one.com.
Using System.Net.Mail.MailMessage and sending via SmtpClient, trhe following code works in my development environment (Windows 10 pro) but not deployed (deployed on Arvixe BusinessClass for Windows shared server environment):
MailMessage message = new MailMessage();
message.To.Add("recipient#mydomain.com");
message.From = new MailAddress("sender#mydomain.com", "sender#mydomain.com");
message.IsBodyHtml = true;
message.Subject = "Subject";
message.Body = "Body<br />";
SmtpClient client = new SmtpClient();
client.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "send.one.com";
client.Port = 587;
client.UseSSL = true;
client.Credentials = new NetworkCredential("sender#mydomain.com", "***");
client.Send(message);
On my Arvixe server I get the error:
"Unable to read data from the transport connection: net_io_connectionclosed".
Using this code I have also tried ports 465 and 25, with SSL set to true and false, and also host "mailout.one.com", without success.
I gave up and tried using MailKit with the following code:
MimeMessage message = new MimeMessage();
message.To.Add(new MailboxAddress("recipient#mydomain.com"));
message.From.Add(new MailboxAddress("sender#mydomain.com", "sender#mydomain.com"));
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "htmlbody<br />";
bodyBuilder.TextBody = "textbody";
message.Body = bodyBuilder.ToMessageBody();
message.Subject = "Subject";
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("send.one.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
client.Authenticate("sender#mydomain.com", "***");
client.Send(message);
client.Disconnect(true);
}
Again it works in my development environment, but not in production. Here I get the error:
"The operation is not allowed on non-connected sockets".
Of note, both solutions work in deployed environment if I change the host to 127.0.0.1 and add an example#mydomain.com email account and connected user account in Arvixe, but only for external email addresses. Addresses ending in mydomain.com obviously get rerouted back to the server itself, as the server's domain is mydomain.com, but as the email provider is one.com and not the Arvixe server, the email doesn't get through.
Other things I have tried without success:
-Setting the SPF record for mydomain.com to include my server address, and my server IP address.
-Using an Office365 account on a different domain to send from (host "smtp.office365.com")
I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
Arvixe/One.com suggestions (from support tickets and live chat):
SMTP Authentication must be used.
The local mail server, or localhost must be used to send email via web script. (try xxx.win.arvixe.com, localhost, or 127.0.0.1 - port 25 or 26, or xxxsecuremail.win.arvixe.com - port 465 for secure mail)
The FROM email address, and SMTP Authentication email address, must be a local user on the local server.
If you have your own remote email server and intend to send emails via web script to email addresses that are local to your domain, then that mail domain needs to be removed from the local server (we have to do this) and you will have to use an account from another domain local to the server to send mail from. Otherwise any email going to the local domain will be delivered locally and not to your remote server.

SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required? [duplicate]

This question already has answers here:
mail sending with network credential as true in windows form not working
(2 answers)
Closed 7 years ago.
I'm working in windows application and C#, I am using following code to send email. The Code not works correctly in my system:
MailMessage mailmsg = new MailMessage();
SmtpClient smtpclient = new SmtpClient();
mailmsg.To.Add(txtTo.Text);
mailmsg.CC.Add(txtCC.Text);
mailmsg.Subject = txtSubj.Text;
mailmsg.From = new MailAddress("buvana#gmail.com");
mailmsg.Body = txtbody.Text;
smtpclient.Port = 587;
smtpclient.Host = "smtp.gmail.com";
smtpclient.EnableSsl = true;
smtpclient.UseDefaultCredentials = false;
smtpclient.Credentials = new NetworkCredential("buvana#gmail.com", "*********");
smtpclient.Send(mailmsg);
How to solve this problem.
Received this error:
SMTP server requires a secure connection or the client was not authenticated
The server response was: 5.5.1 Authentication Required
Your code seems to be fine, useDefaultCredentials is false, port is 587, etc.
I reckon the problem is that you need to configure Gmail to allow Less Secure Applications, following these instructions: https://support.google.com/accounts/answer/6010255?hl=en
Go to the "Less secure apps" section in My Account.
Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)
If you are using 2 factor authentication, you'll need to create a new app password for your application and use that to log in.

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