Question

I created an html using string builder and placed it in the current directory

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

Now I want to attach this file and send it as a mail. How can I add this as an attachment to my email?

This is what I do more or less with a regular html message. How can I add the file as an attachment to it?

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;
Was it helpful?

Solution 2

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

OTHER TIPS

Here's a CodeProject article that goes over adding an attachment to a MailMessage. I'd consider taking a look at that first, and returning with any questions presented.

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

Here's some MSDN reading as well: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top