2016-06-29 6 views
2

私は以下のコードを使用して、テキストファイルを添付して、このようなテキストファイルの内容を送信します"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メソッドは、文字列変数を作成して、その文字列を改行で区切って保持し、添付ファイルに書き込みます。この

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(); 
} 
関連する問題