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.
Related
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.
I am tyring to send an email form an hotmail account using c#.
This is the code:
var smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("userName#hotmail.com" , "password");
smtpClient.Host = "smtp.live.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.SendCompleted += SmtpClientSendCompleted;
var mailMessage = new MailMessage("userName#hotmail.com", "me#gmail.com", "The subject", "The body");
try
{
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("An error occur while sending the mail: {0}", ex.Message));
return;
}
MessageBox.Show("Message was sent succesfully\n");
}
This code works perfectly on Win8 and Win7 but fails on win8.1 and throw the following Exception:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
Also, if I cahnge the mail provider to gmail.com it also works perfectly.
I tries to send a mail from hotmail account on win8.1 using ThunderBird and it worked.
What is the problem with my code?
This is presumably a problem with Windows 8.1 (preview). Several users have already reported this issue on the MSDN forum and TechNet forum.
If you're developing an ASP.NET application, this is probably not a problem, since you'll be running your production build on environment that are yet to be on 8.1.
An alternative solution is to switch to a different SMTP service, some users has report that Gmail's SMTP is working fine with 8.1 preview.
I have a Contact Us page in my website from which I am trying to send mail.Here is my code
MailMessage feedBack = new MailMessage();
feedBack.To.Add("some#some.com");
feedBack.From = new MailAddress("MyClient#some.com");
feedBack.Subject = "Mail from My Client's Website.";
feedBack.Body = "Sender Name: " + Name.Text + "<br/><br/>Sender Last Name:"+LastName.Text+"<br/><br/>Sender Company:"+Company.Text+"<br/><br/>Sender Designation:"+Designation.Text+"<br/><br/>Sender Email:"+Email.Text+"<br/><br/>Sender Phone No:"+ PhoneNo.Text+"<br/><br/>Sender Enquiry:"+Enquiry.Text;
feedBack.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.MyClient.com"; //Or Your SMTP Server Address
//smtp.Port = 25;
//smtp.EnableSsl =false;
smtp.Credentials = new System.Net.NetworkCredential("MyClient#some.com","XXXX");
//Or your Smtp Email ID and Password
smtp.Send(feedBack);
All the time I keep getting this error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.37.118.141:25
I have verified with my client and all the details are correct(smtp server name,credentials).
I also tried setting port to 587 and 465 but it did not work.
Can someone help me out with this?
What can be the cause?
I am not able to find it.
Any suggestions are welcome.
I tested the ip above with port 25 through telnet and got the following response:
Command:
telnet 64.37.118.141 25
Response:
Could not open connection to the host, on port 25: Connect failed
Most likely port 25 is being blocked. You will need to find what port is used with your pop/smtp servers.
MEDIUM TRUST
If you are on a shared hosting service, chances are that ASP.Net is set to run in medium-trust (as it should). This restricts SMTP to port 25 only, you cannot use any other port.
SMTP problems when ASP.NET is not running in full-trust
SO similar post
Have you checked that the server that the web application resides on is set up to be allowed to send mail through the SMTP server? Trust connections sometimes have to be setup to allow relaying off one server throught another.
I set up my hMailServer on my windows 2008 machine, and I'm trying to send emails.
When I do it with C#
MailMessage message = new MailMessage();
message.From = new MailAddress(from, "John");
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient("mail.example.com");
client.Credentials = new System.Net.NetworkCredential("john#example.com", "password");
client.Send(message);
But when I try to send emails with a windows live email client, it gives me an error
The connection to the server has failed
All the settings are exactly the same. I tried several email clients, but it doesn't work. It never happened to me before. I just moved from one machine to another, and got this problem.
I can receive mail in the client though...
Try to telnet to port 25, can it connect?
Open up command prompt:
telnet mail.example.com 25
If it cannot connect (which is what I expect) then you have a problem that is not code related but firewall related. (Or perhaps you're trying to connect to the wrong port if they're running SMTP on a non standard port)
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.