/// <summary> /// Gets the MimeMessage representation of the MailMergeMessage for a specific data item. /// </summary> /// <param name="dataItem"> /// The following types are accepted: /// Dictionary<string,object>, ExpandoObject, DataRow, any other class instances, anonymous types, and null. /// For class instances it's allowed to use the name of parameterless methods; use method names WITHOUT parentheses. /// </param> /// <returns>Returns a MailMessage ready to be sent by an SmtpClient.</returns> /// <exception cref="MailMergeMessageException">Throws a general MailMergeMessageException, which contains a list of exceptions giving more details.</exception> public MimeMessage GetMimeMessage(object dataItem = default(object)) { lock (_syncRoot) { // convert DataRow to Dictionary<string, object> if (dataItem is DataRow) { var row = (DataRow)dataItem; dataItem = row.Table.Columns.Cast <DataColumn>().ToDictionary(c => c.ColumnName, c => row[c]); } var mimeMessage = new MimeMessage(); AddSubjectToMailMessage(mimeMessage, dataItem); AddAddressesToMailMessage(mimeMessage, dataItem); AddAttributesToMailMessage(mimeMessage, dataItem); // must be added before subject and addresses BuildTextMessagePart(dataItem); BuildAttachmentPartsForMessage(dataItem); var exceptions = new List <Exception>(); if (mimeMessage.To.Count == 0 && mimeMessage.Cc.Count == 0 && mimeMessage.Bcc.Count == 0) { exceptions.Add(new AddressException("No recipients.", _badMailAddr, null)); } if (string.IsNullOrWhiteSpace(mimeMessage.From.ToString())) { exceptions.Add(new AddressException("No from address.", _badMailAddr, null)); } if (HtmlText.Length == 0 && PlainText.Length == 0 && Subject.Length == 0 && !FileAttachments.Any() && !InlineAttachments.Any() && !StringAttachments.Any() && !StreamAttachments.Any()) { exceptions.Add(new EmtpyContentException("Message is empty.", null)); } if (_badMailAddr.Count > 0) { exceptions.Add( new AddressException($"Bad mail address(es): {string.Join(", ", _badMailAddr.ToArray())}", _badMailAddr, null)); } if (_badInlineFiles.Count > 0) { exceptions.Add( new AttachmentException( $"Inline attachment(s) missing or not readable: {string.Join(", ", _badInlineFiles.ToArray())}", _badInlineFiles, null)); } if (_badAttachmentFiles.Count > 0) { exceptions.Add( new AttachmentException( $"File attachment(s) missing or not readable: {string.Join(", ", _badAttachmentFiles.ToArray())}", _badAttachmentFiles, null)); } if (_badVariableNames.Count > 0) { exceptions.Add( new VariableException( $"Variable(s) for placeholder(s) not found: {string.Join(", ", _badVariableNames.ToArray())}", _badVariableNames, null)); } // Finally throw general exception if (exceptions.Count > 0) { throw new MailMergeMessageException("Building of message failed with one or more exceptions.", exceptions, mimeMessage); } if (_attachmentParts.Any()) { var mixed = new Multipart("mixed"); if (_textMessagePart != null) { mixed.Add(_textMessagePart); } foreach (var att in _attachmentParts) { mixed.Add(att); } mimeMessage.Body = mixed; } if (mimeMessage.Body == null) { mimeMessage.Body = _textMessagePart ?? new TextPart("plain") { Text = string.Empty }; } return(mimeMessage); } }
/// <summary> /// Prepares the mail message part (plain text and/or HTML: /// Replacing placeholders with their values and setting correct encoding. /// </summary> private void BuildTextMessagePart(object dataIteam) { _badInlineFiles.Clear(); _textMessagePart = null; MultipartAlternative alternative = null; // create the plain text body part TextPart plainTextPart = null; if (!string.IsNullOrEmpty(PlainText)) { var plainText = SearchAndReplaceVars(PlainText, dataIteam); plainTextPart = (TextPart) new PlainBodyBuilder(plainText) { TextTransferEncoding = Config.TextTransferEncoding, CharacterEncoding = Config.CharacterEncoding }.GetBodyPart(); if (!string.IsNullOrEmpty(HtmlText)) { // there is plain text AND html text alternative = new MultipartAlternative { plainTextPart }; _textMessagePart = alternative; } else { // there is only a plain text part, which could even be null _textMessagePart = plainTextPart; } } if (!string.IsNullOrEmpty(HtmlText)) { // create the HTML text body part with any linked resources // replacing any placeholders in the text or files with variable values var htmlBody = new HtmlBodyBuilder(this, dataIteam) { DocBaseUri = Config.FileBaseDirectory, TextTransferEncoding = Config.TextTransferEncoding, BinaryTransferEncoding = Config.BinaryTransferEncoding, CharacterEncoding = Config.CharacterEncoding }; _inlineAttExternal.ForEach(ia => htmlBody.InlineAtt.Add(ia)); if (alternative != null) { alternative.Add(htmlBody.GetBodyPart()); _textMessagePart = alternative; } else { _textMessagePart = htmlBody.GetBodyPart(); } // get inline attachments and bad inline files AFTER htmlBody.GetBodyPart()! InlineAttachments = htmlBody.InlineAtt; // expose all resolved inline attachments in MailMergeMessage htmlBody.BadInlineFiles.ToList().ForEach(f => _badInlineFiles.Add(f)); } else { InlineAttachments.Clear(); _badInlineFiles.Clear(); } }