/// <summary> /// Constructs an attachment from a body string /// </summary> /// <param name="body">The body text</param> /// <param name="type">The content-type text</param> /// <param name="coding">The text Content-transfer-encoding</param> /// <param name="charset">The text charset</param> /// <example> /// <code> /// MimeAttachment m = new MimeAttachment ("hello", MimeTextContentType.TextPlain, MimeCharset.UTF8); /// </code> /// </example> public MimeAttachment(string body, MimeTextContentType type, MimeTransferEncoding coding, System.Text.Encoding charset) { m_headers["Content-Type"] = MimeConstant.GetContentTypeId(type) + "; charset=\"" + charset.BodyName + "\" "; m_headers["Content-Transfer-Encoding"] = MimeConstant.GetTransferEncodingId(coding); m_headers["Content-Disposition"] = "inline;\r\n"; m_bodyBuilder = new StringBuilder(); m_bodyBuilder.Append(endl); if (charset == System.Text.Encoding.ASCII) { m_bodyBuilder.Append(body); } else { if (coding == MimeTransferEncoding.QuotedPrintable) { m_bodyBuilder.Append(QPEncoder.Encode(body, charset)); } else if (coding == MimeTransferEncoding.Base64) { m_bodyBuilder.Append(MimeEncoder.StringBase64(body, charset, 78)); } else { m_bodyBuilder.Append(body); } } m_bodyBuilder.Append(endl); m_body = m_bodyBuilder.ToString(); m_bodyBuilder = null; }
/// <summary> /// Returns a string identifying a Mime text type /// </summary> /// <param name="type">A text type</param> /// <returns>A string identifying the Mime text type</returns> public static string GetContentTypeId(MimeTextContentType type) { switch (type) { case MimeTextContentType.TextHtml: return("text/html"); case MimeTextContentType.TextPlain: return("text/plain"); default: return(""); // default to text/plain in mime standard } }
/// <summary> /// Constructs an attahcment from string /// </summary> /// <param name="body">A string containing the attachment body text</param> /// <param name="type">The Content-Type of the attachement</param> /// <remarks> /// This method initializes a body text with utf8 charset, base64 transfer encoding and inline disposition. /// </remarks> public MimeAttachment(string body, MimeTextContentType type) { System.Text.Encoding encoding; if (QPEncoder.IsAscii(body)) { encoding = System.Text.Encoding.ASCII; } else { encoding = Config.defaultEncoding; } m_headers["Content-Type"] = MimeConstant.GetContentTypeId(type) + "; charset=\"" + encoding.BodyName + "\" "; if (encoding == System.Text.Encoding.ASCII) { m_headers["Content-Transfer-Encoding"] = MimeConstant.GetTransferEncodingId(MimeTransferEncoding.Ascii7Bit); } else { m_headers["Content-Transfer-Encoding"] = MimeConstant.GetTransferEncodingId(MimeTransferEncoding.Base64); } m_headers["Content-Disposition"] = "inline;\r\n"; m_bodyBuilder = new StringBuilder(); //m_bodyBuilder.Append (endl); if (encoding == System.Text.Encoding.ASCII) { m_bodyBuilder.Append(body); } else { m_bodyBuilder.Append(MimeEncoder.StringBase64(body, Config.defaultEncoding, 78)); } m_bodyBuilder.Append(endl); m_body = m_bodyBuilder.ToString(); m_bodyBuilder = null; }
/// <summary> /// Initializes the body /// </summary> /// <param name="body">The body text</param> /// <param name="TransferEncoding">The Transfer encoding format</param> /// <param name="ContentType">The Mime content type</param> /// public void SetBody(string body, MimeTransferEncoding TransferEncoding, MimeTextContentType ContentType) { string type = ""; switch (ContentType) { case MimeTextContentType.TextPlain: type = "text/plain"; break; case MimeTextContentType.TextHtml: type = "text/html"; break; default: throw (new MimeException("Unknown ContentType")); } m_headers["MIME-Version"] = "1.0"; switch (TransferEncoding) { case MimeTransferEncoding.Ascii7Bit: m_headers["Content-Type"] = type + ";\r\n charset=us-ascii"; m_headers["Content-Transfer-Encoding"] = "7bit"; m_body = body; break; case MimeTransferEncoding.QuotedPrintable: m_headers["Content-Type"] = type + ";\r\n charset=" + Config.defaultEncoding.BodyName; m_headers["Content-Transfer-Encoding"] = "quoted-printable"; m_body = QPEncoder.Encode(body, Config.defaultEncoding); break; case MimeTransferEncoding.Base64: m_headers["Content-Type"] = type + ";\r\n charset=" + Config.defaultEncoding.BodyName; m_headers["Content-Transfer-Encoding"] = "base64"; m_body = MimeEncoder.StringBase64(body, Config.defaultEncoding, 78); break; } }
/// <summary> /// Build a MimeMessage with text body and an image as attachment /// </summary> /// <param name="From">The from address</param> /// <param name="To">The destination address</param> /// <param name="Subject">The mail subject</param> /// <param name="Body">The mail body text</param> /// <param name="TextType">The body Content-Type</param> /// <param name="image">The image to send as attachment</param> /// <param name="format">The image format</param> /// <param name="name">The image name</param> /// <returns>A MimeMessage</returns> /// <remarks>All text is encoded with the default charset define in SmtPop.Config</remarks> /// public static MimeMessage Build(string From, string To, string Subject, string Body, MimeTextContentType TextType, Image image, ImageFormat format, string name) { MimeMessage m = new MimeMessage(); m.AddressFrom.Add(new MailAddress(From)); m.AddressTo.Add(new MailAddress(To)); m.SaveAdr(); m.SetSubject(Subject, MimeTransferEncoding.Base64); // define text as first attachment m.AddAttachment(new MimeAttachment(Body, TextType)); // build an attachment with the image MemoryStream stream = new MemoryStream(0); image.Save(stream, format); stream.GetBuffer(); string mime = "image/" + format.ToString().ToLower(); if (name.IndexOf('.') == -1) { name += "." + format.ToString().ToLower(); } m.AddAttachment(new MimeAttachment(name, name, mime, stream.GetBuffer(), MimeAttachment.MimeDisposition.attachment)); stream.Close(); return(m); }
/// <summary> /// Build a MimeMessage with text Body /// </summary> /// <param name="From">The From address</param> /// <param name="To">The destination address</param> /// <param name="Subject">The subject</param> /// <param name="Body">The body text</param> /// <param name="TextType">The body Content-Type</param> /// <returns>A MimeMessage</returns> /// <remarks>All text is encoded with the default charset define in SmtPop.Config</remarks> /// <example> /// <code> /// MimeMessage m = MessageBuilder (<[email protected]>,<[email protected]>,"Test","This is a test", MimeTextContentType.TextPlain); /// /// Smtp.Send (m); /// /// </code> /// </example> public static MimeMessage Build(string From, string To, string Subject, string Body, MimeTextContentType TextType) { MimeMessage m = new MimeMessage(); m.AddressFrom.Add(new MailAddress(From)); m.AddressTo.Add(new MailAddress(To)); m.SaveAdr(); m.SetSubject(Subject); m.SetBody(Body, MimeTransferEncoding.Base64, TextType); return(m); }
/// <summary> /// Build a MimeMessage with text body and binary data as attachment /// </summary> /// <param name="From">The source address</param> /// <param name="To">The destination address</param> /// <param name="Subject">The mail subjec</param> /// <param name="Body">The text body</param> /// <param name="TextType">The body Content-Type</param> /// <param name="data">The object to serialize in binary data</param> /// <param name="MimeType">The Mime type of binary data (ie "application/binary")</param> /// <param name="Name">The attachment's name</param> /// <returns>A MimeMessage</returns> /// <remarks>All text is encoded with the default charset define in SmtPop.Config<br/>The data Object must be binary serializable</remarks> public static MimeMessage Build(string From, string To, string Subject, string Body, MimeTextContentType TextType, Object data, string MimeType, string Name) { MimeMessage m = new MimeMessage(); m.AddressFrom.Add(new MailAddress(From)); m.AddressTo.Add(new MailAddress(To)); m.SaveAdr(); m.SetSubject(Subject, MimeTransferEncoding.Base64); // define text as first attachment m.AddAttachment(new MimeAttachment(Body, TextType)); // build an attachment with data MemoryStream mem = new MemoryStream(); BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(mem, data); m.AddAttachment(new MimeAttachment(Name, Name, MimeType, mem.GetBuffer(), MimeAttachment.MimeDisposition.attachment)); mem.Close(); return(m); }
/// <summary> /// Build a MimeMessage with text body and binary data as attachment /// </summary> /// <param name="From">The source address</param> /// <param name="To">The destination address</param> /// <param name="Subject">The mail subjec</param> /// <param name="Body">The text body</param> /// <param name="TextType">The body Content-Type</param> /// <param name="Stream">The stream that contain binary data</param> /// <param name="MimeType">The Mime type of binary data (ie "application/binary")</param> /// <param name="Name">The attachment's name</param> /// <returns>A MimeMessage</returns> /// <remarks>All text is encoded with the default charset define in SmtPop.Config</remarks> public static MimeMessage Build(string From, string To, string Subject, string Body, MimeTextContentType TextType, BinaryReader Stream, string MimeType, string Name) { MimeMessage m = new MimeMessage(); m.AddressFrom.Add(new MailAddress(From)); m.AddressTo.Add(new MailAddress(To)); m.SaveAdr(); m.SetSubject(Subject, MimeTransferEncoding.Base64); // define text as first attachment m.AddAttachment(new MimeAttachment(Body, TextType)); // build an attachment with data MemoryStream tstream = new MemoryStream(0); tstream.Write(Stream.ReadBytes((int)Stream.BaseStream.Length), 0, (int)Stream.BaseStream.Length); m.AddAttachment(new MimeAttachment(Name, Name, MimeType, tstream.GetBuffer(), MimeAttachment.MimeDisposition.attachment)); tstream.Close(); return(m); }
/// <summary> /// Build a MimeMessage with text body and binary as attachment /// </summary> /// <param name="From">The source address</param> /// <param name="To">The destination address</param> /// <param name="Subject">The mail subject</param> /// <param name="Body">The mail body text</param> /// <param name="TextType">The body Content-Type</param> /// <param name="Data">The binary data to send as attachment</param> /// <param name="MimeType">The mime type for binary data (ie "application/binary")</param> /// <param name="Name">The name of the attachment</param> /// <returns>A MimeMessage</returns> /// <remarks>All text is encoded with the default charset define in SmtPop.Config</remarks> public static MimeMessage Build(string From, string To, string Subject, string Body, MimeTextContentType TextType, Byte[] Data, string MimeType, string Name) { MimeMessage m = new MimeMessage(); m.AddressFrom.Add(new MailAddress(From)); m.AddressTo.Add(new MailAddress(To)); m.SaveAdr(); m.SetSubject(Subject, MimeTransferEncoding.Base64); // define text as first attachment m.AddAttachment(new MimeAttachment(Body, TextType)); // build an attachment with data m.AddAttachment(new MimeAttachment(Name, Name, MimeType, Data, MimeAttachment.MimeDisposition.attachment)); return(m); }