Exemple #1
0
        private static MailAttachment CreateHtmlAttachment(string name, string html)
        {
            Debug.AssertStringNotEmpty(name);
            Debug.AssertStringNotEmpty(html);

            return(MailAttachment.CreateAttachmentFromString(html,
                                                             name + ".html", Encoding.UTF8, "text/html"));
        }
        private static MailAttachment CreateHtmlAttachment(string name, string html)
        {
            Debug.AssertStringNotEmpty(name);
            Debug.AssertStringNotEmpty(html);

#if NET_1_0 || NET_1_1
            //
            // Create a temporary file to hold the attachment. Note that
            // the temporary file is created in the location returned by
            // System.Web.HttpRuntime.CodegenDir. It is assumed that
            // this code will have sufficient rights to create the
            // temporary file in that area.
            //

            string fileName = name + "-" + Guid.NewGuid().ToString() + ".html";
            string path     = Path.Combine(HttpRuntime.CodegenDir, fileName);

            try
            {
                using (StreamWriter attachementWriter = File.CreateText(path))
                    attachementWriter.Write(html);

                return(new MailAttachment(path));
            }
            catch (IOException)
            {
                //
                // Ignore I/O errors as non-critical. It's not the
                // end of the world if the attachment could not be
                // created (though it would be nice). It is more
                // important to get to deliver the error message!
                //

                return(null);
            }
#else
            return(MailAttachment.CreateAttachmentFromString(html,
                                                             name + ".html", Encoding.UTF8, "text/html"));
#endif
        }
Exemple #3
0
        private void SendWithAttachments(MailMessage message)
        {
            string boundary = GenerateBoundary();

            // first "multipart/mixed"
            ContentType messageContentType = new ContentType();

            messageContentType.Boundary  = boundary;
            messageContentType.MediaType = "multipart/mixed";
            messageContentType.CharSet   = null;

            SendHeader(HeaderName.ContentType, messageContentType.ToString());
            SendData(String.Empty);

            // body section
            Attachment body = null;

            if (message.AlternateViews.Count > 0)
            {
                SendWithoutAttachments(message, boundary, true);
            }
            else
            {
                body = Attachment.CreateAttachmentFromString(message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain");
                message.Attachments.Insert(0, body);
            }

            try {
                SendAttachments(message, body, boundary);
            } finally {
                if (body != null)
                {
                    message.Attachments.Remove(body);
                }
            }

            EndSection(boundary);
        }