简体   繁体   English

ASP.net mvc5 每 7 天发送一次电子邮件?

[英]ASP.net mvc5 sending email every 7 day?

I'm working on a small project of mine and I'm not sure if what I am trying to do is possible on a web based solution.我正在做我的一个小项目,我不确定我正在尝试做的事情是否可以在基于 Web 的解决方案上实现。

And right now it's setup like this, a user post something and that date he posted in the database.现在它的设置是这样的,用户发布了一些内容以及他在数据库中发布的日期。 What I want to do is if the user doesn't post another thing within 7 days I want to send them an email saying they are "late" or something similar to that.我想要做的是,如果用户在 7 天内没有发布另一件事,我想向他们发送一封电子邮件,说他们“迟到”或类似的事情。

I know how to send a email in asp.net as my user can request a new password / they need to verify their email.我知道如何在 asp.net 中发送电子邮件,因为我的用户可以请求新密码/他们需要验证他们的电子邮件。 I just don't know how to set it up like I want above ( IF that even is possible )我只是不知道如何像我上面想要的那样设置它(如果有可能的话)

You need a separate process that checks for, and then sends, the emails.您需要一个单独的进程来检查并发送电子邮件。 You won't be able to do it in the web application itself, but some kind of service would do it.您将无法在 Web 应用程序本身中执行此操作,但某种服务可以执行此操作。

This service would just need to periodically check your database for users and the last date they posted.此服务只需要定期检查您的数据库中的用户及其最后发布日期。 If it's more than 7 days, send that email off.如果超过 7 天,请发送该电子邮件。

Remember to record that the email has been sent and check this when determining what emails to send, otherwise you'll get an email sent every time the service checks the database, which might frustrate the user a bit!记住要记录邮件已发送,并在确定要发送的邮件时进行检查,否则每次服务检查数据库时都会收到一封电子邮件,这可能会让用户感到有些沮丧!

sending email in asp is simple.在asp中发送电子邮件很简单。 its a code i did before that check if you want to send a file too:这是我之前做的一个代码,检查你是否也想发送一个文件:

for working with time you have to work with timeSpan and dateTime为了处理时间,您必须使用timeSpandateTime

its from example in stackoverflow: Assuming StartDate and EndDate are of type DateTime:它来自stackoverflow中的示例:假设StartDate和EndDate是DateTime类型:

(EndDate - StartDate).TotalDays

you have to check if this value is bigger than 7你必须检查这个值是否大于 7

so code for sending mail is like below but you have to optimize it for your own:所以发送邮件的代码如下所示,但您必须为自己优化:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
MailMessage mail = new MailMessage();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{

try
{
lblmessage.Text ="";
mail.To.Add(txtto.Text.Trim());
mail.From = new MailAddress(txtfrom.Text.Trim());
mail.Subject = txtsubject.Text.Trim();
mail.Body = txtbody.Text.Trim();
mail.IsBodyHtml = true;
if (uploader.HasFile)
{
string filename = uploader.PostedFile.FileName;
string filepath=Server.MapPath("uploads//"+filename);
uploader.SaveAs(filepath);
Attachment attach = new Attachment(filepath);
attach.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
mail.Attachments.Add(attach);
}

SmtpClient client = new SmtpClient();

client.Host = "mail.youdomain.com";
client.Credentials = new System.Net.NetworkCredential("email username=info@yourdomain.com", "email passowrd");
client.Send(mail);
lblmessage.Text = "sent with success";
}
catch (Exception ex)
{
lblmessage.Text = ex.Message;
}
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM