2012-12-19 35 views
-2

我需要一個最好的(發行性能)代碼發送郵件到多個電子郵件地址以附件的文件,並在C#送貨到達代碼發送郵件到多個電子郵件地址以附件和可送貨

+3

你有什麼試過?你卡在哪裏?你有沒有看過[System.Net.Mail](http://msdn.microsoft.com/en-us/library/system.net.mail.aspx)命名空間? – Heinzi

+0

非常好。嘗試一下,使用谷歌的inital幫助搜索,然後如果你在任何地方張貼問題與任何你已經嘗試。 – Maheep

回答

2

這是從CSV文件中讀取姓名,電子郵件地址以便發送到這些地址的代碼。它涉及許多功能。每一個都是明確的。

//Declare these two variables global in the form, class 
MailMessage message;//= new MailMessage() 
SmtpClient smtpClient;//= new SmtpClient() 

//A function that will read data from CSV File into List and will create a  DataTable 
public DataTable maketable() 
    { 
     string path = this.Server.MapPath("app_data"); 
     path += "\\employeelist.csv"; 
     List<string[]> testParse = 
      parseCSV(path); 
     DataTable newTable = new DataTable(); 
     foreach (string column in testParse[0]) 
     { 
      newTable.Columns.Add(); 
     } 

     foreach (string[] row in testParse) 
     { 
      newTable.Rows.Add(row); 
     } 
     return newTable; 
    } 
//This Functions Actually reads the CSV file and make list 
public List<string[]> parseCSV(string path) 
    { 
     List<string[]> parsedData = new List<string[]>(); 
     try 
     { 
      using (StreamReader readFile = new StreamReader(path)) 
      { 
       string line; 
       string[] row; 
       while ((line = readFile.ReadLine()) != null) 
       { 
        row = line.Split(','); 
        parsedData.Add(row); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      parent.list.Items.Add(e.Message); 
     } 
     return parsedData; 
    } 

//I have called below function with given subject and body text as a string at any place.. either in button_click event or any other place 
SendAttendanceWithAttachment(email_subject, body); 
// Now the body of SendAttendanceWithAttachment(.....); 
void SendAttendanceWithAttachment(string subject, string body) 
    { 
     string FileAbsolutePath = "C:\\att\\"; // where the directory is containing files for attachment 
     string FileName = ""; 
     Attachment attach; 
     //OPen CSV File Read all contact and then send one by one. 
     smtpClient = new SmtpClient(); 
     smtpClient.Host = "<smtp host>";//like for google : ssl://smtp.gmail.com 
     smtpClient.Port = <int>;// port number for google 465 
     smtpClient.UseDefaultCredentials = true; 
     smtpClient.Credentials = new System.Net.NetworkCredential("<gmail address>", "<password>"); 

     MailAddress fromAddress = new MailAddress("<sender email address>"); 
     dt = maketable(); 
     try 
     { 
      add_item("Weekly attendance Sending starts...."); 
      for (int i = 0; i < dt.Rows.Count; i++)//dt.Rows.Count 
      { 
       message = new MailMessage(); 
       if (dt.Rows[i][1].ToString() == string.Empty) 
       { 
        continue; 
       } 
       else 
       { 
        FileName = FileAbsolutePath + dt.Rows[i][0].ToString() + ".csv"; 
        if (File.Exists(FileName)) 
        { 
         attach = new Attachment(FileName); 
         //Add in data 
         message.To.Add(dt.Rows[i][1].ToString()); 
         message.Subject = subject; 
         message.IsBodyHtml = true; 
         message.Body = body; 
         message.From = fromAddress; 
         message.Attachments.Add(attach); 
         //add_item("Sending Message " + (i+1) + " of " + dt.Rows.Count + " to : " + dt.Rows[i][0].ToString()); 
         SendMail(message, smtpClient); 
         //add_item("Email Sent to :" + dt.Rows[i][0].ToString()); 
        } 
       } 
      } 
      //add_item("Weekly attendance Sent to all.."); 
     } 
     catch (System.Exception e) 
     { 
      //parent.list.Items.Add(e.Message); 
     } 
//Finally the SendMail()... 
public void SendMail(MailMessage message, SmtpClient smtpClient) 
    { 
     try 
     { 
      smtpClient.Send(message); 
     } 
     catch (Exception ex) 
     { 
      //parent.list.Items.Add(ex.Message); 
     } 
    } 

它是一個完整的過程中,您是如何從一個CSV文件的讀取,添加地址列表,然後到數據表,然後組成帶有文件附件和發送電子郵件。希望你會從中受益。

+0

在「SendAttendanceWithAttachment」方法'code'message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;'中添加一行 – DareDevil

相關問題