Ejemplo n.º 1
0
 public List <string> GetAttachFiles()
 {
     if (FileAttachments.IsNull() || !FileAttachments.Any())
     {
         LoadAttachFile();
     }
     return(FileAttachments);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the MimeMessage representation of the MailMergeMessage for a specific data item.
        /// </summary>
        /// <param name="dataItem">
        /// The following types are accepted:
        /// Dictionary&lt;string,object&gt;, 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);
            }
        }