/// <summary> /// Creates attachment entity. /// </summary> /// <param name="file">File name with optional path.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null reference.</exception> public static MIME_Entity CreateAttachment(string file) { if (file == null) { throw new ArgumentNullException("file"); } MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetDataFromFile(file, MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(file); FileInfo fileInfo = new FileInfo(file); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(file); disposition.Param_Size = fileInfo.Length; disposition.Param_CreationDate = fileInfo.CreationTime; disposition.Param_ModificationDate = fileInfo.LastWriteTime; disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return(retVal); }
/// <summary> /// Creates attachment entity. /// </summary> /// <param name="stream">Attachment data stream. Data is read from stream current position.</param> /// <param name="fileName">File name.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>fileName</b> is null reference.</exception> public static MIME_Entity CreateAttachment(Stream stream, string fileName) { if (stream == null) { throw new ArgumentNullException("stream"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } long fileSize = stream.CanSeek ? (stream.Length - stream.Position) : -1; MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetData(stream, MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(fileName); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(fileName); disposition.Param_Size = fileSize; //disposition.Param_CreationDate = fileInfo.CreationTime; //disposition.Param_ModificationDate = fileInfo.LastWriteTime; //disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return(retVal); }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner, MIME_h_ContentType defaultContentType, SmartStream stream) { if (owner == null) { throw new ArgumentNullException("owner"); } if (defaultContentType == null) { throw new ArgumentNullException("defaultContentType"); } if (stream == null) { throw new ArgumentNullException("stream"); } MIME_b_Application retVal = null; if (owner.ContentType != null) { retVal = new MIME_b_Application(owner.ContentType.TypeWithSubtype); } else { retVal = new MIME_b_Application(defaultContentType.TypeWithSubtype); } Net_Utils.StreamCopy(stream, retVal.EncodedStream, 32000); return(retVal); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode) { // We have signer certificate, sign this entity. if (this.BodyParts.Count > 0 && m_pSignerCert != null) { // Remove old signature if there is any. if (this.BodyParts.Count > 1) { this.BodyParts.Remove(1); } // Store entity to tmp stream. MemoryStream tmpDataEntityStream = new MemoryStream(); this.BodyParts[0].ToStream(tmpDataEntityStream, null, null, false); // Compute PKCS #7 message. SignedCms signedCms = new SignedCms(new ContentInfo(tmpDataEntityStream.ToArray()), true); signedCms.ComputeSignature(new CmsSigner(m_pSignerCert)); byte[] pkcs7 = signedCms.Encode(); // Create PKCS 7 entity. MIME_Entity entity_application_pkcs7 = new MIME_Entity(); MIME_b_Application application_pkcs7 = new MIME_b_Application(MIME_MediaTypes.Application.x_pkcs7_signature); entity_application_pkcs7.Body = application_pkcs7; application_pkcs7.SetData(new MemoryStream(pkcs7), MIME_TransferEncodings.Base64); entity_application_pkcs7.ContentType.Param_Name = "smime.p7s"; entity_application_pkcs7.ContentDescription = "S/MIME Cryptographic Signature"; this.BodyParts.Add(entity_application_pkcs7); signedCms.Decode(application_pkcs7.Data); signedCms.CheckSignature(true); } base.ToStream(stream, headerWordEncoder, headerParmetersCharset, headerReencode); }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="mediaType">MIME media type. For example: text/plain.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>mediaType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner, string mediaType, SmartStream stream) { if (owner == null) { throw new ArgumentNullException("owner"); } if (mediaType == null) { throw new ArgumentNullException("mediaType"); } if (stream == null) { throw new ArgumentNullException("stream"); } MIME_b_Application retVal = new MIME_b_Application(mediaType); Net_Utils.StreamCopy(stream, retVal.EncodedStream, 32000); return(retVal); }
/// <summary> /// Creates attachment entity. /// </summary> /// <param name="file">File name with optional path.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null reference.</exception> public static MIME_Entity CreateAttachment(string file) { if(file == null){ throw new ArgumentNullException("file"); } MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetBodyDataFromFile(file,MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(file); FileInfo fileInfo = new FileInfo(file); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(file); disposition.Param_Size = fileInfo.Length; disposition.Param_CreationDate = fileInfo.CreationTime; disposition.Param_ModificationDate = fileInfo.LastWriteTime; disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return retVal; }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } MIME_b_Application retVal = null; if(owner.ContentType != null){ retVal = new MIME_b_Application(owner.ContentType.TypeWithSubtype); } else{ retVal = new MIME_b_Application(defaultContentType.TypeWithSubtype); } Net_Utils.StreamCopy(stream,retVal.EncodedStream,stream.LineBufferSize); return retVal; }
/// <summary> /// Generates message parsing failed message. /// </summary> /// <param name="message">Message stream.</param> /// <returns>Returns message parsing failed message.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>message</b> is null reference.</exception> public static Mail_Message GenerateBadMessage(Stream message) { if(message == null){ throw new ArgumentNullException("message"); } Mail_Message msg = new Mail_Message(); msg.MimeVersion = "1.0"; msg.MessageID = MIME_Utils.CreateMessageID(); msg.Date = DateTime.Now; msg.From = new Mail_t_MailboxList(); msg.From.Add(new Mail_t_Mailbox("system","system")); msg.To = new Mail_t_AddressList(); msg.To.Add(new Mail_t_Mailbox("system","system")); msg.Subject = "[BAD MESSAGE] Bad message, message parsing failed !"; //--- multipart/mixed ------------------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed); contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-','.'); MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed); msg.Body = multipartMixed; //--- text/plain --------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; text_plain.SetText(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,"NOTE: Bad message, message parsing failed.\r\n\r\nOriginal message attached as 'data.eml'\r\n"); multipartMixed.BodyParts.Add(entity_text_plain); //--- application/octet-stream -------------------------------------------------------------------------------------- MIME_Entity entity_application_octet_stream = new MIME_Entity(); entity_application_octet_stream.ContentDisposition = new MIME_h_ContentDisposition("attachment"); entity_application_octet_stream.ContentDisposition.Param_FileName = "data.eml"; MIME_b_Application application_octet_stream = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); entity_application_octet_stream.Body = application_octet_stream; application_octet_stream.SetData(message,"base64"); multipartMixed.BodyParts.Add(entity_application_octet_stream); return msg; }
/// <summary> /// Creates attachment entity. /// </summary> /// <param name="stream">Attachment data stream. Data is read from stream current position.</param> /// <param name="fileName">File name.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>fileName</b> is null reference.</exception> public static MIME_Entity CreateAttachment(Stream stream,string fileName) { if(stream == null){ throw new ArgumentNullException("stream"); } if(fileName == null){ throw new ArgumentNullException("fileName"); } long fileSize = stream.CanSeek ? (stream.Length - stream.Position) : -1; MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetData(stream,MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(fileName); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(fileName); disposition.Param_Size = fileSize; //disposition.Param_CreationDate = fileInfo.CreationTime; //disposition.Param_ModificationDate = fileInfo.LastWriteTime; //disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return retVal; }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { // We have signer certificate, sign this entity. if(this.BodyParts.Count > 0 && m_pSignerCert != null){ // Remove old signature if there is any. if(this.BodyParts.Count > 1){ this.BodyParts.Remove(1); } // Store entity to tmp stream. MemoryStream tmpDataEntityStream = new MemoryStream(); this.BodyParts[0].ToStream(tmpDataEntityStream,null,null,false); // Compute PKCS #7 message. SignedCms signedCms = new SignedCms(new ContentInfo(tmpDataEntityStream.ToArray()),true); signedCms.ComputeSignature(new CmsSigner(m_pSignerCert)); byte[] pkcs7 = signedCms.Encode(); // Create PKCS 7 entity. MIME_Entity entity_application_pkcs7 = new MIME_Entity(); MIME_b_Application application_pkcs7 = new MIME_b_Application(MIME_MediaTypes.Application.x_pkcs7_signature); entity_application_pkcs7.Body = application_pkcs7; application_pkcs7.SetData(new MemoryStream(pkcs7),MIME_TransferEncodings.Base64); entity_application_pkcs7.ContentType.Param_Name = "smime.p7s"; entity_application_pkcs7.ContentDescription = "S/MIME Cryptographic Signature"; this.BodyParts.Add(entity_application_pkcs7); signedCms.Decode(application_pkcs7.Data); signedCms.CheckSignature(true); } base.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); }
public void Send(ChannelMessage message) { var creds = CredentialsProvider.GetCredentials().ToNetworkCredential(); Mail_Message msg = new Mail_Message(); msg.MimeVersion = "1.0"; msg.MessageID = MIME_Utils.CreateMessageID(); msg.Subject = message.Context; msg.From = message.From.ToMailBoxList(); msg.ReplyTo = message.ReturnTo == null ? message.From.ToAddressList() : message.ReturnTo.ToAddressList(); if (String.IsNullOrEmpty(message.InReplyTo) == false) msg.InReplyTo = message.InReplyTo; msg.To = new Mail_t_AddressList(); foreach (var address in message.To) msg.To.Add(address.ToMailBox()); msg.Cc = new Mail_t_AddressList(); foreach (var address in message.CC) msg.Cc.Add(address.ToMailBox()); //--- multipart/mixed ------------------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed); contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed); msg.Body = multipartMixed; //--- multipart/alternative ----------------------------------------------------------------------------------------- MIME_Entity entity_multipartAlternative = new MIME_Entity(); MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative); contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative); entity_multipartAlternative.Body = multipartAlternative; multipartMixed.BodyParts.Add(entity_multipartAlternative); //--- text/plain ---------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; // Add text body if there is any if (message.BodyText != null && message.BodyText.Length > 0) { var bodyText = message.BodyText.ReadString(); // Make sure there is a newline at the end of our text, otherwise it will screw up // our multipart data format if (!bodyText.EndsWith(Environment.NewLine)) bodyText = bodyText + Environment.NewLine; text_plain.SetText(MIME_TransferEncodings.SevenBit, Encoding.UTF8, bodyText); } multipartAlternative.BodyParts.Add(entity_text_plain); //--- text/html ------------------------------------------------------------------------------------------------------ MIME_Entity entity_text_html = new MIME_Entity(); MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html); entity_text_html.Body = text_html; if (message.BodyHtml != null && message.BodyHtml.Length > 0) { var bodyHtml = message.BodyHtml.ReadString(); // Make sure there is a newline at the end of our text, otherwise it will screw up // our multipart data format if (!bodyHtml.EndsWith(Environment.NewLine)) bodyHtml = bodyHtml + Environment.NewLine; text_html.SetText(MIME_TransferEncodings.SevenBit, Encoding.UTF8, bodyHtml); } multipartAlternative.BodyParts.Add(entity_text_html); foreach (var channelAttachment in message.Attachments) { MIME_b_Application attachmentBody = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); MIME_Entity attachment = new MIME_Entity(); attachment.Body = attachmentBody; // Has to happen before the following lines of code multipartMixed.BodyParts.Add(attachment); attachment.ContentType = new MIME_h_ContentType(MimeHelper.GetMimeType(channelAttachment.Filename)); attachment.ContentType.Param_Name = channelAttachment.Filename; MIME_h_ContentDisposition contentDisposition = new MIME_h_ContentDisposition(DispositionTypeNames.Attachment); contentDisposition.Param_FileName = channelAttachment.Filename; attachment.ContentDisposition = contentDisposition; attachment.ContentTransferEncoding = TransferEncoding.Base64.ToString(); attachmentBody.SetData(channelAttachment.ContentStream, MIME_TransferEncodings.Base64); } // Inject headers if (!String.IsNullOrEmpty(message.MessageIdentifier)) msg.Header.Add(new MIME_h_Unstructured("x-i2mp-messageid", message.MessageIdentifier)); //if (!String.IsNullOrEmpty(message.Metadata.i2mpFlow)) // msg.Header.Add(new MIME_h_Unstructured("x-i2mp-flow", message.Metadata.i2mpFlow)); //if (!String.IsNullOrEmpty(message.Metadata.i2mpReference)) // mailMessage.Headers.Add("X-i2mp-ref", message.Metadata.i2mpReference); //if (!String.IsNullOrEmpty(message.Metadata.i2mpSequence)) // mailMessage.Headers.Add("X-i2mp-seq", message.Metadata.i2mpSequence); //if (!String.IsNullOrEmpty(message.Metadata.i2mpRelation)) // mailMessage.Headers.Add("X-i2mp-rel", message.Metadata.i2mpRelation); //if (!String.IsNullOrEmpty(message.Metadata.i2mpRelationId)) // mailMessage.Headers.Add("X-i2mp-rel-id", message.Metadata.i2mpRelationId); // Send message try { SMTP_Client client = new SMTP_Client(); if ("/Settings/Channels/LoggerEnabled".AsKey(false)) client.Logger = new LumiSoft.Net.Log.Logger(); // todo push this logic into the smtp client implementation itself if (Hostname == "smtp.live.com") { // Hack for hotmail, first do a connect with no secured channel, // then a STARTTLS client.Connect(Hostname, Port, false); client.StartTLS(); } else { client.Connect(Hostname, Port, IsSecured); } client.Authenticate(creds.UserName, creds.Password); using (MemoryStream ms = new MemoryStream()) { client.MailFrom(msg.From[0].Address, -1); msg.ToStream(ms, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8); // Reset stream ms.Seek(0, SeekOrigin.Begin); foreach (var address in message.To) client.RcptTo(address.Address); foreach (var address in message.CC) client.RcptTo(address.Address); foreach (var address in message.BCC) client.RcptTo(address.Address); try { client.SendMessage(ms); } finally { client.Dispose(); } } } catch(SmtpFailedRecipientsException e) { throw new ChannelFunctionalException(e.Message, e) { DoNotRetry = true }; } catch (SmtpException e) { throw new ChannelFunctionalException(e.Message, e); } catch (Exception e) { throw new ChannelFunctionalException(e.Message, e); } }