/// <summary> /// Create a new e-mail content type by parsing the given string. /// </summary> /// <param name="ContentTypeString"></param> public MailContentType(AbstractEMail EMailHeader, String ContentTypeString) { this._EMailHeader = EMailHeader; this._Text = ContentTypeString; var Splitted = ContentTypeString. Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries). Select(v => v.Trim()). ToArray(); if (Splitted.Length == 0) this._ContentType = MailContentTypes.text_plain; else if (!Enum.TryParse<MailContentTypes>(Splitted[0].Replace("/", "_").Replace("+", "__"), out this._ContentType)) this._ContentType = MailContentTypes.text_plain; else foreach (var SubInformation in Splitted.Skip(1)) { if (SubInformation.ToLower().StartsWith("charset=")) { _CharSet = SubInformation.Substring("charset=".Length).Trim().ToLower(); if (_CharSet.StartsWith(@"""")) _CharSet = _CharSet.Remove(0, 1); if (_CharSet.EndsWith(@"""")) _CharSet = _CharSet.Substring(0, _CharSet.Length - 1); continue; } if (SubInformation.ToLower().StartsWith("boundary=")) { _MIMEBoundary = SubInformation.Substring("boundary=".Length).Trim(); if (_MIMEBoundary.StartsWith(@"""")) _MIMEBoundary = _MIMEBoundary.Remove(0, 1); if (_MIMEBoundary.EndsWith(@"""")) _MIMEBoundary = _MIMEBoundary.Substring(0, MIMEBoundary.Length - 1); continue; } } // Update text-version within the e-mail header if (_EMailHeader != null) _EMailHeader.SetEMailHeader("Content-Type", this.ToString()); }
/// <summary> /// Adds the given file as an attachment to the given e-mail. /// </summary> /// <typeparam name="T">The type of the e-mail builder.</typeparam> /// <param name="MailBuilder">An e-mail builder.</param> /// <param name="FileStream">The file attachment as a stream of bytes.</param> /// <param name="Filename">The name of the file to add.</param> /// <param name="ContentType">The content type of the file to add.</param> /// <param name="ContentLanguage">The content language of the file to add.</param> public static T AddAttachment <T>(this T MailBuilder, Stream FileStream, String Filename, MailContentTypes ContentType = MailContentTypes.text_plain, String ContentLanguage = null) where T : AbstractEMailBuilder { #region Initial checks if (MailBuilder == null) { throw new ArgumentNullException("The given e-mail builder must not be null!"); } if (FileStream == null) { throw new ArgumentNullException("The given FileStream must not be null!"); } if (Filename.IsNullOrEmpty()) { throw new ArgumentNullException("The given file name must not be null or empty!"); } #endregion return(MailBuilder.AddAttachment <T>( new EMailBodypart(ContentTypeBuilder: AMail => new MailContentType(AMail, ContentType) { CharSet = "utf-8" }, ContentTransferEncoding: "base64", ContentLanguage: ContentLanguage, Content: new String[] { Convert.ToBase64String("FileStream".ToUTF8Bytes()) }). SetEMailHeader("Content-Disposition", ContentDispositions.attachment.ToString() + "; filename=\"" + Filename + "\""))); }
/// <summary> /// Adds the given file as an attachment to the given e-mail. /// </summary> /// <typeparam name="T">The type of the e-mail builder.</typeparam> /// <param name="MailBuilder">An e-mail builder.</param> /// <param name="FileInfo">The file to add.</param> /// <param name="ContentType">The content type of the file to add.</param> /// <param name="ContentLanguage">The content language of the file to add.</param> public static T AddAttachment <T>(this AbstractEMailBuilder MailBuilder, FileInfo FileInfo, MailContentTypes ContentType = MailContentTypes.text_plain, String ContentLanguage = null) where T : AbstractEMailBuilder { #region Initial checks if (MailBuilder == null) { throw new ArgumentNullException("The given e-mail builder must not be null!"); } if (FileInfo == null) { throw new ArgumentNullException("The given file name must not be null or empty!"); } if (!FileInfo.Exists) { throw new ArgumentNullException("The given file does not exist!"); } #endregion return(MailBuilder.AddAttachment <T>( new EMailBodypart(ContentTypeBuilder: AMail => new MailContentType(AMail, ContentType) { CharSet = "utf-8" }, ContentTransferEncoding: "base64", ContentLanguage: ContentLanguage, Content: new String[] { Convert.ToBase64String(File.ReadAllBytes(FileInfo.FullName)) }). SetEMailHeader("Content-Disposition", ContentDispositions.attachment.ToString() + "; filename=\"" + FileInfo.Name + "\""))); }
/// <summary> /// Create a new e-mail content type by parsing the given string. /// </summary> /// <param name="ContentTypeString"></param> public MailContentType(AbstractEMail EMailHeader, String ContentTypeString) { this._EMailHeader = EMailHeader; this._Text = ContentTypeString; var Splitted = ContentTypeString. Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries). Select(v => v.Trim()). ToArray(); if (Splitted.Length == 0) { this._ContentType = MailContentTypes.text_plain; } else if (!Enum.TryParse <MailContentTypes>(Splitted[0].Replace("/", "_").Replace("+", "__"), out this._ContentType)) { this._ContentType = MailContentTypes.text_plain; } else { foreach (var SubInformation in Splitted.Skip(1)) { if (SubInformation.ToLower().StartsWith("charset=")) { _CharSet = SubInformation.Substring("charset=".Length).Trim().ToLower(); if (_CharSet.StartsWith(@"""")) { _CharSet = _CharSet.Remove(0, 1); } if (_CharSet.EndsWith(@"""")) { _CharSet = _CharSet.Substring(0, _CharSet.Length - 1); } continue; } if (SubInformation.ToLower().StartsWith("boundary=")) { _MIMEBoundary = SubInformation.Substring("boundary=".Length).Trim(); if (_MIMEBoundary.StartsWith(@"""")) { _MIMEBoundary = _MIMEBoundary.Remove(0, 1); } if (_MIMEBoundary.EndsWith(@"""")) { _MIMEBoundary = _MIMEBoundary.Substring(0, MIMEBoundary.Length - 1); } continue; } } } // Update text-version within the e-mail header if (_EMailHeader != null) { _EMailHeader.SetEMailHeader("Content-Type", this.ToString()); } }
/// <summary> /// Create a new e-mail content type. /// </summary> /// <param name="ContentType">The content type.</param> public MailContentType(AbstractEMail EMailHeader, MailContentTypes ContentType = MailContentTypes.unknown) { this._EMailHeader = EMailHeader; this._ContentType = ContentType; }