0

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

3 回答 3

2

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

上面的答案只是一个谷歌。

于 2013-01-17T13:51:46.253 回答
1

这是一篇 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

于 2013-01-17T13:52:45.913 回答
1
Attachment attachment = new Attachment(attachmentPath);
msg.Attachments.Add(attachment);
于 2013-01-17T14:28:37.457 回答