private byte[] GetBytes(string text, string encoding) { if (encoding == "base64") { return(Convert.FromBase64String(text)); } if (encoding == "quoted-printable") { var decoder = new QuotedPrintable(); var iso = Encoding.GetEncoding("ISO-8859-1"); return(Encoding.UTF8.GetBytes(iso.GetString(decoder.Decode(text)))); } return(Encoding.UTF8.GetBytes(text)); }
private void OnHandleDataCommand(IncommingMail context, string commandLine) { // parse header data here if (commandLine == string.Empty) { context.State = SmtpState.Body; return; } if (commandLine.StartsWith("Subject:", StringComparison.InvariantCultureIgnoreCase)) { context.Message.Subject = commandLine.Substring(8).Trim(); if (context.Message.Subject.StartsWith("=") && context.Message.Subject.EndsWith("=")) { var decoder = new QuotedPrintable(); var iso = Encoding.GetEncoding("ISO-8859-1"); context.Message.Subject = iso.GetString(decoder.Decode(context.Message.Subject)); } context.RecentLine = commandLine; return; } if (commandLine.StartsWith("Content-Type:", StringComparison.InvariantCultureIgnoreCase)) { var contentType = commandLine.Substring(13).Trim(); if (context.IsMultipart) { context.PartContentType = contentType; if (contentType.Contains("name=")) { var name = contentType.IndexOf("name=", StringComparison.InvariantCultureIgnoreCase); context.PartName = contentType.Substring(name + 5).Trim(); } if (string.IsNullOrEmpty(context.PartName) && contentType.Contains("file=")) { var file = contentType.IndexOf("file=", StringComparison.InvariantCultureIgnoreCase); context.PartName = contentType.Substring(file + 5).Trim(); } if (string.IsNullOrEmpty(context.PartName) && contentType.Contains("filename=")) { var filename = context.PartDisposition.IndexOf("filename=", StringComparison.InvariantCultureIgnoreCase); context.PartName = context.PartDisposition.Substring(filename + 9).Trim(); } } else { context.ContentType = contentType; if (contentType.Contains("multipart") && contentType.Contains("boundary=")) { var boundary = contentType.IndexOf("boundary=", StringComparison.InvariantCultureIgnoreCase); context.Boundary = contentType.Substring(boundary + 9).Trim(); } } if (context.IsMultipart && commandLine.Contains("boundary=")) { var boundary = commandLine.IndexOf("boundary=", StringComparison.InvariantCultureIgnoreCase); context.Boundary = commandLine.Substring(boundary + 9).Trim(); } context.RecentLine = commandLine; return; } if (commandLine.StartsWith("Content-Disposition:", StringComparison.InvariantCultureIgnoreCase)) { var disposition = commandLine.Substring(20).Trim(); context.PartDisposition = disposition; context.RecentLine = commandLine; return; } if (commandLine.StartsWith("Content-Transfer-Encoding:", StringComparison.InvariantCultureIgnoreCase)) { var encoding = commandLine.Substring(26).Trim(); if (context.IsMultipart) { context.PartContentEncoding = encoding; } else { context.ContentEncoding = encoding; } context.RecentLine = commandLine; } }