2010-03-10 84 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