/// <summary> /// Encodes one email address according to RFC2822 to use in the header /// fields of the message and in the collection encoding /// /// </summary> /// <param name="address">An email address to encode</param><param name="encoding">>A character encoding to use</param><param name="firstLineLength">The length of the first line of the encoded string</param> /// <returns> /// The encoded email address /// </returns> /// <exception cref="T:System.ArgumentNullException">Neither parameter can be null</exception><exception cref="T:System.ArgumentOutOfRangeException">firstLineLength parameter cannot be negative</exception> private static string EncodeEmailAddress(EmailAddress address, Encoding encoding, int firstLineLength) { if (address == null) { throw new ArgumentNullException("address"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (firstLineLength < 0) { throw new ArgumentOutOfRangeException("firstLineLength"); } if (address.DisplayName == null) { return(string.Format("<{0}>", (object)address.Address)); } string str1 = string.Format("<{0}>", (object)address.Address); string str2 = string.Format("{0}", (object)MailMessageRFCEncoder.MimeEncodeHeader(address.DisplayName, 76, firstLineLength, encoding)); if (str1.Length + str2.Length + 1 > 76) { return(str2 + "\r\n " + str1); } else { return(str2 + " " + str1); } }
/// <summary> /// Encodes the collection of the email addresses according to RFC2822 /// for use in message headers like "To:", "CC:", etc. /// /// </summary> /// <param name="collection">A collection of the email addresses to encode</param><param name="encoding">A character encoding to use</param><param name="firstLineLength">The length of the first line of the encoded string</param> /// <returns> /// The encoded string /// </returns> /// <exception cref="T:System.ArgumentNullException">Neither parameter can be null</exception><exception cref="T:System.ArgumentOutOfRangeException">firstLineLength parameter cannot be negative</exception> private static string EncodeEmailAddressCollection(IEnumerable <EmailAddress> collection, Encoding encoding, int firstLineLength) { if (collection == null) { throw new ArgumentNullException("collection"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (firstLineLength < 0) { throw new ArgumentOutOfRangeException("firstLineLength"); } StringBuilder stringBuilder = new StringBuilder(""); foreach (EmailAddress address in collection) { stringBuilder.Append(MailMessageRFCEncoder.EncodeEmailAddress(address, encoding, firstLineLength)); stringBuilder.Append(", "); firstLineLength = stringBuilder.Length % 76; } stringBuilder.Remove(stringBuilder.Length - ", ".Length, ", ".Length); return(((object)stringBuilder).ToString()); }
/// <summary> /// Returns encoded message as iterator of strings /// /// </summary> /// <param name="message">Message to encode</param> /// <returns> /// Iterator through encoded strings, that are ready to sent to smtp server /// </returns> /// <exception cref="T:System.ArgumentNullException">The message argument cannot be null</exception> public static IEnumerable <string> GetEncoded(SmtpMessage message) { if (message == null) { throw new ArgumentNullException("message"); } else { return(MailMessageRFCEncoder.GetEncoded(message, message.TextEncoding)); } }
/// <summary> /// Encodes message header contained non-ASCII characters using MIME standart /// /// </summary> /// <param name="text">Header text to encode</param><param name="maxLineLength">A maximum line length in encoded header</param><param name="firstLineLength">A length of the first line in the encoded header</param><param name="encoding">Encoding to use</param> /// <returns> /// Encoded header as string /// </returns> /// <exception cref="T:System.ArgumentNullException">Neither argument can be null</exception><exception cref="T:System.ArgumentOutOfRangeException">Both maxLineLength and firstLineLength cannot be negative</exception><exception cref="T:System.ArgumentException">firstLineLength cannot be greater than maxLineLength</exception> public static string MimeEncodeHeader(string text, int maxLineLength, int firstLineLength, Encoding encoding) { if (text == null) { throw new ArgumentNullException("text"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (firstLineLength < 0) { throw new ArgumentOutOfRangeException("firstLineLength"); } if (maxLineLength <= 0) { throw new ArgumentOutOfRangeException("maxLineLength"); } if (maxLineLength < firstLineLength) { throw new ArgumentException("firstLineLength must be less than maxLineLength"); } string str1; if (MailMessageRFCEncoder.IsAsciiCharOnly(text)) { str1 = text; } else { string str2 = encoding.WebName.ToUpper(); string str3 = Convert.ToBase64String(encoding.GetBytes(text)); int num = str2.Length + "=??B?=".Length; int maxBase64LineLength = (maxLineLength - num) / 4 * 4; if (str3.Length > firstLineLength - num) { int firstLineLength1 = (firstLineLength - num) / 4 * 4; List <string> list = MailMessageRFCEncoder.MimeBase64Encode(text, maxBase64LineLength, firstLineLength1, encoding); string[] strArray = new string[list.Count]; for (int index = 0; index < list.Count; ++index) { strArray[index] = string.Format("=?{0}?{1}?{2}?=", (object)str2, (object)"B", (object)list[index]); } str1 = string.Join("\r\n ", strArray); } else { str1 = string.Format("=?{0}?{1}?{2}?=", (object)str2, (object)"B", (object)str3); } } return(str1); }
protected virtual IList <SendResult> PerformSendMailInteraction(IEnumerable <SmtpMessage> messages, IConnection connection) { if (messages == null) { throw new ArgumentNullException("messages"); } if (connection.State != EConnectionState.Connected) { throw new InvalidOperationException("Connection should be opened"); } IList <SendResult> list = new List <SendResult>(); if (this.Connected != null) { this.Connected(this); } SmtpResponse lastResponse = new SmtpResponse(connection); if (lastResponse.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.StrangeServerResponse, lastResponse)); return(list); } if (this.DoAction(new EHLOCommand(), connection).Type != ESmtpResponseType.PositiveCompletion) { SmtpResponse response3 = this.DoAction(new HELOCommand(), connection); if (response3.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.StrangeServerResponse, response3)); return(list); } } if (this._configurationProvider.SSLInteractionType == EInteractionType.StartTLS) { SmtpResponse response4 = this.DoAction(new StartTlsCommand(), connection); if (response4.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.SslWasNotStarted, response4)); return(list); } connection.SwitchToSslChannel(); } SmtpResponse response2 = this.DoAction(new EHLOCommand(), connection); if (response2.Type != ESmtpResponseType.PositiveCompletion) { SmtpResponse response5 = this.DoAction(new HELOCommand(), connection); if (response5.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.StrangeServerResponse, response5)); return(list); } } if (this._configurationProvider.AuthenticationType != EAuthenticationType.None) { ISmtpAction action = GetAuthenticationCommand(this.Username, this.Password, this._configurationProvider.AuthenticationType, response2.Lines); if (action == null) { throw new InvalidOperationException("Server doesn't support existed authentication mechanisms or doesn't require any authentication"); } SmtpResponse response6 = this.DoAction(action, connection) as AuthCommandResponse; if (response6.Type != ESmtpResponseType.PositiveCompletion) { SendResult item = new SendResult(ESendResult.AuthenticationFailed, response6); try { this.DoAction(new QUITCommand(), connection); } catch { } list.Add(item); return(list); } } if (this.Authenticated != null) { this.Authenticated(this); } List <EmailAddress> invalidEmails = new List <EmailAddress>(); foreach (SmtpMessage message in messages) { SmtpResponse response7 = this.DoAction(new MAILCommand(message.From), connection); if (response7.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.FromAddressFailed, response7)); } else { foreach (EmailAddress address in message.To) { if (this.DoAction(new RCPTCommand(address), connection).Type != ESmtpResponseType.PositiveCompletion) { invalidEmails.Add(address); } } foreach (EmailAddress address2 in message.CarbonCopies) { if (this.DoAction(new RCPTCommand(address2), connection).Type != ESmtpResponseType.PositiveCompletion) { invalidEmails.Add(address2); } } foreach (EmailAddress address3 in message.BlindedCarbonCopies) { if (this.DoAction(new RCPTCommand(address3), connection).Type != ESmtpResponseType.PositiveCompletion) { invalidEmails.Add(address3); } } IEnumerable <string> encoded = MailMessageRFCEncoder.GetEncoded(message); SmtpResponse response11 = this.DoAction(new DATACommand(encoded), connection); if (response11.Type != ESmtpResponseType.PositiveCompletion) { list.Add(new SendResult(ESendResult.DataSendingFailed, response11, invalidEmails)); continue; } if (this.MessageSent != null) { this.MessageSent(this); } if (invalidEmails.Count == 0) { list.Add(new SendResult(ESendResult.Ok, response11)); continue; } list.Add(new SendResult(ESendResult.OkWithInvalidEmails, response11, invalidEmails)); } } try { this.DoAction(new QUITCommand(), connection); } catch { } if (this.Completed != null) { this.Completed(this); } return(list); }
/// <summary> /// Returns encoded message as iterator of strings /// /// </summary> /// <param name="message">Message to encode</param><param name="encoding">Encoding of text part to use</param> /// <returns> /// Iterator through encoded strings, that are ready to sent to smtp server /// </returns> /// <exception cref="T:System.ArgumentNullException">Neither argument can be null</exception><exception cref="T:System.InvalidOperationException">HTML content type is specified, but PlainText field is not initialized</exception><exception cref="T:System.NotSupportedException">Only PlainText and HTML content type is supported</exception> public static IEnumerable <string> GetEncoded(SmtpMessage message, Encoding encoding) { // ISSUE: fault handler if (message == null) { throw new ArgumentNullException("message"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } yield return("MIME-Version: 1.0"); yield return(string.Format("From:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddress(message.From, encoding, 76 - "From:".Length))); if (message.Sender != null) { yield return(string.Format("Sender:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddress(message.Sender, encoding, 76 - "Sender:".Length))); } if (message.ReplyTo.Count > 0) { yield return(string.Format("Reply-To:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.ReplyTo, encoding, 76 - "Reply-To:".Length))); } if (message.To.Count > 0) { yield return(string.Format("To:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.To, encoding, 76 - "To:".Length))); } if (message.CarbonCopies.Count > 0) { yield return(string.Format("Cc:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.CarbonCopies, encoding, 76 - "Cc:".Length))); } yield return(string.Format("Subject:{0}", (object)MailMessageRFCEncoder.MimeEncodeHeader(message.Subject, 76, 76 - "Subject:".Length, encoding))); if (message.Date != DateTime.MinValue) { yield return(string.Format("Date:{0}", (object)message.Date.ToUniversalTime().ToString("r"))); } else { yield return(string.Format("Date:{0}", (object)DateTime.UtcNow.ToUniversalTime().ToString("r"))); } if (!string.IsNullOrEmpty(message.InReplyTo)) { yield return(string.Format("In-Reply-To:{0}", (object)message.InReplyTo)); } if (!string.IsNullOrEmpty(message.References)) { yield return(string.Format("References:{0}", (object)message.References)); } if (message.ResentDate != DateTime.MinValue) { yield return(string.Format("Resent-Date:{0}", (object)message.ResentDate.ToUniversalTime().ToString("r"))); } if (message.ResentFrom != null) { yield return(string.Format("Resent-From:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddress(message.ResentFrom, encoding, 76 - "Resent-From:".Length))); } if (message.ResentSender != null) { yield return(string.Format("Resent-Sender:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddress(message.ResentSender, encoding, 76 - "Resent-Sender:".Length))); } if (message.ResentTo != null && message.ResentTo.Count > 0) { yield return(string.Format("Resent-To:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.ResentTo, encoding, 76 - "Resent-To:".Length))); } if (message.ResentCarbonCopies != null && message.ResentCarbonCopies.Count > 0) { yield return(string.Format("Resent-Cc:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.ResentCarbonCopies, encoding, 76 - "Resent-Cc:".Length))); } if (message.ResentBlindedCarbonCopies != null && message.ResentBlindedCarbonCopies.Count > 0) { yield return(string.Format("Resent-Bcc:{0}", (object)MailMessageRFCEncoder.EncodeEmailAddressCollection((IEnumerable <EmailAddress>)message.ResentBlindedCarbonCopies, encoding, 76 - "Resent-Bcc:".Length))); } if (!string.IsNullOrEmpty(message.ResentMessageID)) { yield return(string.Format("Resent-Message-ID:{0}", (object)message.ResentMessageID)); } if (!string.IsNullOrEmpty(message.Comments)) { yield return(string.Format("Comments:{0}", (object)MailMessageRFCEncoder.MimeEncodeHeader(message.Comments, 76, 76 - "Comments:".Length, encoding))); } if (message.Keywords != null && message.Keywords.Count > 0) { yield return(string.Format("Keywords:{0}", (object)MailMessageRFCEncoder.MimeEncodeHeader(MailMessageRFCEncoder.StringJoin((IEnumerable <string>)message.Keywords, ", "), 76, 76 - "Keywords:".Length, encoding))); } IEnumerator <KeyValuePair <string, string> > enumerator1 = message.ExtraHeaders.GetEnumerator(); while (enumerator1.MoveNext()) { KeyValuePair <string, string> pair = enumerator1.Current; yield return(string.Format("{0}: {1}", (object)pair.Key, (object)pair.Value)); } if (enumerator1 != null) { enumerator1.Dispose(); } if (message.Attachments.Count > 0) { Guid boundaryId = Guid.NewGuid(); yield return(string.Format("Content-Type: multipart/mixed; boundary=\"{0}\"", (object)boundaryId)); yield return(""); yield return(string.Format("--{0}", (object)boundaryId)); IEnumerator <string> enumerator2 = MailMessageRFCEncoder.EncodeMessageText((MailMessage)message, encoding).GetEnumerator(); while (enumerator2.MoveNext()) { string str = enumerator2.Current; yield return(str); } if (enumerator2 != null) { enumerator2.Dispose(); } IEnumerator <Attachment> enumerator3 = message.Attachments.GetEnumerator(); while (enumerator3.MoveNext()) { Attachment attachment = enumerator3.Current; yield return(""); yield return(string.Format("--{0}", (object)boundaryId)); yield return(string.Format("Content-Type: {0}; name=\"{1}\"", (object)(attachment.ContentType.Type + "/" + attachment.ContentType.SubType), (object)attachment.TransferFilename)); yield return(string.Format("Content-Disposition: attachment; filename=\"{0}\"", (object)attachment.TransferFilename)); yield return("Content-Transfer-Encoding: base64"); yield return(""); IEnumerator <string> enumerator4 = MailMessageRFCEncoder.Base64EncodeAttachment(attachment).GetEnumerator(); while (enumerator4.MoveNext()) { string line = enumerator4.Current; yield return(line); } if (enumerator4 != null) { enumerator4.Dispose(); } } if (enumerator3 != null) { enumerator3.Dispose(); } yield return(""); yield return(string.Format("--{0}--", (object)boundaryId.ToString())); } else { IEnumerator <string> enumerator2 = MailMessageRFCEncoder.EncodeMessageText((MailMessage)message, encoding).GetEnumerator(); while (enumerator2.MoveNext()) { string str = enumerator2.Current; yield return(str); } if (enumerator2 != null) { enumerator2.Dispose(); } } }