C# C asp.net windows服务中存在多个错误
我创建了以下C windows服务,根据订阅类型每天和/或每周向我的asp.net 3.5 C网站的所有订阅用户发送电子邮件C# C asp.net windows服务中存在多个错误,c#,windows-services,C#,Windows Services,我创建了以下C windows服务,根据订阅类型每天和/或每周向我的asp.net 3.5 C网站的所有订阅用户发送电子邮件 using System; using System.Security; using System.Web; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using
using System;
using System.Security;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;
using System.Timers;
using System.Runtime.Remoting.Messaging;
namespace AutoEmailService
{
public partial class AutoEmailService : ServiceBase
{
private System.ComponentModel.IContainer components = null;
private System.Timers.Timer Timer;
public AutoEmailService()
{
InitializeComponent(); <-- this one
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new AutoEmailService()
};
ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
this.Timer = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.Timer)).BeginInit();
this.Timer.Interval = 60000;
this.Timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Elapsed);
this.ServiceName = "AutomaticEmailService";
((System.ComponentModel.ISupportInitialize)(this.Timer)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose(); <-- this
}
}
base.Dispose(disposing);
}
protected override void OnStart(string[] args)
{
this.Timer.Enabled = true;
LogMessage.LogMessages("Service Started"); <-- this
}
protected override void OnStop()
{
Timer.Enabled = false;
LogMessage.LogMessages("Service Stopped"); <-- this
}
// Respond to the Elapsed event of the timer control
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//add a log entry to windows log to check windows service current position/activity
LogMessage.LogMessages("Service Running"); <-- this
if (DateTime.Now.ToString("hh tt") == "05 PM")
{
List DailySubscription = new List();
DataSet dataset = new DataSet();
DataSet ArticlesDataSet = new DataSet();
int subscriptionType = 0;
string strArticleHeading = null;
DateTime currentDate = DateTime.Now.Date;
//Get all the subscribed users
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT SubType, Email FROM ArticleSubscription WHERE SubType > 0", Conn);
sqlDataAdapter.Fill(dataset);
//Seprate users on the bases of their subscription type
foreach (DataRow dr in dataset.Tables[0].Rows)
{
subscriptionType = Convert.ToInt32(dr["SubType"].ToString());
if (subscriptionType == 1 || subscriptionType == 3)
{
DailySubscription.Add(dr["Email"].ToString());
}
}
// get all today's articles
SqlDataAdapter sqlArticlesDataAdapter = new SqlDataAdapter();
SqlCommand Command = new SqlCommand("SELECT articleheading FROM Articles WHERE postedDate %LIKE% @currentDate", Conn);
Command.Parameters.Add("@currentDate", SqlDbType.NVarChar);
Command.Parameters["@currentDate"].Value = DateTime.Now.Date;
sqlArticlesDataAdapter.SelectCommand = Command;
sqlArticlesDataAdapter.Fill(dealsDataSet);
//copy all the articles to a string
foreach (DataRow dr in dealsDataSet.Tables[0].Rows)
{
strArticleHeading = strArticleHeading + dr["articleheading"].ToString();
}
//send emails to all subscribers
foreach (string email in DailySubscription)
{
MailMessage mail = new MailMessage(); mail.From = new MailAddress("123@mywebsite.com", "My Web Site");
mail.To.Add(new MailAddress(email.ToString()));
mail.Subject = "Today's Articles";
mail.IsBodyHtml = true;
mail.Body = strArticleHeading;
System.Net.Mail.SmtpClient SmtpMail = "IP Address";
SmtpMail.Send(mail); <-- this
}
}
if (DateTime.Now.ToString("hh tt") == "07 PM" && DateTime.Now.DayOfWeek.ToString("dddd") == "Friday")
{
List WeeklySubscription = new List();
DataSet dataset = new DataSet();
int subscriptionType = 0;
DataSet articlesDataSet = new DataSet();
string strArticleHeading = null;
//Get all the subscribed users
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT SubType, Email FROM ArticleSubscription WHERE SubType > 0", Conn);
sqlDataAdapter.Fill(dataset);
//Seprate users on the bases of their subscription type
foreach (DataRow dr in dataset.Tables[0].Rows)
{
subscriptionType = Convert.ToInt32(dr["SubType"].ToString());
if (subscriptionType == 2 || subscriptionType == 3)
{
WeeklySubscription.Add(dr["Email"].ToString());
}
}
// get all articles of this week
SqlDataAdapter sqlArticlesDataAdapter = new SqlDataAdapter();
SqlCommand Command = new SqlCommand("SELECT articleheading FROM Articles WHERE postedDate >= @currentDate", Conn);
Command.Parameters.Add("@currentDate", SqlDbType.NVarChar);
Command.Parameters["@currentDate"].Value = DateTime.Now.Date.AddDays(-7);
sqlArticlesDataAdapter.SelectCommand = Command;
sqlArticlesDataAdapter.Fill(articlesDataSet);
//copy all the articles to a string
foreach (DataRow dr in dealsDataSet.Tables[0].Rows)
{
strArticleHeading = strArticleHeading + dr["articleheading"].ToString();
}
//send emails to all subscribers
foreach (string email in WeeklySubscription)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("123@mywebsite.com", "My Web Site");
mail.To.Add(new MailAddress(email.ToString()));
mail.Subject = "This week's Articles";
mail.IsBodyHtml = true;
mail.Body = strArticleHeading;
System.Net.Mail.SmtpClient SmtpMail = "IP Address";
SmtpMail.Send(mail); <-- this
}
}
}
}
}
我已经用标记了行,请尝试格式化代码。除此之外:
System.Net.Mail.SmtpClient SmtpMail = "IP Address";
您正在尝试将字符串分配给SmtpClient类型的变量。以下是您的答案
既然您声明了分部类,我猜Visual Studio编辑器已经为您声明了一个.Designer.cs文件,其中包含组件、Dispose和InitializeComponents。您需要自己找出添加它们的原因,并决定是保留您添加的,还是使用设计师为您添加的。这将处理不明确的代码。
SmtpMail类位于System.Web.Mail命名空间中,因为在代码顶部的using指令中没有该类,所以编译器无法找到它。添加正确的using语句。
LogMessage我不知道它是什么,我猜它是项目中的一个类,看看是否需要为它添加一个using指令。
专用服务器的smtp发送设置,唯一可以告诉您是配置服务器的人。可能有一个SMTP服务在本地运行,在这种情况下,您可能希望将ip地址设置为localhost,但您可能需要使用它进行身份验证。
另外,当您询问有关堆栈溢出和其他地方的问题时,这里有一个一般提示。尽量说得具体些。转储整个代码文件,列出4+个问题,然后用“如果有人发现此windows服务中的任何其他逻辑和编程错误/错误”来结束您的问题,这几乎可以保证很少有人会看到它。此外,还要学习如何正确设置代码和列表的格式,确保问题质量的时间越少,人们就越少花时间帮助您解决问题。此错误代码:
//send emails to all subscribers
foreach (string email in DailySubscription)
{
MailMessage mail = new MailMessage(); mail.From = new MailAddress("123@mywebsite.com", "My Web Site");
mail.To.Add(new MailAddress(email.ToString()));
mail.Subject = "Today's Articles";
mail.IsBodyHtml = true;
mail.Body = strArticleHeading;
System.Net.Mail.SmtpClient SmtpMail = "IP Address";
SmtpMail.Send(mail); <-- this
}
在这里,我建议使用SendAsync方法,因为您使用的是同一个线程,操作将等待电子邮件发送,如果您循环并且电子邮件尚未发送,您将收到错误消息-
检查一下,让我知道,我也有同样的问题
希望这有帮助 请求帮助的好方法。祝你好运。编辑器中有一个格式化代码选项。如果没有格式化,你很难阅读代码。你能简化你的代码吗?删除不必要的细节并格式化它吗?下次,使用这个网站上的预览部分和问题文本区域上方的工具栏来正确格式化你的问题,否则你最好用希腊语发布问题,你让人们看到它的机会差不多。你没有调试问题。你有一个“我不理解这些编译器错误”的问题。调试之后进行。关于2的思考:我一直认为System.Net.Mail取代了System.Web.Mail命名空间。我不记得我在哪里读到的,我想两者都有用。。在msdn上,System.Net.Mail似乎较新,并且有新的名称空间气味:-是的,可能是,我也不使用,我只是没有真正的动机去寻找通过C发送电子邮件的正确方式。我使用了这个SendAsync方法并在我的本地机器上安装了这个windows服务,但它没有发送任何电子邮件。