2012-05-09 28 views
-6

我试图一次性向超过一百人发送带有附件的电子邮件,并且我发现非常好的源代码可以与附件完美协作,但不能一次向多个人发送邮件。如何通过此代码发送更多人的邮件?

所以,现在我的问题是:你能帮我修改现有的代码,使其工作?

编程语言:C#

这里是代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Mail; 

namespace ConsoleApplication3 
{ 

    public class GmailAccount 
    { 
     public string Username; 
     public string Password; 
     public string DisplayName; 

     public string Address 
     { 
      get 
      { 
       return Username + "@gmail.com"; 
      } 
     } 

     private SmtpClient client; 

     public GmailAccount(string username, string password, string displayName = null) 
     { 
      Username = username; 
      Password = password; 
      DisplayName = displayName; 

      client = new SmtpClient 
      { 
       Host = "smtp.gmail.com", 
       Port = 587, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       Credentials = new NetworkCredential(Address, password) 
      }; 
     } 

     public void SendMessage(string targetAddress, string subject, string body, params string[] files) 
     { 
      MailMessage message = new MailMessage(new MailAddress(Address, DisplayName), new MailAddress(targetAddress)) 
      { 
       Subject = subject, 
       Body = body 
      }; 

      foreach (string file in files) 
      { 
       Attachment attachment = new Attachment(file); 
       message.Attachments.Add(attachment); 
      } 

      client.Send(message); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      GmailAccount acc = new GmailAccount("username", "password", "Caption"); 
      acc.SendMessage("[email protected]", "Hello World!", "like in the title...", "C:\\temp.rar"); 
     } 
    } 
    } 
+1

你好想成为垃圾邮件机器人号10230920:P除非您选择只邮寄希望邮寄,并签署了它的人,不要期望太大帮助其被问这么多次在这里:P – RhysW

+0

很多事情......我从我自己的类似代码工作,但发送多个附件的问题.. – John

+0

约翰他的观点是告诉我们你已经尝试或至少告诉我们,所以我们可以说为什么它不工作... – RhysW

回答

0

传递更多价值发信息。

示例代码将邮件发送到多人:

public bool SendEmail() 
{ 
    bool status = false; 

     try 
     { 
      //code to send email 
      this._mail = new MailMessage(); 

      this._mail.From = new MailAddress(this.From, this.DisplayName); 

      if (!string.IsNullOrEmpty(this.To)) 
      { 
       var distinctAddress = new List<string>(this.To.Split(',').Distinct()); 
       this.To = string.Empty; 

       foreach (string address in distinctAddress) // Loop through all strings 
       { 
        this.To += address + ","; // Append string to StringBuilder 
       } 
       this.To = this.To.TrimEnd(','); 
       this._mail.To.Add(this.To); 
      } 

      if (!string.IsNullOrEmpty(this.CC)) 
       this._mail.CC.Add(this.CC); 

      if (!string.IsNullOrEmpty(this.BCC)) 
       this._mail.Bcc.Add(this.BCC); 

      this._mail.Subject = this.Subject; 

      this._mail.IsBodyHtml = this.IsBodyHtml; 

      this._mail.Body = this.Body; 

      if (this.Priority == true) 
      { 
       this._mail.Priority = MailPriority.High; 
      } 

      this._mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; 

      if (this._attachments != null && this._attachments.Count > 0) 
      { 
       foreach (string file in this._attachments) 
       { 
        Attachment attachment = new Attachment(file); 
        this._mail.Attachments.Add(attachment); 
       } 
      } 

      this._smtpServer = new SmtpClient(this.EmailServer); 
      this._smtpServer.EnableSsl = this.EnableSsl; 
      this._smtpServer.Port = this.Port; 
      this._smtpServer.Credentials = new System.Net.NetworkCredential(this.UserId, this.Password); 

      if (String.IsNullOrEmpty(this.To) != true || string.IsNullOrEmpty(this.CC) != true || string.IsNullOrEmpty(this.BCC) != true) 
       this._smtpServer.Send(this._mail); 
      status = true; 
     } 
     catch (Exception ex) 
     { 
     } 
    return status; 
} 
+0

thnx for answere我会尝试一下... – John

+3

* facepalm *教新人自己做事情的方式,而不是依靠其他人为他做:P – RhysW

1

我认为这是你在找什么:在的targetAddress参数用逗号splited

message.To.Add(new MailAddress("[email protected]"); 
+1

他会更好地使用包含他想要邮寄并遍历循环的整个数百人的列表“[email protected]”是我在哪里我是循环计数和列表编号,然后运行直到它到达列表的末尾,使用你的代码循环内的一个变化,我建议:)(不带你离开,加入你的答案,使其更有用) – RhysW

+1

我同意。既然他没有在他的问题上付出太多努力,我不想给他所有的一切。 –