2016-06-29 24 views
2

我正在使用下面的代碼來發送帶有文本文件的郵件作爲附件以及像這樣的文本文件的內容「License ADD LOGJz + RC- cVecSpK5-57AOrUlA-6aD3n5Fy-ejFGFIIa-XPU」將字符串列表寫入到文本文件中作爲附件添加到郵件中使用c#

public static bool sendMailFromToCCSubMsgAttachment(string fromName, string fromAdd, string toline, string subj, string htmlmsg, string cc, string strAttachment) 
    { 

     SmtpClient smtpClient = new SmtpClient(ConfigurationSettings.AppSettings["MAIL_SERVER"], Int32.Parse(ConfigurationSettings.AppSettings["SERVER_PORT"])); 
     MailMessage mail = new MailMessage(); 

     if (!string.IsNullOrEmpty(strAttachment)) 
     { 
      ContentType ct = new ContentType(); 
      ct.MediaType = MediaTypeNames.Text.Plain; 

      Attachment attachFile = Attachment.CreateAttachmentFromString(strAttachment, ct); 



      mail.Attachments.Add(attachFile); 
     } 
    ......... 
    ......... 
    .......... 
    } 

在這裏,我們通過上面的內容‘strAttachment’變量這種方法 我現在正在傳遞的字符串列表的內容以文本文件和下面的內容格式..

License ADD 2N674h5A-cVc9XiCG-N0TChPo3-mRVmOtUm-GYup9evK-3d4 
License ADD VljH169B-cVe22hrW-U/HMICqW-1aeB5pJE-YpZIOThd-eBc 
License ADD FIsc70dC-cVeVN9Ed-J833n4q4-vyMgnOXM-HjsMKrhT-qy0 
License ADD LOGJz+RC-cVecSpK5-57AOrUlA-6aD3n5Fy-ejFGFIIa-XPU 

我如何使用上面的代碼片斷添加字符串列表這些作爲內容的文本文件.. 會不會有人請就這個..幫助 提前許多感謝....

+0

正在使用這些字符串在一個選項臨時文件 - 這將是快速和簡單的,也許否則內存流? – BugFinder

回答

2

更改輸入參數到List<string>和inside方法創建一個字符串變量來保存由Newline分隔的字符串,將它們寫入您的附件文件。

像這樣

public static bool sendMailFromToCCSubMsgAttachment(string fromName, string fromAdd, string toline, string subj, string htmlmsg, string cc, List<string> strAttachment) 
     { 

      SmtpClient smtpClient = new SmtpClient(ConfigurationSettings.AppSettings["MAIL_SERVER"], Int32.Parse(ConfigurationSettings.AppSettings["SERVER_PORT"])); 
      MailMessage mail = new MailMessage(); 
      string strAttachmentboyd = string.Join(Environment.NewLine, strAttachment); 
      // your other code 
     } 
+1

很多謝謝.. :) –

0

這是我使用的文件附加到電子郵件的代碼。

public static void sendMailWithAttachment(string strAttachment) 
{ 
//insert the licence into a memorystream. 
//Note not using the 'using' statement with the MemoryStream and StreamWriter. 
//If you do you will get a 'cannot access a closed stream' error when attaching the stream into the email 
MemoryStream stream = new MemoryStream(); 
StreamWriter writer = new StreamWriter(stream); 
writer.Write(strAttachment); 
writer.Flush(); 
stream.Position = 0; 

//start the mail sending 
using (SmtpClient client = new SmtpClient()) 
using (MailMessage oMail = new MailMessage()) 
{ 
    //mail config settings 
    client.Port = 25; 
    client.Host = "localhost"; 
    client.Timeout = 30000; 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.UseDefaultCredentials = false; 
    client.Credentials = new NetworkCredential("userName", "passWord"); 

    //mail content stuff 
    oMail.From = new MailAddress("[email protected]", "yourName"); 
    oMail.To.Add(new MailAddress("[email protected]")); 
    oMail.Subject = "Your Licence"; 
    oMail.IsBodyHtml = true; 
    oMail.Body = "<html><head></head><body> Hello </body></html>"; 

    //insert the text file as attatchment from the stream 
    if (!string.IsNullOrEmpty(strAttachment)) 
    { 
     oMail.Attachments.Add(new Attachment(stream, "Licence.txt", "text/plain")); 
    } 

    //send the mail 
    client.Send(oMail); 
} 

//dispose the stream and writer containing the license 
writer.Dispose(); 
stream.Dispose(); 
} 
相關問題