无法从Win7的Windows服务发送SMTP电子邮件 [英] Cannot send SMTP email from windows service on Win7
问题描述
目的是在处理过程中发送错误时直接发送电子邮件。
我已经添加了一个代码片段来显示我想要实现的内容:
try
{
string emailAddresses =me@sample.com;
string emailCCAddresses =you@sample.com;
//发送电子邮件
使用(MailMessage mailMsg = new MailMessage())
{
mailMsg.To.Add(emailAddresses);
mailMsg.From = new MailAddress(winService@sample.com);
mailMsg.CC.Add(emailCCAddresses);
mailMsg.Subject =错误消息;
mailMsg.Body = DateTime.Now +:无效文件遇到。 ;
////使用(Attachment data = new Attachment(C:\\\usus\\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(异常e)
{
Console.WriteLine(e.ToString() );
}
我所面临的问题是 - 电子邮件选项无法从赢服务于任何Windows 7机器。然而,完全相同的代码将在Win XP机器上正常工作。警告,这不能作为控制台应用程序测试。它需要作为服务运行才能看到行为。我将相同的代码放在控制台应用程序中,并且在我的win 7机器上运行正常。
此外,我启用了telnet客户端和服务器窗口功能,并且能够通过命令提示符成功发送电子邮件。
我尝试了一些事情来尝试隔离原因(没有工作) -
-
明确设置NetworkCredential并设置UseDefaultCredentials = false。
emailClient.Credentials = new System.Net.NetworkCredential(username,pwd,Domain1); -
更改登录作为服务的选项是我的用户标识和pwd
- From和To电子邮件地址是真实的,而不是虚拟地址(在我的真实代码中!)
我可以找到唯一的区别是Windows服务安装在本地系统帐户下。但是,将登录更改为应该会导致它使用我的验证详细信息/代码中指定的凭据。我办公室的IT人员说,他看到的唯一的区别是我的消息是作为一个中继发送的(不知道这是什么意思,从/到并登录为帐户,当更新到我的登录没有改变结果) p>
我看到被捕获的异常是:
抛出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, IP地址和地址,ConnectSocketState状态,IAsyncResult asyncResult,Int32超时,异常和异常)
任何想法都赞赏!
我觉得它与Windows 7安全策略/功能有关,希望可能像更改默认设置一样简单。
您可以作为当前用户运行服务。只需选择服务安装程序中的选项USER,它将作为当前用户工作。我今天遇到了同样的错误,我发现了这个解决方案。我希望对任何人都有帮助。
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.
这篇关于无法从Win7的Windows服务发送SMTP电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!