2010-03-10 85 views
6

我需要发送大量电子邮件的(大概有几百天)交付基础发送电子邮件的正确方式。 我想这样做的方式如下,但问题是,我的身体领域可以变得非常大,如果我将它添加为字符串它变得丑陋。从Windows服务

 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("[email protected]"); 
     message.Subject = "My subject"; 
     message.To.Add(new MailAddress("[email protected]")); 
     try 
     { 
      client.Send(message); 
     } 
     catch (Exception) 
     { 

     } 

,当我不得不从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()); 

我认为这是很好的解决方案,但我不想提及System.Web.UI.WebControls.MailDefinition 因为我在winservice。

我的问题是:

  1. 什么是从WINSERVICE发送大量电子邮件 最好的方式?
  2. 它是更多钞票从Gmail 帐户发送了吗?或者过了一段时间他们会阻止我 ?

感谢您的帮助。

+0

我认为Gmail将有500个收件人在24小时内限制。但是,我不鼓励使用GMail进行批量电子邮件;它可能违反了他们的TOS。 – 2010-03-10 17:11:09

+0

感谢Ryan, 这里是一个帖子,更详细地解释它 http://www.labnol.org/internet/email/gmail-daily-limit-sending-bulk-email/2191/ 所以我想我将不得不停止使用Gmail发送电子邮件:( – UshaP 2010-03-10 18:51:22

回答

5

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

所有你正在做的是一个foreach过的信息的数据集与模板合并。加载您的合并数据,循环合并数据,替换当前合并记录中模板中的标记。将消息正文设置为当前生成的消息。将消息附加到消息队列或现在发送,无论您选择哪一个。

这不是火箭科学。你已经有了创建消息的代码,所以这只是一个加载合并数据并循环的例子。我已经简化说明概念,我已经用于合并数据的CSV和假设没有字段包含任何逗号:

message.IsBodyHtml = true; 
message.From = new MailAddress("[email protected]"); 
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); 
} 
+0

我认为有另一种方式,而不是加载htm作为字符串和替换令牌。但我想没有。 感谢您的回答。 – UshaP 2010-03-10 18:54:59

+0

@UshaP - 加载htm的方法有很多种,但并不总是归结为“做什么最聪明的方式”。通常最简单的方法是最好的。您可以(如其他人所建议的那样)将htm作为XML文档加载并使用XPath来查询和设置值。我会说这完全是矫枉过正,因为这个特殊情况的聪明而过于聪明。 – BenAlabaster 2010-03-10 19:14:35

+0

也许这是一个微不足道的问题,但我不知道如何动态地采取我的HTM路径​​。我有一个参考BL类库项目的Windows服务,在那里我把我的HTM文件。 File.ReadAllText我正在看'C:\ Windows \ system32 \ .. – UshaP 2010-03-10 22:38:51

0

简单的回答是这样的:

message.Body = File.ReadAllText(@"~\EmailTemplate\Template1.htm"); 

我不知道你的具体问题,但我敢肯定,别人会回答这些:)

0

这有什么错用

"messageBody".Replace("<% Name %>", curr.Name).Replace(....)...

至于从gMail发送邮件,你可以通过SMTP来完成,但他们不会让你欺骗另一个“发件人”地址,所以收件人会将其视为来自gMail地址。

0

我会用一些模板引擎。例如StringTemplate

+3

哇,该网站是眼睛很难。 – 2010-03-10 17:15:50