我可以很容易地在php/mysql中做到这一点,但是,c# mvc剃须刀是一个完全不同的故事。
我所要做的就是在服务器上设置一个cron作业(我假设它不是c# mvc的cron作业,甚至不知道什么叫cron作业),这样就可以根据月发送电子邮件: 1mth、2mth等等。
下面是我的sendEmail,它不能工作,因为我需要在代码中包含用户名/密码才能工作,在我看来,这是荒谬的( "php“中的sendemail不需要发送者证书),除非有更好的方法,请告诉我方法。
//send email
public void sendEmail()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("toemail@domain.com");
message.To.Add(new MailAddress("toemail@domain.com"));
message.Subject = "This is my subject";
message.Body = "testing testing testing";
SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = false;
client.Send(message);
}
//date difference
public decimal monthDifference(DateTime d1, DateTime d2)
{
if (d1 > d2)
{
DateTime hold = d1;
d1 = d2;
d2 = hold;
}
decimal monthsApart = Math.Abs((12 * (d1.Year - d2.Year)) + d2.Month - d1.Month - 1);
decimal daysinStartingMonth = DateTime.DaysInMonth(d1.Year, d1.Month);
monthsApart = monthsApart + (1 - ((d1.Day - 1) / daysinStartingMonth));
decimal daysinEndingMonth = DateTime.DaysInMonth(d2.Year, d2.Month);
monthsApart = monthsApart + (d2.Day / daysinEndingMonth);
return monthsApart;
}
//i will use linq to query the database and get the start and end date
if(monthDifference(start, end) == 2 || monthDifference(start, end) == 1)
{
sendEmail();
}发布于 2013-12-16 16:41:04
若要按计划发送电子邮件,应创建控制台应用程序,并在需要时使用Windows任务计划程序运行应用程序。如果不能使用Windows,可以使用像Quartz.net这样的库
可以使用web.config / app.config文件:http://msdn.microsoft.com/en-us/library/w355a94k(v=vs.110).aspx配置smtp服务器。
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>发布于 2013-12-16 16:39:27
您使用的是SmtpClient对象,它使用的是--您猜是-- SMTP。
在PHP/MySQL中,您经常使用sendmail,它可以在不使用SMTP服务器的情况下从服务器发送电子邮件。
Windows不包含此功能,您必须始终使用某种邮件中继提供商向世界发送电子邮件。
如果不想存储用户名/密码,那么配置SMTP服务器,使其允许来自某些IP或主机名的匿名连接。
也可以将SMTP登录凭据存储在app.config中。
https://stackoverflow.com/questions/20615813
复制相似问题