在 c# windows 服务中使用模板发送电子邮件
send emails with templates in c# windows service
我是 windows 服务的新手。
我遇到了一些问题:-
- 每次重装都要重启系统
- 我可以在 windows 服务中调试我的代码吗?
- 有没有什么方法可以使用命令提示符安装它(我用过
VS2015 的开发人员命令提示符)。
我创建了一个发送简单电子邮件的服务,现在我更改了代码以发送带有模板的电子邮件,但它不起作用。
错误:- "Service Error on: 10/04/2017 07:40:00 PM Could not find a part of the path 'C:\Windows\system32\~\Payment-Receipt.htm'"。
File.ReadAllText 不是 Server.MapPath 的替代品,所以应该使用什么?
private void SchedularCallback(object e)
{
try
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("getAllInvoices"))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
foreach (DataRow row in dt.Rows)
{
string name = row["FirstName"].ToString();
string email = row["EmailId"].ToString();
long InvoiceId = Convert.ToInt64(row["InvoiceId"].ToString());
string amount = row["SettledAmount"].ToString();
WriteToFile("Trying to send email to: " + name + " " + email);
string body = this.createEmailBody(name, "Please check your account Information", amount);
using (MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["UserName"], email))
{
mm.Subject = "Package Changed";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = ConfigurationManager.AppSettings["UserName"];
credentials.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mm);
}
}
this.ScheduleService();
}
catch (Exception ex)
{
WriteToFile("Service Error on: {0} " + ex.Message + ex.StackTrace);
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
private string createEmailBody(string userName, string title, string amount)
{
string body = string.Empty;
string originalTemplate = File.ReadAllText(@"~\Payment-Receipt.htm");
using (StreamReader reader = new StreamReader(originalTemplate))
{
body = reader.ReadToEnd();
}
body = body.Replace("{FirstName}", userName); //replacing the required things
body = body.Replace("{Amount}", amount);
return body;
}
private void WriteToFile(string text)
{
string path = "C:\ServiceLog.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
writer.Close();
}
}
我相信可以使用 Visual Studio 2015 调试服务。如果您 运行 在远程服务器上,则需要在端点上安装远程调试工具。据我所知,您需要 运行 将服务作为自己的进程(它不能托管在 svchost 中),但我通常 运行 在尝试部署之前将服务单独放在自己的进程中生产系统。只要您不运行在生产系统上使用此服务,运行使用远程调试工具应该没问题。如果该进程是本地的,那么您应该能够附加到该进程。
您收到的错误是因为您正在尝试读取相对路径。我会将路径粘贴在我的配置文件中,然后阅读它。如果您将 Environment.CurrentDirectory
属性 打印到调试文件,您会看到您的服务 运行 正在 Windows 系统目录中。
我是 windows 服务的新手。
我遇到了一些问题:-
- 每次重装都要重启系统
- 我可以在 windows 服务中调试我的代码吗?
- 有没有什么方法可以使用命令提示符安装它(我用过 VS2015 的开发人员命令提示符)。
我创建了一个发送简单电子邮件的服务,现在我更改了代码以发送带有模板的电子邮件,但它不起作用。
错误:- "Service Error on: 10/04/2017 07:40:00 PM Could not find a part of the path 'C:\Windows\system32\~\Payment-Receipt.htm'"。
File.ReadAllText 不是 Server.MapPath 的替代品,所以应该使用什么?
private void SchedularCallback(object e)
{
try
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("getAllInvoices"))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
foreach (DataRow row in dt.Rows)
{
string name = row["FirstName"].ToString();
string email = row["EmailId"].ToString();
long InvoiceId = Convert.ToInt64(row["InvoiceId"].ToString());
string amount = row["SettledAmount"].ToString();
WriteToFile("Trying to send email to: " + name + " " + email);
string body = this.createEmailBody(name, "Please check your account Information", amount);
using (MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["UserName"], email))
{
mm.Subject = "Package Changed";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = ConfigurationManager.AppSettings["UserName"];
credentials.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mm);
}
}
this.ScheduleService();
}
catch (Exception ex)
{
WriteToFile("Service Error on: {0} " + ex.Message + ex.StackTrace);
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
private string createEmailBody(string userName, string title, string amount)
{
string body = string.Empty;
string originalTemplate = File.ReadAllText(@"~\Payment-Receipt.htm");
using (StreamReader reader = new StreamReader(originalTemplate))
{
body = reader.ReadToEnd();
}
body = body.Replace("{FirstName}", userName); //replacing the required things
body = body.Replace("{Amount}", amount);
return body;
}
private void WriteToFile(string text)
{
string path = "C:\ServiceLog.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
writer.Close();
}
}
我相信可以使用 Visual Studio 2015 调试服务。如果您 运行 在远程服务器上,则需要在端点上安装远程调试工具。据我所知,您需要 运行 将服务作为自己的进程(它不能托管在 svchost 中),但我通常 运行 在尝试部署之前将服务单独放在自己的进程中生产系统。只要您不运行在生产系统上使用此服务,运行使用远程调试工具应该没问题。如果该进程是本地的,那么您应该能够附加到该进程。
您收到的错误是因为您正在尝试读取相对路径。我会将路径粘贴在我的配置文件中,然后阅读它。如果您将 Environment.CurrentDirectory
属性 打印到调试文件,您会看到您的服务 运行 正在 Windows 系统目录中。