I am writing a Windows service using C # .NET 4.0 (my development workstation is Windows 7). The goal is to send an email from the service directly in case of errors during processing. I included a code snippet to show what I'm trying to achieve:
try
{
string emailAddresses = "me@sample.com";
string emailCCAddresses = "you@sample.com";
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." ;
using (Attachment data = new Attachment("C:\\users\\sample.xml", MediaTypeNames.Application.Octet))
{
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);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
The problem I encountered is an email option that does not work from the win service on any Windows 7 computer. However, the same code will work fine on a Win XP machine. Caveat, this cannot be verified as a console application. It must be running as a service in order to see the behavior. I put the same code in a console application and it worked perfectly on my machine with win 7.
, telnet .
, ( ) -
, , , Windows . , / , . - , , , , ( , / , , , )
, , :
SMTP: System.Net.Mail.SmtpException: . --- > System.Net.WebException: --- > System.Net.Sockets.SocketException: , XXX.XXX.XXX.XXX XX System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket & socket, IPAddress & address, ConnectSocketState, IAsyncResult asyncResult, Int32, )
!
, / Windows 7 , , , .