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.
Related
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.
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 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 am writing a Windows service using c# .NET 4.0 (my development workstation is Windows 7).
The aim is to send an email from the service directly in case of errors during processing.
I've included a code snippet to show what i am trying to achieve:
try
{
string emailAddresses = "me#sample.com";
string emailCCAddresses = "you#sample.com";
//Send the email
using (MailMessage mailMsg = new MailMessage())
{
mailMsg.To.Add(emailAddresses);
mailMsg.From = new MailAddress("winService#sample.com");
mailMsg.CC.Add(emailCCAddresses);
mailMsg.Subject = " Error Message ";
mailMsg.Body = DateTime.Now + " : Invalid File Encountered." ;
////Creating the file attachment
using (Attachment data = new Attachment("C:\\users\\sample.xml", MediaTypeNames.Application.Octet))
{
//Add timestamp info for the file
ContentDisposition disposition = data.ContentDisposition;
disposition.ModificationDate = File.GetLastWriteTime("C:\\users\\sample.xml");
mailMsg.Attachments.Add(data);
SmtpClient emailClient = new SmtpClient("example.Sample.com", 25);
emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
emailClient.UseDefaultCredentials = true;
emailClient.Send(mailMsg);
}
}//end using
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
The issue i am facing is - the email option does not work from a win service on any windows 7 machine. However the exact same code will work on a Win XP machine perfectly fine. Caveat, this cannot be tested as a console app. It needs to be run as a service to see the behavior. I put the same code in a console application and it worked fine on my win 7 machine.
Additionally, i enabled the telnet client and server windows features and was able to send email via the command prompt successfully.
I tried a few things to try and isolate the cause (none worked) -
Setting the NetworkCredential explicitly and setting UseDefaultCredentials = false.
emailClient.Credentials = new System.Net.NetworkCredential("username","pwd","Domain1");
changing the Log On As option of the service to be my userid and pwd
The From and To email addresses are authentic, not dummy addresses(in my real code that is!)
The only differences i can find is that the windows service is installed under the local system account. But changing the log on as should have caused it to use my authentication details/the credentials specified in the code. The IT guy in my office said the only difference he saw was that my message was sent as a relay(not sure what this means as the from/to and log on as accounts when updated to my login did not change the outcome)
the exception i see being caught is :
SMTP Exception thrown : System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions XXX.XXX.XXX.XXX XX
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
Any ideas are appreciated!
I feel it has to do with a windows 7 security policy/feature and hopefully might be as simple as changing a default setting.
You can run service as current user. Just select option USER in service installer and it will work as current user. I got the same error today and I found this solution. I hope it will be helpful for anybody.
I found it was the security policy on our new work laptops and the IT department refused to let us change it. Our test server doesn't have the same issue , of ourse it's not windows 7 either so I can't be 100% sure. Try sending email via command line to see if smtp is allowed from your pc.
The same code worked fine whn run on a win xp or windows server machine which didn't hav any restrictions
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.