每当我启动服务时,邮件不会发送,但当我进行调试时,它会像我预期的那样发送邮件
本文关键字:服务 启动 调试 | 更新日期: 2023-09-27 18:27:41
每当我启动服务时,它都不会执行任何操作,因为服务不是"发送邮件",但当我进行调试时,它会像我预期的那样发送邮件。我不知道为什么会这样。我认为这不是代码的问题。当我调试时,代码运行良好。请推荐我。
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>
可能在调试时,您的应用程序有足够的时间发送电子邮件。
将您的代码更改为以下代码:
Task t = Task.Run(() => {
EnvVars envVars = EnvVars.GetSettings();
});
t.Wait();
您可能需要检查权限。该服务在哪个帐户下运行?它是否有访问文件系统的权限?(也可能是邮件客户端的问题,尽管可能性较小)
为了检查这个问题,将try-catch with log添加到代码中。
类似这样的东西:
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;
}
}
}
}