每当我启动服务时,它都没有进行任何操作,因为服务不是"发送邮件"但是当我调试它的时候就像我预期的那样发送邮件。为什么它发生了我不知道。我认为这不是code.as编码在我调试时运行良好的问题。请建议我。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MyUtility1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
OnStart(null);
}
protected override void OnStart(string[] args)
{
Timer t1;
t1 = new System.Timers.Timer();
t1.Enabled = true;
Task.Run(() =>
{
EnvVars envVars = EnvVars.GetSettings();
});
}
protected override void OnStop()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net.Mail;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Timers;
using System.Configuration;
using System.IO;
namespace MyUtility1
{
public class EnvVars
{
public static EnvVars GetSettings()
{ EnvVars envVars = new EnvVars();
String[] files = Directory.GetFiles(@"H:\\EDI", "*.brl", SearchOption.AllDirectories);
if (files.Length > 0)
{
var mail_server1 = ConfigurationManager.AppSettings.Get("mail_server");
var mail_from_address1 = ConfigurationManager.AppSettings.Get("mail_from_address");
var mail_address1 = ConfigurationManager.AppSettings.Get("mail_address");
MailMessage mm = new MailMessage(mail_from_address1, mail_address1);
mm.Body = ConfigurationManager.AppSettings.Get("body");
mm.Subject = ConfigurationManager.AppSettings.Get("subject");
mm.IsBodyHtml = true;
int leng = files.Length;
for (int i = 0; i < files.Length; i++)
{
String sep = " | ";
String Body1 = String.Join(sep, files, 0, leng);
mm.Body = Body1;
}
SmtpClient client = new SmtpClient(mail_server1);
try
{
client.Send(mm);
}
catch (Exception e) { }
}
return envVars;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.web>
<authorization>
<allow users="dkamd@gmail.com" />
</authorization>
</system.web>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="localhost"
port="25"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
<appSettings>
<add key="mail_server" value="mail.gmail.com"/>
<add key="mail_address" value="dkamd@gmail.com"/>
<add key="mail_from_address" value="dkamd@gmail.com"/>
<add key="body" value="New file found"/>
<add key="subject" value="BRL files"/>
</appSettings>
</configuration>
答案 0 :(得分:0)
您可能想要检查权限。 在什么帐户下运行服务? 它是否有权访问文件系统? (也可能是邮件客户端的问题,但不太可能)
要检查此问题, 添加try - 使用日志捕获代码。
这样的事情:
using System;
public class EnvVars
{
public static EnvVars GetSettings()
{
EnvVars envVars = new EnvVars();
try{
//TODO - log begin work
String[] files = Directory.GetFiles(@"H:\\EDI", "*.brl", SearchOption.AllDirectories);
if (files.Length > 0)
{
var mail_server1 = ConfigurationManager.AppSettings.Get("mail_server");
var mail_from_address1 = ConfigurationManager.AppSettings.Get("mail_from_address");
var mail_address1 = ConfigurationManager.AppSettings.Get("mail_address");
MailMessage mm = new MailMessage(mail_from_address1, mail_address1);
mm.Body = ConfigurationManager.AppSettings.Get("body");
mm.Subject = ConfigurationManager.AppSettings.Get("subject");
mm.IsBodyHtml = true;
int leng = files.Length;
for (int i = 0; i < files.Length; i++)
{
String sep = " | ";
String Body1 = String.Join(sep, files, 0, leng);
mm.Body = Body1;
}
SmtpClient client = new SmtpClient(mail_server1);
client.Send(mm);
}
}
catch (Exception e)
{
//TODO - log exception
}
finally
{
return envVars;
}
}
}
}
答案 1 :(得分:0)
可能在调试时,您的应用程序有足够的时间发送电子邮件。
将您的代码更改为此代码:
Task t = Task.Run(() => {
EnvVars envVars = EnvVars.GetSettings();
});
t.Wait();