Example #1
0
        private void Setup()
        {
            // read content
            string html = ReadHtmlText(_htmlFile);

            // insert your own e-mail address here!
            MailMergeAddress myMailAddress = new MailMergeAddress(MailAddressType.TestAddress,
                                    Helper.FromEmailAddress, Helper.FromEmailName, Encoding.Default);
            // create the mail message
            //_mmm = new MailMergeMessage("CRON Job Status Report for Domain '{DomainName:\"{0}{empty:[name not registered!]}\"}'", null, html);
            _mmm = new MailMergeMessage(_subject, _plainText, html, _fileAttachments);
            //_mmm.PlainText =_plainText;

            // adjust mail specific settings
            _mmm.CharacterEncoding = Encoding.GetEncoding("iso-8859-1");
            _mmm.CultureInfo = new System.Globalization.CultureInfo("en-US");
            _mmm.TextTransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
            _mmm.BinaryTransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            // add recipients, from address and test address to use.
            // the address part of the test address will be used instead of the other addresses.
            _mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "<{Email}>", "{FirstName} {LastName}", Encoding.Default));
            _mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, myMailAddress.Address, myMailAddress.DisplayName, Encoding.Default));
            _mmm.MailMergeAddresses.Add(myMailAddress);

            // base directory for html images
            _mmm.FileBaseDir = Helper.EmailHTMLFileImagesFolder; //GetMailDemoFilesDir();

            // setup the mail sender
            _mailSender = null;
            _mailSender = new MailMergeSender();

            SetupEventHandlers();

            //_mailSender.LocalHostName = "mail." + Environment.MachineName;
            _mailSender.MaxFailures = Helper.EmailMaxFailures;
            _mailSender.DelayBetweenMessages = Helper.EmailDelayBetweenMessages;

            _mailSender.MailOutputDirectory = _outputFolder;
            _mailSender.MessageOutput = Helper.EmailMessageOutput;  // change to MessageOutput.SmtpServer if you like, but be careful :)

            // smtp details - change to your demands
            _mailSender.SmtpHost = Helper.EmailSmtpHost;
            _mailSender.SmtpPort = Helper.EmailSmtpPort;
            _mailSender.SetSmtpAuthentification(Helper.EmailSmtpAuthentificationUserName, Helper.EmailSmtpAuthentificationPassword);
            _mailSender.LocalHostName = Helper.EmailLocalHostName;
        }
Example #2
0
        /// <summary>
        /// Prepares all recipient address and the corresponding header fields of a mail message.
        /// </summary>
        private void AddAddressesToMailMessage()
        {
            #region *** Clear MailMessage headers ***

            /*
             * Not really necessary because we always work on a NEW instance of a MailMessage
             */

            //cc, _bcc, _sender, _from, _replyto;
            _mailMessage.To.Clear();
            _mailMessage.CC.Clear();
            _mailMessage.Bcc.Clear();
            _mailMessage.ReplyToList.Clear();
            _mailMessage.Sender = null;

            #endregion

            _badMailAddr.Clear();

            MailMergeAddress testAddress = null;
            foreach (MailMergeAddress mmAddr in MailMergeAddresses.Where(mmAddr => mmAddr.AddrType == MailAddressType.TestAddress))
            {
                testAddress = new MailMergeAddress(MailAddressType.TestAddress, mmAddr.Address, mmAddr.DisplayName,
                                                   mmAddr.DisplayNameCharacterEncoding);
            }

            // ShowNullsAs MUST be string.empty with email addresses!
            TextVariableManager txtMgr = _textVariableManager.Clone();
            txtMgr.ShowNullAs = txtMgr.ShowEmptyAs = string.Empty;

            foreach (MailMergeAddress mmAddr in MailMergeAddresses)
            {
                try
                {
                    MailAddress mailAddr;
                    // use the address part the test mail address (if set) but use the original display name
                    if (testAddress != null)
                    {
                        testAddress.DisplayName         = mmAddr.DisplayName;
                        testAddress.TextVariableManager = txtMgr;
                        mailAddr = testAddress.GetMailAddress();
                    }
                    else
                    {
                        mmAddr.TextVariableManager = txtMgr;
                        mailAddr = mmAddr.GetMailAddress();
                    }

                    _badVariableNames.AddRange(txtMgr.BadVariables);
                    _badInlineFiles.AddRange(txtMgr.BadFiles);

                    if (IgnoreEmptyRecipientAddr && mailAddr == null)
                    {
                        continue;
                    }

                    switch (mmAddr.AddrType)
                    {
                    case MailAddressType.To:
                        _mailMessage.To.Add(mailAddr);
                        break;

                    case MailAddressType.CC:
                        _mailMessage.CC.Add(mailAddr);
                        break;

                    case MailAddressType.Bcc:
                        _mailMessage.Bcc.Add(mailAddr);
                        break;

                    case MailAddressType.ReplyTo:
                        _mailMessage.ReplyToList.Add(mailAddr);
                        break;

                    case MailAddressType.ConfirmReadingTo:
                        _mailMessage.Headers.Remove(CConfirmReading);
                        _mailMessage.Headers.Remove(CDispositionNotification);
                        _mailMessage.Headers.Add(CConfirmReading, mailAddr.Address);
                        _mailMessage.Headers.Add(CDispositionNotification, mailAddr.Address);
                        break;

                    case MailAddressType.ReturnReceiptTo:
                        _mailMessage.Headers.Remove(CReturnReceipt);
                        _mailMessage.Headers.Add(CReturnReceipt, mailAddr.Address);
                        break;

                    case MailAddressType.Sender:
                        _mailMessage.Sender = mailAddr;
                        break;

                    case MailAddressType.From:
                        _mailMessage.From = mailAddr;
                        break;
                    }
                }
                catch (FormatException)
                {
                    _badMailAddr.Add(mmAddr.ToString());
                }
            }
        }