将文件附加到邮件

时间:2013-01-17 13:48:50

标签: c# sendmail email-attachments mailmessage

我使用字符串构建器创建了一个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;

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

这是一篇关于向MailMessage添加附件的CodeProject文章。我考虑首先考虑一下,然后回答任何问题。

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

答案 2 :(得分:1)

Attachment attachment = new Attachment(attachmentPath);
msg.Attachments.Add(attachment);