【问题标题】:attaching a file to a message将文件附加到消息
【发布时间】:2013-01-01 01:50:45
【问题描述】:

我使用字符串生成器创建了一个html并将其放在当前目录中

StreamWriter sw = new StreamWriter("../../Data.html");

现在我想附上这个文件并作为邮件发送。如何将此作为附件添加到我的电子邮件中?

这或多或少是我对常规 html 消息所做的。如何将文件添加为附件?

public bool sendMailAttachment(string to, string from, string subject, string body, string attachment)
    {
        bool k = false;
        try
        {
            SmtpClient client;
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = subject;
            msg.Body = body;
            msg.IsBodyHtml = true;

            client = new SmtpClient();
            client.Host = "staging.itmaniax.co.za";
            //client.Port = 25;

            //****
            //client.EnableSsl = true;
            client.Send(msg);

            k = true;

        }
        catch (Exception exe)
        {
            Console.WriteLine(exe.ToString());
        }
        return k;

【问题讨论】:

  • MailMessage 对象上有一个 Attachments 属性......
  • 在创建新问题之前,请回顾您之前的问题并接受任何对您有用的答案...例如,您昨天问了一个 非常 类似的问题,它是回答:stackoverflow.com/questions/14357877/…

标签: c# sendmail email-attachments mailmessage


【解决方案1】:

【讨论】:

    【解决方案2】:

    这是一篇 CodeProject 文章,介绍了将附件添加到 MailMessage。我会考虑先看一下,然后返回提出的任何问题。

    http://www.codeproject.com/Articles/10828/Sending-Email-with-attachment-in-ASP-NET-using-SMT

    这里还有一些 MSDN 阅读:http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

    【讨论】:

      【解决方案3】:
      Attachment attachment = new Attachment(attachmentPath);
      msg.Attachments.Add(attachment);
      

      【讨论】:

      • 需要注意的是,如果您没有正确处理该附件,它将无限期地保持文件打开。
      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 2020-11-08
      • 2013-03-31
      • 2012-12-03
      • 1970-01-01
      • 2014-05-26
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多