使用c#将字符串列表写入文本文件,并将其作为附件添加到邮件中

本文关键字:添加 列表 字符串 使用 文件 文本 | 更新日期: 2023-09-27 17:53:13

我使用下面的代码发送一封带有文本文件作为附件的邮件,文本文件的内容如下"License ADD LOGJz+RC-cVecSpK5-57AOrUlA-6aD3n5Fy-ejFGFIIa-XPU"

    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

我如何将这些字符串列表作为内容添加到使用上面的代码片段的文本文件..有人能帮帮忙吗?提前感谢....

使用c#将字符串列表写入文本文件,并将其作为附件添加到邮件中

将输入参数更改为List<string>,并在方法中创建一个字符串变量来保存以换行符分隔的字符串,将这些字符串写入附件文件

这样的

 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
        }

这是我用来将文件附加到电子邮件中的代码。

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("fake@false.nl", "yourName");
    oMail.To.Add(new MailAddress("fake@false.nl"));
    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();
}