public override string ToString() { if (((this.disposition == null) || this.isChanged) || (this.parameters != null)) { StringBuilder builder = new StringBuilder(); builder.Append(this.dispositionType); foreach (string str in this.Parameters.Keys) { builder.Append("; "); builder.Append(str); builder.Append('='); MailBnfHelper.GetTokenOrQuotedString(this.parameters[str], builder); } this.disposition = builder.ToString(); this.isChanged = false; this.isPersisted = false; } return(this.disposition); }
internal void PrepareHeaders(bool sendEnvelope, bool allowUnicode) { string headerName; if (_headersEncoding == null) { _headersEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet); } //ContentType is written directly to the stream so remove potential user duplicate Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType) !); Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0"; // add sender to headers first so that it is written first to allow the IIS smtp svc to // send MAIL FROM with the sender if both sender and from are present headerName = MailHeaderInfo.GetString(MailHeaderID.Sender) !; if (Sender != null) { Headers.InternalAdd(headerName, Sender.Encode(headerName.Length, allowUnicode)); } else { Headers.Remove(headerName); } headerName = MailHeaderInfo.GetString(MailHeaderID.From) !; Headers.InternalAdd(headerName, From !.Encode(headerName.Length, allowUnicode)); headerName = MailHeaderInfo.GetString(MailHeaderID.To) !; if (To.Count > 0) { Headers.InternalAdd(headerName, To.Encode(headerName.Length, allowUnicode)); } else { Headers.Remove(headerName); } headerName = MailHeaderInfo.GetString(MailHeaderID.Cc) !; if (CC.Count > 0) { Headers.InternalAdd(headerName, CC.Encode(headerName.Length, allowUnicode)); } else { Headers.Remove(headerName); } headerName = MailHeaderInfo.GetString(MailHeaderID.ReplyTo) !; if (ReplyTo != null) { Headers.InternalAdd(headerName, ReplyTo.Encode(headerName.Length, allowUnicode)); } else if (ReplyToList.Count > 0) { Headers.InternalAdd(headerName, ReplyToList.Encode(headerName.Length, allowUnicode)); } else { Headers.Remove(headerName); } Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc) !); if (_priority == MailPriority.High) { Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)] = "1"; Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)] = "urgent"; Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high"; } else if (_priority == MailPriority.Low) { Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)] = "5"; Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)] = "non-urgent"; Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low"; } //if the priority was never set, allow the app to set the headers directly. else if (((int)_priority) != -1) { Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority) !); Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority) !); Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance) !); } Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date) !, MailBnfHelper.GetDateTimeString(DateTime.Now, null) !); headerName = MailHeaderInfo.GetString(MailHeaderID.Subject) !; if (!string.IsNullOrEmpty(_subject)) { if (allowUnicode) { Headers.InternalAdd(headerName, _subject); } else { Headers.InternalAdd(headerName, MimeBasePart.EncodeHeaderValue(_subject, _subjectEncoding, MimeBasePart.ShouldUseBase64Encoding(_subjectEncoding), headerName.Length)); } } else { Headers.Remove(headerName); } EncodeHeaders(_headers !, allowUnicode); }
internal void SetContentFromString(string content, Encoding encoding, string mediaType) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (_part.Stream != null) { _part.Stream.Close(); } if (mediaType == null || mediaType == string.Empty) { mediaType = MediaTypeNames.Text.Plain; } //validate the mediaType int offset = 0; try { string value = MailBnfHelper.ReadToken(mediaType, ref offset, null); if (value.Length == 0 || offset >= mediaType.Length || mediaType[offset++] != '/') { throw new ArgumentException(Strings.MediaTypeInvalid, nameof(mediaType)); } value = MailBnfHelper.ReadToken(mediaType, ref offset, null); if (value.Length == 0 || offset < mediaType.Length) { throw new ArgumentException(Strings.MediaTypeInvalid, nameof(mediaType)); } } catch (FormatException) { throw new ArgumentException(Strings.MediaTypeInvalid, nameof(mediaType)); } ContentType contentType = new ContentType(mediaType); if (encoding == null) { if (MimeBasePart.IsAscii(content, false)) { encoding = Encoding.ASCII; } else { encoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet); } } contentType.CharSet = encoding.BodyName; byte[] buffer = encoding.GetBytes(content); _part.SetContent(new MemoryStream(buffer), contentType); if (MimeBasePart.ShouldUseBase64Encoding(encoding)) { _part.TransferEncoding = TransferEncoding.Base64; } else { _part.TransferEncoding = TransferEncoding.QuotedPrintable; } }
// Parses the local-part section of an address. The local-part may be in dot-atom format or // quoted-string format. e.g. <user.name@domain> or <"user name"@domain> // We do not support the obsolete formats of user."name"@domain, "user".name@domain, or "user"."name"@domain. // // Preconditions: // - data[index + 1] is the '@' symbol // // Postconditions: // - data[index] should refer to the '<', if any, otherwise the next non-CFWS char. // - index == -1 if the beginning of the data string has been reached. // - returns the parsed local-part, including any bounding quotes around quoted-strings // // Throws a FormatException or false is returned: // - For invalid un-escaped chars, including Unicode // - If the final value of data[index] is not a valid character to precede the local-part private static bool TryParseLocalPart(string data, ref int index, bool expectAngleBracket, bool expectMultipleAddresses, [NotNullWhen(true)] out string?localPart, bool throwExceptionIfFail) { // Skip comments and whitespace if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail)) { localPart = default; return(false); } // Mark the start of the local-part int startingIndex = index; // Is the local-part component in quoted-string format or dot-atom format? if (data[index] == MailBnfHelper.Quote) { if (!QuotedStringFormatReader.TryReadReverseQuoted(data, index, true, out index, throwExceptionIfFail)) { localPart = default; return(false); } } else { if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail)) { localPart = default; return(false); } // Check that the local-part is properly separated from the next component. It may be separated by a // comment, whitespace, an expected angle bracket, a quote for the display-name, or an expected comma // before the next address. if (index >= 0 && !( MailBnfHelper.IsAllowedWhiteSpace(data[index]) || // < local@domain > data[index] == MailBnfHelper.EndComment || // <(comment)local@domain> (expectAngleBracket && data[index] == MailBnfHelper.StartAngleBracket) || // <local@domain> (expectMultipleAddresses && data[index] == MailBnfHelper.Comma) // local@dom,local@dom // Note: The following condition is more lax than the RFC. This is done so we could support // a common invalid formats as shown below. || data[index] == MailBnfHelper.Quote // "display"local@domain ) ) { if (throwExceptionIfFail) { throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index])); } else { localPart = default; return(false); } } } localPart = data.Substring(index + 1, startingIndex - index); if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail)) { return(false); } if (!TryNormalizeOrThrow(localPart, out localPart, throwExceptionIfFail)) { return(false); } return(true); }
internal void PrepareHeaders(bool sendEnvelope) { if (this.headersEncoding == null) { this.headersEncoding = Encoding.GetEncoding("utf-8"); } this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType)); this.Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0"; if (this.Sender != null) { this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Sender), this.Sender.Encode(MailHeaderInfo.GetString(MailHeaderID.Sender).ToString().Length)); } else { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Sender)); } this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.From), this.From.Encode(MailHeaderInfo.GetString(MailHeaderID.From).ToString().Length)); if (this.To.Count > 0) { string str = this.To.Encode(MailHeaderInfo.GetString(MailHeaderID.To).Length); this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.To), str); } else { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.To)); } if (this.CC.Count > 0) { this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Cc), this.CC.Encode(MailHeaderInfo.GetString(MailHeaderID.Cc).Length)); } else { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Cc)); } if (this.ReplyTo != null) { this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyTo.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length)); } else if (this.ReplyToList.Count > 0) { this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyToList.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length)); } else { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ReplyTo)); } this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc)); if (this.priority == MailPriority.High) { this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)] = "1"; this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)] = "urgent"; this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high"; } else if (this.priority == MailPriority.Low) { this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)] = "5"; this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)] = "non-urgent"; this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low"; } else if (this.priority != ~MailPriority.Normal) { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority)); this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority)); this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance)); } this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date), MailBnfHelper.GetDateTimeString(DateTime.Now, null)); if (!string.IsNullOrEmpty(this.subject)) { this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Subject), MimeBasePart.EncodeHeaderValue(this.subject, this.subjectEncoding, MimeBasePart.ShouldUseBase64Encoding(this.subjectEncoding), MailHeaderID.Subject.ToString().Length)); } else { this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Subject)); } this.EncodeHeaders(this.headers); }
private void ParseValue() { int offset = 0; Exception exception = null; this.parameters = new StringDictionary(); try { this.mediaType = MailBnfHelper.ReadToken(this.type, ref offset, null); if (((this.mediaType == null) || (this.mediaType.Length == 0)) || ((offset >= this.type.Length) || (this.type[offset++] != '/'))) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); } if (exception == null) { this.subType = MailBnfHelper.ReadToken(this.type, ref offset, null); if ((this.subType == null) || (this.subType.Length == 0)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); } } if (exception == null) { while (MailBnfHelper.SkipCFWS(this.type, ref offset)) { string str2; if (this.type[offset++] != ';') { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (!MailBnfHelper.SkipCFWS(this.type, ref offset)) { break; } string key = MailBnfHelper.ReadParameterAttribute(this.type, ref offset, null); if ((key == null) || (key.Length == 0)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if ((offset >= this.type.Length) || (this.type[offset++] != '=')) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (!MailBnfHelper.SkipCFWS(this.type, ref offset)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (this.type[offset] == '"') { str2 = MailBnfHelper.ReadQuotedString(this.type, ref offset, null); } else { str2 = MailBnfHelper.ReadToken(this.type, ref offset, null); } if (str2 == null) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } this.parameters.Add(key, str2); } } } catch (FormatException) { throw new FormatException(SR.GetString("ContentTypeInvalid")); } if (exception != null) { throw new FormatException(SR.GetString("ContentTypeInvalid")); } }