从Windows服务发送电子邮件的正确方法

Ush*_*haP 7 .net c# email windows-services smtp

我需要按计划发送大量电子邮件(可能每天数百封).我想要这样做的方式如下,但问题是我的Body字段可以变得非常大,如果我将它添加为字符串,它会变得难看.

        SmtpClient client = new SmtpClient(); //host and port picked from web.config
        client.EnableSsl = true;

        MailMessage message = new MailMessage();

        message.Body = "test from winservice"; // HERE IS MY PROBLEM
        message.IsBodyHtml = true;
        message.From = new MailAddress("donotreply@abcde.com");
        message.Subject = "My subject";
        message.To.Add(new MailAddress("piniusha@abcde.com"));
        try
        {
            client.Send(message);
        }
        catch (Exception)
        {

        }
Run Code Online (Sandbox Code Playgroud)

当我不得不从我使用的aspx页面做到这一点

    MailDefinition message = new MailDefinition();  

    message.BodyFileName = @"~\EmailTemplate\Template1.htm";
    ListDictionary replacements = new ListDictionary();
    replacements.Add("<% Name %>", this.txtName.Text);
    replacements.Add("<% PhoneOrEmail %>", this.txtPhoneOrEmail.Text);
    replacements.Add("<% Message %>", this.txtMessage.Text);
    MailMessage msgHtml = message.CreateMailMessage(RECIPIENTS, replacements, new LiteralControl());
Run Code Online (Sandbox Code Playgroud)

我认为这是优雅的解决方案,但我不想引用System.Web.UI.WebControls.MailDefinition,因为我在winservice.

我的问题是:

  1. 从winservice发送批量电子邮件的最佳方式是什么?
  2. 从gmail帐户发送它是否可行?或者他们会在一段时间后阻止我?

谢谢你的帮助.

Ben*_*ter 5

为什么不使用与MailDefinition使用完全相同的概念?从模板文件加载正文,用另一个列表中的文本替换一些标记 - 邮件合并样式?

您所做的只是对要与模板合并的信息数据集进行预测.加载合并数据,循环合并数据,用当前合并记录替换模板中的标记.将邮件正文设置为当前构建的邮件.将消息附加到消息队列或立即发送,无论您选择哪个.

这不是火箭科学.您已经获得了创建消息的代码,因此只需加载合并数据并循环遍历即可.我已经简化了演示这个概念,并且我使用了CSV作为合并数据,并假设没有字段包含任何逗号:

message.IsBodyHtml = true;
message.From = new MailAddress("MailSender@MyCompany.com");
message.Subject = "My bogus email subject";

string[] lines = File.ReadAllLines(@"~\MergeData.csv");
string originalTemplate = File.ReadAllText(@"~\Template.htm");

foreach(string line in lines)
{
    /* Split out the merge data */
    string[] mergeData = line.Split(',');

    /* Reset the template - to revert changes made in previous loop */
    string currentTemplate = originalTemplate;

    /* Replace the merge tokens with actual data */
    currentTemplate = currentTemplate.Replace("[[FullNameToken]]", mergeData[0]); 
    currentTemplate = currentTemplate.Replace("[[FirstNameToken]]", mergeData[1]);
    currentTemplate = currentTemplate.Replace("[[OtherToken]]", mergeData[2]);

    /*... other token replacements as necessary.
     * tokens can be specified as necessary using whatever syntax you choose
     * just make sure that there's something denoting the token so you can
     * easily replace it */

    /* Transfer the merged template to the message body */
    message.Body = currentTemplate;

    /* Clear out the address from the previous loop before adding the current one */
    message.To.Clear();
    message.To.Add(new MailAddress(mergeData[3]));
    client.Send(message);
}
Run Code Online (Sandbox Code Playgroud)