gpt4 book ai didi

c# - 每当我启动服务时,邮件都不会发送,但当我调试时,它会按我预期的那样发送邮件

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:35 24 4
gpt4 key购买 nike

每当我启动服务时,它都没有执行任何操作,因为服务不是“发送邮件”,但是当我调试它时,它会按我预期的那样发送邮件。我不知道为什么会这样。我想这不是代码的问题。因为我在调试时编码运行良好。请给我建议。

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();

关于c# - 每当我启动服务时,邮件都不会发送,但当我调试时,它会按我预期的那样发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com