Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# csharp计划电子邮件razor mvc4_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# csharp计划电子邮件razor mvc4

C# csharp计划电子邮件razor mvc4,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我可以在php/mysql中轻松做到这一点,然而,c#mvc razor是一个完全不同的故事 我想做的就是在服务器上设置一个cron作业(我假设它不是c#mvc的cron作业,甚至不知道它叫什么),这样就可以根据月份发送电子邮件:1个月、2个月等等 下面是我的sendEmail,它不工作,因为我需要在代码中包含用户名/密码,这样它才能工作,这在我看来是荒谬的(“php”中的sendEmail不需要发件人凭据),除非有更好的方法,请告诉我方法 //send email public void se

我可以在php/mysql中轻松做到这一点,然而,c#mvc razor是一个完全不同的故事

我想做的就是在服务器上设置一个cron作业(我假设它不是c#mvc的cron作业,甚至不知道它叫什么),这样就可以根据月份发送电子邮件:1个月、2个月等等

下面是我的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();
}

您正在使用一个
SmtpClient
对象,您猜它使用SMTP

在PHP/MySQL中,您经常使用
sendmail
,它能够从服务器发送电子邮件,而无需SMTP服务器

Windows不包含此功能,您必须始终使用某种邮件中继提供商向世界发送电子邮件

如果不想存储用户名/密码,请将SMTP服务器配置为允许来自特定IP或主机名的匿名连接


或者,您可以将SMTP登录凭据存储在您的
app.config

中。您正在使用的
SmtpClient
对象可能使用SMTP

在PHP/MySQL中,您经常使用
sendmail
,它能够从服务器发送电子邮件,而无需SMTP服务器

Windows不包含此功能,您必须始终使用某种邮件中继提供商向世界发送电子邮件

如果不想存储用户名/密码,请将SMTP服务器配置为允许来自特定IP或主机名的匿名连接


或者,您可以将SMTP登录凭据存储在
app.config

中。要按计划发送电子邮件,您应该创建控制台应用程序,并在需要时使用Windows任务计划程序运行该应用程序。 如果无法使用Windows任务计划程序,则可以使用类似于

您可以使用
web.config
/
app.config
文件配置smtp服务器:


要按计划发送电子邮件,应创建控制台应用程序,并在需要时使用Windows任务计划程序运行该应用程序。 如果无法使用Windows任务计划程序,则可以使用类似于

您可以使用
web.config
/
app.config
文件配置smtp服务器:


<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network
          host="localhost"
          port="25"
          defaultCredentials="true"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>