如何集成自动安排的电子邮件服务与用户界面asp .net

本文关键字:邮件服务 net asp 用户界面 何集成 集成 | 更新日期: 2023-09-27 18:16:04

我创建了一个web服务,可以从Global.cs自动发送电子邮件。我使用了参考代码项目参考模拟WS

为自动安排的电子邮件服务,它是发送电子邮件每2分钟从我的gmail帐户到另一个gmail帐户。现在,我想在这次成功的基础上走得更远。我想有用户界面,用户可以选择谁发送,开始日期和结束日期在网络形式,使他们的工作。

用户可以从列表框中选择发送对象和开始/结束日期。然后,用户可以设置为每日发送邮件。我已经为用户名、用户电子邮件、开始日期和结束日期创建了一个表。我不知道如何链接输入从这个GUI到Global.cs。有什么想法吗?我还想确保服务应该在结束日期停止运行。请adivse。这是我的自动邮件服务代码。

public class Global : System.Web.HttpApplication
{   //private const string CONNECTION_STRING = "Data Source=(local);InitialCatalog=tempdb;Integrated Security=SSPI;";
// private const string LOG_FILE = @"c:'temp'Cachecallback.txt";
// private const string MSMQ_NAME = ".''private$''ASPNETService";
private const string DummyCacheItemKey = "abcdefgh";
public static ArrayList _JobQueue = new ArrayList();

public Global()
{
    InitializeComponent();
}
protected void Application_Start(object sender, EventArgs e)
{
    RegisterCacheEntry();
}
private void RegisterCacheEntry()
{
    // Prevent duplicate key addition
    if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return;
    HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null, DateTime.MaxValue,
        TimeSpan.FromMinutes(1), CacheItemPriority.NotRemovable,
        new CacheItemRemovedCallback(CacheItemRemovedCallback));
}
/// <summary>
/// Callback method which gets invoked whenever the cache entry expires.
/// We can do our "service" works here.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="reason"></param>
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    Debug.WriteLine("Cache item callback: " + DateTime.Now.ToString());
    // Do the service works
    DoWork();
    // We need to register another cache item which will expire again in one
    // minute. However, as this callback occurs without any HttpContext, we do not
    // have access to HttpContext and thus cannot access the Cache object. The
    // only way we can access HttpContext is when a request is being processed which
    // means a webpage is hit. So, we need to simulate a web page hit and then 
    // add the cache item.
    HitPage();
}
/// <summary>
/// Hits a local webpage in order to add another expiring item in cache
/// </summary>
private void HitPage()
{
    WebClient client = new WebClient();
    client.DownloadData(DummyPageUrl);
}
/// <summary>
/// Asynchronously do the 'service' works
/// </summary>
private void DoWork()
{
    Debug.WriteLine("Begin DoWork...");
    Debug.WriteLine("Running as: " + WindowsIdentity.GetCurrent().Name);
 DoSomeEmailSendStuff();
    Debug.WriteLine("End DoWork...");
}

/// <summary>
/// Test email send
/// </summary>
private void DoSomeEmailSendStuff()
{
    try
    {
        string mailbody;
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("def@gmail.com");
        mailMessage.From = new MailAddress("abc@gmail.com", "Joe Testing");

        mailMessage.Subject = "Test from ASP .NET";
        mailbody = "This is test only from ASP .NET for auto email scheduling";
        mailMessage.Body = "<html><body>" + mailbody.Replace("'n", "<br/>") + "</body></html>";
        mailMessage.IsBodyHtml = true;
        // Create the credentials to login to the gmail account associated with my custom domain
        string sendEmailsFrom = "abc@gmail.com";
        string sendEmailsFromPassword = "xxxxxxxxxx";
        NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);

        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
        mailClient.EnableSsl = true;
        mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        mailClient.UseDefaultCredentials = false;
        //mailClient.Timeout = 20000;
        mailClient.Credentials = cred;
        //mailClient.Credentials = CredentialCache.DefaultNetworkCredentials;
        mailClient.Send(mailMessage);

    }
    catch (Exception x)
    {
        Debug.WriteLine(x);
    }
}

protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
 // If the dummy page is hit, then it means we want to add another item in cache
  if (HttpContext.Current.Request.Url.ToString() == DummyPageUrl)
    {
    // Add the item in cache and when succesful, do the work.
    RegisterCacheEntry();
    }
}
protected void Application_End(object sender, EventArgs e)
{
}
#region Web Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
        {
             this.components = new System.ComponentModel.Container();
        }
#endregion

}

如何集成自动安排的电子邮件服务与用户界面asp .net

要做的第一件事是在Global.cs类文件中添加一些更多的字段。它将处理用户将使用的值。

private string Recipient;
private DateTime StartTime;
private DateTime EndTime;

…如果你想添加更多的字段,你可以添加更多的字段,等等。

然后您可以使用表单来填充POST请求中的这些字段,如下所示

if(IsPost) {
   // get the form items and add details to the Global.cs
}

一旦您完成了添加细节,执行您已经拥有的函数并从Global.cs中添加细节。