加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:56:35 所属栏目:百科 来源:网络整理
导读:我需要按计划发送大量电子邮件(可能每天数百封). 我想要这样做的方式如下,但问题是我的Body字段可以变得非常大,如果我将它添加为字符串,它会变得难看. SmtpClient client = new SmtpClient(); //host and port picked from web.config client.EnableSsl = tr
我需要按计划发送大量电子邮件(可能每天数百封).
我想要这样做的方式如下,但问题是我的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)
        {

        }

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

MailDefinition message = new MailDefinition();  

    message.BodyFileName = @"~EmailTemplateTemplate1.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());

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

我的问题是:

>什么是发送批量的最佳方式
来自winservice的电子邮件?
>从gmail发送它是否可行
帐户?或者他们会阻止
我过了一会儿?

谢谢你的帮助.

解决方法

为什么不使用与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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读