2015-05-13 90 views
1

我們試圖一次發送多封電子郵件給公司,其中電子郵件可以被選中或取消選中的列表。如果電子郵件被選中,則需要發送給他們。我設法讓它發送郵件到每個電子郵件地址打勾,但我們希望它是一次一個。 IE瀏覽器,而不是全部一次性發送一封電子郵件Contact1,Contact2,Contact3,我們要發送一封電子郵件Contact1,遍歷,然後第二次發送相同的電子郵件Contact2,等等。迭代通過複選框,發送電子郵件和循環

試過foreach循環,但它只是發送電子郵件三次給每個收件人,而不是一個收件人在同一時間,而只顯示在「收件人」字段的電子郵件。

發送電子郵件的代碼如下:

 var smtp = new SmtpClient 
     { 
      Host = "", 
      Port = 25, 
      EnableSsl = false, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = true, 
     }; 

     //Set message details, ensuring HTML is displayed when sent 
     using (var message = new MailMessage(fromAddress, toAddress) 
     { 
      Subject = subject, 
      Body = body, 
      IsBodyHtml = true, 
      BodyEncoding = System.Text.Encoding.UTF8 
     }) 
      //Try send the email to the users 
      try 
      { 
       smtp.Send(message); //Send the message to selected users 

       Response.Redirect("~/BulkEmail/Index"); //Redirect back to the Index page if send is successful. 
      } 
      catch 
      { 
       Response.Redirect("~/Home/Failure"); 
      } 

這工作得很好,但因爲它會去到外部的郵件,我們希望他們在每一次收到一個。郵件收集和「檢查」的視圖如下:

@{ 
    string Email; 
    Email = User.Identity.Name.Split('\\')[1] + "@email.email"; 
} 
@{ 
    int count = 1; 
} 

@using (Html.BeginForm("SendEmail", "BulkEmail", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
<label>To:</label> @User.Identity.Name.Split('\\')[1]<br /> 
<label>From:</label> <input type="text" name="txtFrom" value="@Email" /><br /> 
<table border="1" width="100%"> 
<label>BCC:</label>@foreach (var item in Model) 
        { 
    <tr width="100%"> 
     <td> 
      @count 
     </td> 
     <td wdith="25"> 
      @Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) 
     </td> 
     <td wdith="25"> 
      @Html.DisplayFor(modelItem => item.tblContact.Company) 
     </td> 
     <td wdith="25"> 
      @Html.DisplayFor(modelItem => item.Email) 
     </td> 
     <td wdith="25" align="center"> 
      <input type="checkbox" name="chkBox" value="@item.Email" checked="checked"/> 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id = item.MailRecipientId }) | 
      @Html.ActionLink("Details", "Details", new { id = item.MailRecipientId }) | 
      @Html.ActionLink("Delete", "Delete", new { id = item.MailRecipientId }) 
     </td> 
    </tr>  
         count += 1; 
        } 
    </table><br /> 
<label>Subject:</label> <input type="text" name="txtSubject" value="Quotations Needed"/> 
<textarea name="message"></textarea><br /> 
<input type="submit" value="BOOM THE EMAILS!" /> 
} 
@Html.Action("TinyMCE", "Include") 

任何幫助,將不勝感激。我覺得這應該是一個foreach循環,但我不是對如何正確地執行它在這種特定情況下確保100%。

感謝

請求的代碼:

  var fromName = @User.Identity.Name.Split('\\')[1]; 
     var fromAddress = Request.Form["txtFrom"]; 
     var toAddress = Request.Form["chkBox"]; 
     var subject = Request.Form["txtSubject"]; 
     var q = Request.Unvalidated.Form; 
     var messageBody = q["message"]; 

控制器更新

public ActionResult SendEmail() 
    { 

     //Create variables for where the message is going internally, setting the subject and body whilst allowing an unvalidated HTML tinyMCE box to be posted. 
     var fromName = @User.Identity.Name.Split('\\')[1]; 
     var fromAddress = Request.Form["txtFrom"]; 
     var toAddress = Request.Form["chkBox[]"]; 
     var subject = Request.Form["txtSubject"]; 
     var q = Request.Unvalidated.Form; 
     var messageBody = q["message"]; 

     String body = messageBody; 

     MailMessage mail_client = new MailMessage(); 
     int index = 0; 
     foreach (var email in toAddress) 
     { 
      mail_client.To.Add(chkBox[index]); 
      index++; 
     } 
     mail_client.From = new MailAddress(fromAddress); 
     mail_client.Subject = subject; 
     mail_client.IsBodyHtml = true; 
     mail_client.Body = body; 


     { 
      //Create new SMTP client and give server details 
      var smtp = new SmtpClient 
      { 
       Host = "", 
       Port = 25, 
       EnableSsl = false, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = true, 
      }; 

      //Set message details, ensuring HTML can be displayed when sent 
      using (var message = new MailMessage(fromAddress, toAddress) 
      { 
       Subject = subject, 
       Body = body, 
       IsBodyHtml = true, 
       BodyEncoding = System.Text.Encoding.UTF8 
      }) 
       //Try send the email to the users 
       try 
       { 
        smtp.Send(message); //Send the message to selected users 

        Response.Redirect("~/BulkEmail/Index"); //Redirect back to the Index page if send is successful. 
       } 
       //If the email doesn't send, allow the page to redirect rather than giving a vile error! 
       catch 
       { 
        Response.Redirect("~/Home/Failure"); 
       } 

     } 
     return View(); 
    } 
+1

顯示在您檢查檢查列表和地址添加到要 – Sachu

+0

@Sachu我已經添加了代碼「的toAddress」區域爲主,後期的代碼。然後在原始文章的第二個代碼塊中使用 –

回答

1

更改名稱chkbox以「chkbox []」 所以在控制器,你會得到一個參數(字符串[] chkbox)只包含檢查的人..

然後創建一個郵件對象

MailMessage mail_client = new MailMessage(); 
int index = 0 
    foreach (string email in chkbox) 
    { 
    mail_client.To.Add(chkbox[index]); 
    index++; 
    } 

mail_client.From = new MailAddress(""); 
mail_client.Subject = subject; 
mail_client.IsBodyHtml = true; 
mail_client.Body = body; 

smtp.Host = ""; 
smtp.Port = ; 
smtp.UseDefaultCredentials = true; 
smtp.Send(mail_client); 

,那麼你可以使用相同的發送郵件

1

添加要發送到陣列,如所有的電子郵件地址:

//Email Address 
List<string> recipients= new List<string>(); 
recipients.Add("Email1"); 
recipients.Add("Email2"); 
recipients.Add("Email3"); 
recipients.Add("Email4"); 
recipients.Add("Email5"); 

然後做一個循環是這樣的:

for (int i = 0; i < recipients.Count; i++) 
{ 
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
    message.IsBodyHtml = true; 
    message.To.Add(recipients[i]); 
    message.Subject = subject; 
    message.From = from; 
    message.Body = body; 
    System.Net.Mail.SmtpClient smtp = smtpClient; 
    smtp.Send(message); 
} 
+0

電子郵件是從數據庫中讀取的,因此將每個電子郵件添加到數組中意味着它們是硬編碼的,不是嗎? –

+0

您仍然可以從他們的數據庫與選擇查詢添加到一個數組。什麼樣的數據庫? –

+0

使用SQL數據庫,使用實體框架 –