Ejemplo n.º 1
0
        /// <summary>
        ///     Get a new mail message.
        ///     Use SmtpClient to send that mail message.
        /// </summary>
        /// <param name="sender">Your mail address</param>
        /// <param name="displayName">
        ///     Email sending display name. It will be displayed on the user's mailboxes left node as a name, instead of email
        ///     address.
        /// </param>
        /// <param name="subject">Email subject</param>
        /// <param name="body">emailAddress body</param>
        /// <param name="isHtmlBody">By default : true</param>
        /// <param name="bodyEncoding">By default : Encoding.UTF8</param>
        /// <param name="deliveryNotification"></param>
        /// <returns>Returns a new MailMessage.</returns>
        public MailMessage GetNewMailMessage(
            string subject        = "",
            string body           = "",
            bool isHtmlBody       = true,
            Encoding bodyEncoding = null,
            string sender         = null,
            string displayName    = null,
            DeliveryNotificationOptions deliveryNotification = DeliveryNotificationOptions.OnFailure)
        {
            bodyEncoding = bodyEncoding ?? Encoding.UTF8;
            var mail = new MailMessage {
                BodyEncoding = bodyEncoding,
                DeliveryNotificationOptions = deliveryNotification,
                IsBodyHtml = isHtmlBody,
                Body       = body,
                Subject    = subject
            };

            sender      = sender ?? _senderMail;
            displayName = displayName ?? DisplayName;
            var mailAddress = new MailAddress(sender, displayName);

            mail.Sender = mailAddress;
            mail.From   = mailAddress;
            //mail.ReplyToList.Add(mailAddress);
            return(mail);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// send email
        /// </summary>
        /// <param name="fromAddress"></param>
        /// <param name="toAddress"></param>
        /// <param name="mainBody"></param>
        /// <param name="subject"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool SendEmail(MailAddress fromAddress, MailAddress toAddress, string mainBody, string subject)
        {
            try {
                //create email message
                DeliveryNotificationOptions  DeliveryOptions = DeliveryNotificationOptions.OnFailure;
                System.Net.Mail.MailPriority Priority        = MailPriority.Normal;

                string      Attachfile = null;
                MailAddress bcc        = null;
                MailAddress CC         = null;

                //add header (greetings) to body
                dynamic Body = "<p></p>Dear " + toAddress.DisplayName.ToUpperInvariant() + "," + "<p></p>" + mainBody;
                //add footer to body
                Body = Body + "<p></p><br /><br />Kind Regards," + "<p></p><br />" + fromAddress.DisplayName.ToUpperInvariant();


                return(SendMail.Send(Body, subject, DeliveryOptions, Priority, toAddress, fromAddress, Attachfile, bcc, CC));
            }
            catch (Exception ex) {
                //do nothing if there is an exception logging an exception - really bad!
                System.Diagnostics.Debug.Print("Error in sending email exception " + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Message);
                ExceptionHandling.LogException(ref ex);
                //UtilsShared.LogException(ex3);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public EncryptedEmail(MailPriority mailPriority, DeliveryNotificationOptions deliveryNotificationOptions, string from, IList <SecureMailAddress> recipients, string subject, string body, params Attachment[] attachments)
        {
            if (string.IsNullOrEmpty(from))
            {
                throw new ArgumentException("NULL/string.Empty is not allowed", "from");
            }

            if (recipients == null || recipients.Count == 0)
            {
                throw new ArgumentException("NULL/Zero is not allowed", "recipients");
            }

            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentException("NULL/string.Empty is not allowed", "subject");
            }

            if (body == null)
            {
                throw new ArgumentException("NULL is not allowed", "body");
            }

            this.MailPriority = mailPriority;
            this.DeliveryNotificationOptions = deliveryNotificationOptions;
            this.From        = from;
            this.Recipients  = recipients;
            this.Subject     = subject;
            this.Body        = body;
            this.Attachments = attachments;
        }
Ejemplo n.º 4
0
        public static bool Send(string Body, string Subject, DeliveryNotificationOptions DeliveryOptions, MailPriority Priority, MailAddress ToMail, MailAddress FromMail, string AttachFile, MailAddress bcc, MailAddress CC)
        {
            MailMessage mail = new MailMessage();

            // Check that the connection is good
            if (!DoesPing("securesend.lawyersonline.co.uk"))
            {
                //Need to try another time
                //set flag for retry?
                return(false);
            }

            using (mail) {
                try {
                    var _with1 = mail;
                    if ((AttachFile != null) && System.IO.File.Exists(AttachFile))
                    {
                        //check that file exists Throw exception?
                        Attachment item = new Attachment(AttachFile);
                        _with1.Attachments.Add(item);
                    }
                    if ((bcc != null))
                    {
                        _with1.Bcc.Add(bcc);
                    }
                    if ((CC != null))
                    {
                        _with1.CC.Add(CC);
                    }
                    _with1.BodyEncoding = System.Text.Encoding.Unicode;
                    _with1.To.Add(ToMail);
                    _with1.From     = FromMail;
                    _with1.Priority = Priority;
                    _with1.DeliveryNotificationOptions = DeliveryOptions;
                    if ((FromMail != null))
                    {
                        _with1.ReplyTo = FromMail;
                    }
                    _with1.Sender     = FromMail;
                    _with1.Subject    = Subject;
                    _with1.Body       = Body;
                    _with1.IsBodyHtml = true;
                    SmtpClient SmtpMail = new SmtpClient();
                    var        _with2   = SmtpMail;
                    _with2.Host           = "securesend.lawyersonline.co.uk";
                    _with2.DeliveryMethod = SmtpDeliveryMethod.Network;
                    _with2.Send(mail);
                    //smtpMail
                    //mail
                    return(true);
                }
                catch (Exception ex) {
                    System.Diagnostics.Debug.Print(ex.Message); ExceptionHandling.LogException(ref ex);
                    return(false);
                }
            }
            //mail
        }
Ejemplo n.º 5
0
 public DeliveryNotificationCommand(
     ILogger <DeliveryNotificationCommand> logger,
     ICertificateService certificateService,
     IExternalBlobFileTransferClient externalFileTransferClient,
     IInternalBlobFileTransferClient internalFileTransferClient,
     IOptions <DeliveryNotificationOptions> options)
     : base(externalFileTransferClient, internalFileTransferClient)
 {
     _logger             = logger;
     _certificateService = certificateService;
     _options            = options?.Value;
 }
Ejemplo n.º 6
0
        public void Arrange()
        {
            _mockLogger                     = new Mock <ILogger <Domain.Print.DeliveryNotificationCommand> >();
            _mockCertificateService         = new Mock <ICertificateService>();
            _mockExternalFileTransferClient = new Mock <IExternalBlobFileTransferClient>();
            _mockInternalFileTransferClient = new Mock <IInternalBlobFileTransferClient>();
            _mockOptions                    = new Mock <IOptions <DeliveryNotificationOptions> >();
            _mockMessageQueue               = new Mock <ICollector <string> >();

            _options = new DeliveryNotificationOptions
            {
                Directory        = "MockDeliveryNotificationDirectory",
                ArchiveDirectory = "MockArchiveDeliveryNotificationDirectory"
            };

            _mockOptions
            .Setup(m => m.Value)
            .Returns(_options);

            _downloadedFiles = new List <string>();
            var generator = new RandomGenerator();

            for (int i = 0; i < 10; i++)
            {
                var filename = $"DeliveryNotifications-{generator.Next(DateTime.Now.AddDays(-100), DateTime.Now.AddDays(100)).ToString("ddMMyyHHmm")}.json";
                _downloadedFiles.Add(filename);

                _mockExternalFileTransferClient
                .Setup(m => m.DownloadFile($"{_options.Directory}/{filename}"))
                .ReturnsAsync(JsonConvert.SerializeObject(new DeliveryReceipt {
                    DeliveryNotifications = new List <DeliveryNotification>
                    {
                        new DeliveryNotification {
                            BatchID = _batchNumber
                        }
                    }
                }));
            }
            ;

            _mockExternalFileTransferClient
            .Setup(m => m.GetFileNames(It.IsAny <string>(), It.IsAny <string>(), false))
            .ReturnsAsync(_downloadedFiles);

            _sut = new Domain.Print.DeliveryNotificationCommand(
                _mockLogger.Object,
                _mockCertificateService.Object,
                _mockExternalFileTransferClient.Object,
                _mockInternalFileTransferClient.Object,
                _mockOptions.Object
                );
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Serializable Mail Message Contstructor
        /// </summary>
        /// <param name="mailMessage">Original Mail Message to be serialized</param>
        public SerializableMailMessage(MailMessage mailMessage)
        {
            MailID              = Guid.NewGuid().ToString();
            this.IsBodyHtml     = mailMessage.IsBodyHtml;
            this.Body           = mailMessage.Body;
            this.Subject        = mailMessage.Subject;
            this.SerializedFrom = SerializableMailAddress.GetSerializableMailAddress(mailMessage.From);

            this.serializedTo = new List <SerializableMailAddress>();
            foreach (var ma in mailMessage.To)
            {
                this.serializedTo.Add(SerializableMailAddress.GetSerializableMailAddress(ma));
            }

            this.serializedBcc = new List <SerializableMailAddress>();
            foreach (var ma in mailMessage.Bcc)
            {
                this.serializedBcc.Add(SerializableMailAddress.GetSerializableMailAddress(ma));
            }

            this.serializedCC = new List <SerializableMailAddress>();
            foreach (var ma in mailMessage.CC)
            {
                this.serializedCC.Add(SerializableMailAddress.GetSerializableMailAddress(ma));
            }

            this.serializedReplyTo = new List <SerializableMailAddress>();
            foreach (var ma in mailMessage.ReplyToList)
            {
                this.serializedReplyTo.Add(SerializableMailAddress.GetSerializableMailAddress(ma));
            }

            this.serializedAttachments = new List <SerializableAttachment>();
            foreach (var att in mailMessage.Attachments)
            {
                this.serializedAttachments.Add(SerializableAttachment.GetSerializeableAttachment(att));
            }

            this.bodyEncoding = mailMessage.BodyEncoding;

            this.serializedDeliveryNotificationOptions = mailMessage.DeliveryNotificationOptions;
            this.serializedHeaders = SerializableCollection.GetSerializableCollection(mailMessage.Headers);
            this.mailPriority      = mailMessage.Priority;
            this.SerializedSender  = SerializableMailAddress.GetSerializableMailAddress(mailMessage.Sender);
            this.subjectEncoding   = mailMessage.SubjectEncoding;

            foreach (var av in mailMessage.AlternateViews)
            {
                this.serializedAlternateViews.Add(SerializableAlternateView.GetSerializableAlternateView(av));
            }
        }
Ejemplo n.º 8
0
        public SerializableMailMessage(MailMessage mailMessage)
        {
            IsBodyHtml = mailMessage.IsBodyHtml;
            Body       = mailMessage.Body;
            Subject    = mailMessage.Subject;
            From       = new SerializableMailAddress(mailMessage.From);

            foreach (MailAddress ma in mailMessage.To)
            {
                To.Add(new SerializableMailAddress(ma));
            }

            foreach (MailAddress ma in mailMessage.CC)
            {
                CC.Add(new SerializableMailAddress(ma));
            }

            foreach (MailAddress ma in mailMessage.Bcc)
            {
                Bcc.Add(new SerializableMailAddress(ma));
            }

            foreach (Attachment att in mailMessage.Attachments)
            {
                Attachments.Add(new SerializableAttachment(att));
            }

            BodyEncoding = mailMessage.BodyEncoding;

            DeliveryNotificationOptions = mailMessage.DeliveryNotificationOptions;
            Headers  = new SerializableCollection(mailMessage.Headers);
            Priority = mailMessage.Priority;

            foreach (MailAddress ma in mailMessage.ReplyToList)
            {
                ReplyToList.Add(new SerializableMailAddress(ma));
            }

            if (mailMessage.Sender != null)
            {
                Sender = new SerializableMailAddress(mailMessage.Sender);
            }

            SubjectEncoding = mailMessage.SubjectEncoding;

            foreach (AlternateView av in mailMessage.AlternateViews)
            {
                AlternateViews.Add(new SerializableAlternateView(av));
            }
        }
Ejemplo n.º 9
0
        public EmailTemplate()
            : base()
        {
            ReplyToList = new MailAddressCollection();
            To          = new MailAddressCollection();
            CC          = new MailAddressCollection();
            Bcc         = new MailAddressCollection();

            Headers     = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            Attachments = new List <Attachment>();
            DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.None;
            Priority        = MailPriority.Normal;
            LinkedResources = new Dictionary <string, LinkedResource>();
        }
Ejemplo n.º 10
0
        public string DeliveryNotification(DeliveryNotificationOptions value)
        {
            string str;

            try
            {
                this.message.DeliveryNotificationOptions = value;
                str = "";
            }
            catch (Exception exception)
            {
                str = SmtpMessage.ErrorMessage(exception);
            }
            return(str);
        }
        ///

        /// Creates a new serializeable mailmessage based on a MailMessage object
        ///

        ///
        public SerializeableMailMessage(MailMessage mm)
        {
            IsBodyHtml = mm.IsBodyHtml;
            Body       = mm.Body;
            Subject    = mm.Subject;
            From       = SerializeableMailAddress.GetSerializeableMailAddress(mm.From);
            To         = new List <SerializeableMailAddress>();
            foreach (MailAddress ma in mm.To)
            {
                To.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));
            }

            CC = new List <SerializeableMailAddress>();
            foreach (MailAddress ma in mm.CC)
            {
                CC.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));
            }

            Bcc = new List <SerializeableMailAddress>();
            foreach (MailAddress ma in mm.Bcc)
            {
                Bcc.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));
            }

            Attachments = new List <SerializeableAttachment>();
            foreach (Attachment att in mm.Attachments)
            {
                Attachments.Add(SerializeableAttachment.GetSerializeableAttachment(att));
            }

            BodyEncoding = mm.BodyEncoding;

            DeliveryNotificationOptions = mm.DeliveryNotificationOptions;
            Headers         = SerializeableCollection.GetSerializeableCollection(mm.Headers);
            Priority        = mm.Priority;
            ReplyTo         = SerializeableMailAddress.GetSerializeableMailAddress(mm.ReplyTo);
            Sender          = SerializeableMailAddress.GetSerializeableMailAddress(mm.Sender);
            SubjectEncoding = mm.SubjectEncoding;

            foreach (AlternateView av in mm.AlternateViews)
            {
                AlternateViews.Add(SerializeableAlternateView.GetSerializeableAlternateView(av));
            }
        }
Ejemplo n.º 12
0
        ///
        /// Creates a new serializeable mailmessage based on a MailMessage object
        ///
        public SerializeableMailMessage(MailMessage mailMessage)
        {
            IsBodyHtml = mailMessage.IsBodyHtml;
            Body       = mailMessage.Body;
            Subject    = mailMessage.Subject;
            From       = SerializeableMailAddress.GetSerializeableMailAddress(mailMessage.From);
            _to        = new List <SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.To)
            {
                _to.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _cc = new List <SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.CC)
            {
                _cc.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _bcc = new List <SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.Bcc)
            {
                _bcc.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _attachments = new List <SerializeableAttachment>();
            foreach (var attachment in mailMessage.Attachments)
            {
                _attachments.Add(SerializeableAttachment.GetSerializeableAttachment(attachment));
            }

            _bodyEncoding = mailMessage.BodyEncoding;

            _deliveryNotificationOptions = mailMessage.DeliveryNotificationOptions;
            _headers         = SerializeableCollection.GetSerializeableCollection(mailMessage.Headers);
            _priority        = mailMessage.Priority;
            ReplyTo          = mailMessage.ReplyToList.Select(SerializeableMailAddress.GetSerializeableMailAddress).ToList();
            Sender           = SerializeableMailAddress.GetSerializeableMailAddress(mailMessage.Sender);
            _subjectEncoding = mailMessage.SubjectEncoding;

            foreach (AlternateView av in mailMessage.AlternateViews)
            {
                _alternateViews.Add(SerializeableAlternateView.GetSerializeableAlternateView(av));
            }
        }
        ///
        /// Creates a new serializeable mailmessage based on a MailMessage object
        ///
        public SerializeableMailMessage(MailMessage mailMessage)
        {
            IsBodyHtml = mailMessage.IsBodyHtml;
            Body = mailMessage.Body;
            Subject = mailMessage.Subject;
            From = SerializeableMailAddress.GetSerializeableMailAddress(mailMessage.From);
            _to = new List<SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.To)
            {
                _to.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _cc = new List<SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.CC)
            {
                _cc.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _bcc = new List<SerializeableMailAddress>();
            foreach (var mailAddress in mailMessage.Bcc)
            {
                _bcc.Add(SerializeableMailAddress.GetSerializeableMailAddress(mailAddress));
            }

            _attachments = new List<SerializeableAttachment>();
            foreach (var attachment in mailMessage.Attachments)
            {
                _attachments.Add(SerializeableAttachment.GetSerializeableAttachment(attachment));
            }

            _bodyEncoding = mailMessage.BodyEncoding;

            _deliveryNotificationOptions = mailMessage.DeliveryNotificationOptions;
            _headers = SerializeableCollection.GetSerializeableCollection(mailMessage.Headers);
            _priority = mailMessage.Priority;
            ReplyTo = mailMessage.ReplyToList.Select(SerializeableMailAddress.GetSerializeableMailAddress).ToList();
            Sender = SerializeableMailAddress.GetSerializeableMailAddress(mailMessage.Sender);
            _subjectEncoding = mailMessage.SubjectEncoding;

            foreach (AlternateView av in mailMessage.AlternateViews)
                _alternateViews.Add(SerializeableAlternateView.GetSerializeableAlternateView(av));
        }
Ejemplo n.º 14
0
        ///
        /// Creates a new serializeable mailmessage based on a MailMessage object
        ///
        public MailMessageWrapper(MailMessage mm)
        {
            IsBodyHtml = mm.IsBodyHtml;
            Body       = mm.Body;
            Subject    = mm.Subject;
            From       = MailAddressWrapper.GetSerializeableMailAddress(mm.From);
            foreach (MailAddress ma in mm.To)
            {
                To.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
            }
            foreach (MailAddress ma in mm.CC)
            {
                CC.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
            }
            foreach (MailAddress ma in mm.Bcc)
            {
                Bcc.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
            }
            foreach (Attachment att in mm.Attachments)
            {
                Attachments.Add(AttachmentWrapper.GetSerializeableAttachment(att));
            }

            BodyEncoding = mm.BodyEncoding;

            DeliveryNotificationOptions = mm.DeliveryNotificationOptions;
            Headers  = CollectionWrapper.GetSerializeableCollection(mm.Headers);
            Priority = mm.Priority;

            foreach (MailAddress ma in mm.ReplyToList)
            {
                ReplyTo.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
            }

            Sender          = MailAddressWrapper.GetSerializeableMailAddress(mm.Sender);
            SubjectEncoding = mm.SubjectEncoding;

            foreach (AlternateView av in mm.AlternateViews)
            {
                AlternateViews.Add(AlternateViewWrapper.GetSerializeableAlternateView(av));
            }
        }
Ejemplo n.º 15
0
    /// 
    /// Creates a new serializeable mailmessage based on a MailMessage object
    /// 
    public MailMessageWrapper(MailMessage mm)
    {
      IsBodyHtml = mm.IsBodyHtml;
      Body = mm.Body;
      Subject = mm.Subject;
      From = MailAddressWrapper.GetSerializeableMailAddress(mm.From);
      foreach (MailAddress ma in mm.To)
      {
        To.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
      }
      foreach (MailAddress ma in mm.CC)
      {
        CC.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
      }
      foreach (MailAddress ma in mm.Bcc)
      {
        Bcc.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
      }
      foreach (Attachment att in mm.Attachments)
      {
        Attachments.Add(AttachmentWrapper.GetSerializeableAttachment(att));
      }

      BodyEncoding = mm.BodyEncoding;

      DeliveryNotificationOptions = mm.DeliveryNotificationOptions;
      Headers = CollectionWrapper.GetSerializeableCollection(mm.Headers);
      Priority = mm.Priority;

      foreach (MailAddress ma in mm.ReplyToList)
      {
        ReplyTo.Add(MailAddressWrapper.GetSerializeableMailAddress(ma));
      }

      Sender = MailAddressWrapper.GetSerializeableMailAddress(mm.Sender);
      SubjectEncoding = mm.SubjectEncoding;

      foreach (AlternateView av in mm.AlternateViews)
        AlternateViews.Add(AlternateViewWrapper.GetSerializeableAlternateView(av));
    }
Ejemplo n.º 16
0
        public SerializableMailMessage(MailMessage mailMessage)
        {
            IsBodyHtml = mailMessage.IsBodyHtml;
            Body = mailMessage.Body;
            Subject = mailMessage.Subject;
            From = new SerializableMailAddress(mailMessage.From);

            foreach (MailAddress ma in mailMessage.To)
                To.Add(new SerializableMailAddress(ma));

            foreach (MailAddress ma in mailMessage.CC)
                CC.Add(new SerializableMailAddress(ma));

            foreach (MailAddress ma in mailMessage.Bcc)
                Bcc.Add(new SerializableMailAddress(ma));

            foreach (Attachment att in mailMessage.Attachments)
                Attachments.Add(new SerializableAttachment(att));

            BodyEncoding = mailMessage.BodyEncoding;

            DeliveryNotificationOptions = mailMessage.DeliveryNotificationOptions;
            Headers = new SerializableCollection(mailMessage.Headers);
            Priority = mailMessage.Priority;

            foreach (MailAddress ma in mailMessage.ReplyToList)
                ReplyToList.Add(new SerializableMailAddress(ma));

            if (mailMessage.Sender != null)
                Sender = new SerializableMailAddress(mailMessage.Sender);

            SubjectEncoding = mailMessage.SubjectEncoding;

            foreach (AlternateView av in mailMessage.AlternateViews)
                AlternateViews.Add(new SerializableAlternateView(av));
        }
        /// <summary>
        /// Sends a message Va Client
        /// </summary>
        /// <param name="To">Who are we sending to</param>
        /// <param name="Subject">what is the subject</param>
        /// <param name="Message">what is the message</param>
        /// <param name="CC">Ad more people muhahaha know to tag them Default = null</param>
        /// <param name="MailPriority">Is this important to get network priority Default = MailPriority.Normal</param>
        /// <param name="IsHTML">Will it use HTML Default = true(of course you eould)</param>
        /// <param name="DeliveryNotificationOptions">Probably better you dont know if it was sent but would you like to Default = DeliveryNotificationOptions.None</param>
        /// <param name="From">From Default = Client.DefaultFrom (null will do this for you)</param>
        /// <param name="MSGEncoding">The Messages encoding Default = Client.MSGEncoding (null will do this for you)</param>
        public void SendMessage(string[] To, string Subject, string Message, string[] CC = null, MailPriority MailPriority = MailPriority.Normal, bool IsHTML = true, DeliveryNotificationOptions DeliveryNotificationOptions = DeliveryNotificationOptions.None, MailAddress From = null, Encoding MSGEncoding = null)
        {
            //the system expects a list but often its easier to give an array of strings so add the person to a list and check if stings are valid
            List <MailAddress> ToList = new List <MailAddress>();
            List <MailAddress> CCList = new List <MailAddress>();

            //if any thing is bad just throw
            if (To == null)
            {
                throw new ArgumentNullException("To is null");
            }

            if (Subject == null)
            {
                throw new ArgumentNullException("To is null");
            }

            if (Message == null)
            {
                throw new ArgumentNullException("To is null");
            }

            //try building a list and throw if any one is not a valid email the count should give us the index if it fails as the count will stay at the last count
            foreach (string s in To)
            {
                try
                {
                    ToList.Add(new MailAddress(s));
                }
                catch (Exception e)
                {
                    throw new Exception(s + " was not a valid Email in To emails on index " + ToList.Count, e);
                }
            }
            //try building a list and throw if any one is not a valid email the count should give us the index if it fails as the count will stay at the last count
            if (CC != null)
            {
                foreach (string s in CC)
                {
                    try
                    {
                        CCList.Add(new MailAddress(s));
                    }
                    catch (Exception e)
                    {
                        throw new Exception(s + " was not a valid Email in CC emails on index " + ToList.Count, e);
                    }
                }
            }
            //leverage the earlier vers
            SendMessage(ToList.ToArray(), Subject, Message, CCList.ToArray(), MailPriority, IsHTML, DeliveryNotificationOptions, From, MSGEncoding);
        }
 /// <summary>
 /// Sends a message Va Client
 /// </summary>
 /// <param name="To">Who are we sending to</param>
 /// <param name="Subject">what is the subject</param>
 /// <param name="Message">what is the message</param>
 /// <param name="CC">Ad more people muhahaha know to tag them Default = null</param>
 /// <param name="MailPriority">Is this important to get network priority Default = MailPriority.Normal</param>
 /// <param name="IsHTML">Will it use HTML Default = true(of course you eould)</param>
 /// <param name="DeliveryNotificationOptions">Probably better you dont know if it was sent but would you like to Default = DeliveryNotificationOptions.None</param>
 /// <param name="From">From Default = Client.DefaultFrom (null will do this for you)</param>
 /// <param name="MSGEncoding">The Messages encoding Default = Client.MSGEncoding (null will do this for you)</param>
 public void SendMessage(string To, string Subject, string Message, string[] CC = null, MailPriority MailPriority = MailPriority.Normal, bool IsHTML = true, DeliveryNotificationOptions DeliveryNotificationOptions = DeliveryNotificationOptions.None, MailAddress From = null, Encoding MSGEncoding = null)
 {
     //the system expects a list but often its easier than that with a single person for each so add the person to a an array and leverage the array call
     SendMessage(new string[] { To }, Subject, Message, CC, MailPriority, IsHTML, DeliveryNotificationOptions, From, MSGEncoding);
 }
        /// <summary>
        /// Sends a message Va Client
        /// </summary>
        /// <param name="To">Who are we sending to</param>
        /// <param name="Subject">what is the subject</param>
        /// <param name="Message">what is the message</param>
        /// <param name="CC">Ad more people muhahaha know to tag them Default = null</param>
        /// <param name="MailPriority">Is this important to get network priority Default = MailPriority.Normal</param>
        /// <param name="IsHTML">Will it use HTML Default = true(of course you eould)</param>
        /// <param name="DeliveryNotificationOptions">Probably better you dont know if it was sent but would you like to Default = DeliveryNotificationOptions.None</param>
        /// <param name="From">From Default = Client.DefaultFrom (null will do this for you)</param>
        /// <param name="MSGEncoding">The Messages encoding Default = Client.MSGEncoding (null will do this for you)</param>
        public void SendMessageQuick(string To, string Subject, string Message, string[] CC = null, MailPriority MailPriority = MailPriority.Normal, bool IsHTML = true, DeliveryNotificationOptions DeliveryNotificationOptions = DeliveryNotificationOptions.None, string From = null, Encoding MSGEncoding = null)
        {
            MailAddress MAF;

            try
            {
                MAF = From != null ? new MailAddress(From) : DefaultFrom;
            }
            catch (Exception e)
            {
                throw new Exception(From + " was not a valid Email", e);
            }

            SendMessage(new string[] { To }, Subject, Message, CC, MailPriority, IsHTML, DeliveryNotificationOptions, MAF, MSGEncoding);
        }
Ejemplo n.º 20
0
 public void SetNotify(DeliveryNotificationOptions options)
 {
     MailMessage.DeliveryNotificationOptions = options;
 }
Ejemplo n.º 21
0
        /// <summary>
        /// 发送邮件(无附件)
        /// </summary>
        /// <param name="subject">标题</param>
        /// <param name="content">内容</param>
        /// <param name="showEmail">显示的发件人邮箱</param>
        /// <param name="showName">显示的发件人名称</param>
        /// <param name="receiveEmails">收件人邮箱列表(多个邮箱“英文分号”分隔)</param>
        /// <param name="ccEmails">抄送人邮箱列表(多个邮箱“英文分号”分隔)</param>
        /// <param name="notification">邮件通知类型(DeliveryNotificationOptions.None)</param>
        /// <param name="true_name">发件人账号(不用写@符号后面的)MyEncrypt.MyEncrypt.Decrypt("dmsxdm9zd3MxcUBsdC5sa3NuOC5tMnc=")</param>
        /// <param name="true_pwd">发件人密码MyEncrypt.MyEncrypt.Decrypt("dnZ3QGFiY2RlZg==")</param>
        public static void SendEmail_noFile(string subject, string content, string showEmail, string showName, string receiveEmails, string ccEmails, DeliveryNotificationOptions notification, string true_name, string true_pwd)
        {
            if (string.IsNullOrEmpty(receiveEmails) || !receiveEmails.Contains('@'))
            {
                return;
            }
            MailAddress from = new MailAddress(showEmail, showName); //邮件的发件人
            MailMessage mail = new MailMessage();

            //设置邮件的标题
            mail.Subject = subject;

            //设置邮件的发件人
            //Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
            mail.From = from;

            //设置邮件的收件人
            string address     = receiveEmails;
            string displayName = "";

            /*  这里这样写是因为可能发给多个联系人,每个地址用 ; 号隔开
             * 一般从地址簿中直接选择联系人的时候格式都会是 :用户名1 < mail1 >; 用户名2 < mail 2>;
             * 因此就有了下面一段逻辑不太好的代码
             * 如果永远都只需要发给一个收件人那么就简单了 mail.To.Add("收件人mail");
             */
            string[] mailNames = address.Split(';');
            foreach (string name in mailNames)
            {
                if (name != string.Empty)
                {
                    if (name.IndexOf('<') > 0)
                    {
                        displayName = name.Substring(0, name.IndexOf('<'));
                        address     = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    else
                    {
                        displayName = string.Empty;
                        address     = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    mail.To.Add(new MailAddress(address, displayName));
                }
            }

            //设置邮件的抄送收件人
            string[] ccStrs = ccEmails.Split(';');
            foreach (string name in ccStrs)
            {
                if (name != string.Empty)
                {
                    if (name.IndexOf('<') > 0)
                    {
                        displayName = name.Substring(0, name.IndexOf('<'));
                        address     = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    else
                    {
                        displayName = string.Empty;
                        address     = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    mail.CC.Add(new MailAddress(address, displayName));
                }
            }
            //设置邮件的内容
            mail.Body = content;
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml   = true;
            //设置邮件的发送级别
            mail.Priority = MailPriority.Normal;

            //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
            //string fileName = "";
            //fileName = "D:/UpFile/" + fileName.Substring(fileName.LastIndexOf("/") + 1);
            //mail.Attachments.Add(new Attachment(fileName));

            mail.DeliveryNotificationOptions = notification;

            SmtpClient client = new SmtpClient();

            //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
            client.Host = "mail1-in.baidu.com";
            //设置用于 SMTP 事务的端口,默认的是 25
            client.UseDefaultCredentials = false;
            //这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 hbgx@hotmail, 我的用户名为 hbgx ,我的密码是 xgbh
            client.Credentials    = new System.Net.NetworkCredential(true_name, true_pwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //都定义完了,正式发送了,很是简单吧!
            client.Send(mail);
        }
Ejemplo n.º 22
0
 MailMessage IMail.CreateMailMessage(string from, string to, string subject, string body, Encoding encoding, bool isBodyHtml, List <Attachment> attachments, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
 {
     return(((IMail)this).CreateMailMessage(from, to, "", "", "", subject, body, encoding, isBodyHtml, attachments, priority, deliveryNotificationOptions));
 }
Ejemplo n.º 23
0
 MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, string filePath, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
 {
     if (filePath != null)
     {
         return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, new FileInfo[] { new FileInfo(filePath) }, priority, deliveryNotificationOptions));
     }
     else
     {
         return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, default(FileInfo[]), priority, deliveryNotificationOptions));
     }
 }
Ejemplo n.º 24
0
        MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, List <IAttachmentBytes> attachmentBytes, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
        {
            List <Attachment> attachments;

            attachments = null;

            if (attachmentBytes != null && attachmentBytes.Count > 0)
            {
                attachments = new List <Attachment>();

                foreach (IAttachmentBytes attachment in attachmentBytes)
                {
                    attachments.Add(new Attachment(new MemoryStream(attachment.Bytes), attachment.FileName, attachment.MediaType));
                }
            }

            return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, attachments, priority, deliveryNotificationOptions));
        }
Ejemplo n.º 25
0
        MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, string[] filePaths, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
        {
            List <FileInfo> fileInfos;

            if (filePaths != null)
            {
                fileInfos = new List <FileInfo>();

                foreach (string tmp in filePaths)
                {
                    if (tmp != null && tmp != null)
                    {
                        fileInfos.Add(new FileInfo(tmp));
                    }
                }

                return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, fileInfos.ToArray(), priority, deliveryNotificationOptions));
            }
            else
            {
                return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, default(FileInfo[]), priority, deliveryNotificationOptions));
            }
        }
Ejemplo n.º 26
0
 MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
 {
     return(((IMail)this).CreateMailMessage(from, to, "", "", "", subject, body, encoding, isBodyHtml, default(FileInfo[]), priority, deliveryNotificationOptions));
 }
        /// <summary>
        /// Sends a message Va Client
        /// </summary>
        /// <param name="To">Who are we sending to</param>
        /// <param name="Subject">what is the subject</param>
        /// <param name="Message">what is the message</param>
        /// <param name="CC">Ad more people muhahaha know to tag them Default = null</param>
        /// <param name="MailPriority">Is this important to get network priority Default = MailPriority.Normal</param>
        /// <param name="IsHTML">Will it use HTML Default = true(of course you eould)</param>
        /// <param name="DeliveryNotificationOptions">Probably better you dont know if it was sent but would you like to Default = DeliveryNotificationOptions.None</param>
        /// <param name="From">From Default = Client.DefaultFrom (null will do this for you)</param>
        /// <param name="MSGEncoding">The Messages encoding Default = Client.MSGEncoding (null will do this for you)</param>
        public void SendMessage(MailAddress[] To, string Subject, string Message, MailAddress[] CC = null, MailPriority MailPriority = MailPriority.Normal, bool IsHTML = true, DeliveryNotificationOptions DeliveryNotificationOptions = DeliveryNotificationOptions.None, MailAddress From = null, Encoding MSGEncoding = null)
        {
            if (To == null)
            {
                throw new ArgumentNullException("To is null");
            }

            if (Subject == null)
            {
                throw new ArgumentNullException("To is null");
            }

            if (Message == null)
            {
                throw new ArgumentNullException("To is null");
            }

            // we'll compose and send a message
            using (MailMessage MSG = new MailMessage())
            {
                MSG.BodyEncoding = MSGEncoding ?? DefaultEncoding;
                MSG.Priority     = MailPriority;
                MSG.IsBodyHtml   = IsHTML;
                MSG.Subject      = Subject;
                MSG.Body         = Message;
                MSG.From         = From ?? DefaultFrom;
                MSG.DeliveryNotificationOptions = DeliveryNotificationOptions;

                //build a list of Mail tos and CC if that is available
                foreach (MailAddress MA in To)
                {
                    MSG.To.Add(MA);
                }

                if (CC != null)
                {
                    foreach (MailAddress MA in CC)
                    {
                        MSG.CC.Add(MA);
                    }
                }

                //send the message
                Client.Send(MSG);
            }
        }
Ejemplo n.º 28
0
 MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, Attachment attachment, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
 {
     if (attachment != null)
     {
         return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, new List <Attachment> {
             attachment
         }, priority, deliveryNotificationOptions));
     }
     else
     {
         return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, default(List <Attachment>), priority, deliveryNotificationOptions));
     }
 }
Ejemplo n.º 29
0
    private static MailMessage ConstructEmailMessage(
        SysConfig sc,
        string mailTo,
        string mailSubject,
        string mailBody,
        MailAddress fromAddress,
        string mailCc,
        string blindCopyRec,
        string replyAddress,
        string fileAttachments,
        ref string validAttachment,
        bool requestReadReceipt,
        int sensitivity,
        DeliveryNotificationOptions deliveryNotification,
        MailPriority mailPriorty,
        bool bodyHtml)
    {
        var eMail = new MailMessage {
            From = fromAddress
        };


        if (replyAddress.Trim() != string.Empty)
        {
            foreach (var eml in replyAddress.Split(';'))
            {
                if (eml.Trim().Length > 0)
                {
                    // eMail.ReplyToList.Add(eml.Trim());
                }
            }
        }

        foreach (var eml in mailTo.Split(';'))
        {
            if (eml.Trim().Length > 0)
            {
                eMail.To.Add(eml.Trim());
            }
        }

        if (mailCc.Trim() != string.Empty)
        {
            foreach (var eml in mailCc.Split(';'))
            {
                if (eml.Trim().Length > 0)
                {
                    eMail.CC.Add(eml.Trim());
                }
            }
        }

        if (blindCopyRec.Trim() != string.Empty)
        {
            foreach (var eml in blindCopyRec.Split(';'))
            {
                if (eml.Trim().Length > 0)
                {
                    eMail.Bcc.Add(eml.Trim());
                }
            }
        }

        if (fileAttachments.Trim() != string.Empty)
        {
            // message.Attachments.Add(new Attachment(PathToAttachment));
            foreach (string eml in fileAttachments.Split(';'))
            {
                if (eml.Trim().Length > 0)
                {
                    string ext = Path.GetExtension(eml).Replace(".", string.Empty);
                    if (sc.ProhibitedExtension.Contains(ext))
                    {
                        LogEntry.LogItem("Prohibit extension!", "Warning",
                                         $"Attachment : {eml} has prohibit extension!", eMail.HeaderInformation());
                        continue;
                    }

                    if (File.Exists(eml) == false)
                    {
                        LogEntry.LogItem("File does not exists", "Warning",
                                         $"Attachment :{eml} does not exists on file system!", eMail.HeaderInformation());
                        continue;
                    }

                    if (sc.MaxFileSize < new FileInfo(eml).Length)
                    {
                        LogEntry.LogItem("Max.file size limit!", "Warning",
                                         $"Attachment : {eml} has size larger then allowed!", eMail.HeaderInformation());
                        continue;
                    }

                    var a = new Attachment(eml)
                    {
                        NameEncoding = Encoding.UTF8
                    };
                    eMail.Attachments.Add(a);
                    validAttachment += eml + ";";
                }
            }
        }

        if (requestReadReceipt)
        {
            eMail.Headers.Add("Disposition-Notification-To", replyAddress == string.Empty ? eMail.From.Address : replyAddress);
        }


        if (sensitivity >= 0 && sensitivity <= 2)
        {
            // sensitivity = "Personal" / "Private" / "Company-Confidential"
            if (sensitivity == 0)
            {
                eMail.Headers.Add("Sensitivity", "Personal");
            }
            else if (sensitivity == 1)
            {
                eMail.Headers.Add("Sensitivity", "Private");
            }
            else
            {
                eMail.Headers.Add("Sensitivity", "Company-Confidential");
            }
        }

        eMail.DeliveryNotificationOptions = deliveryNotification;
        eMail.Priority   = mailPriorty;
        eMail.IsBodyHtml = bodyHtml;

        eMail.SubjectEncoding = Encoding.UTF8;
        eMail.BodyEncoding    = Encoding.UTF8;

        eMail.Subject = mailSubject;
        eMail.Body    = mailBody;

        return(eMail);
    }
Ejemplo n.º 30
0
        MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, List <Attachment> attachments, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
        {
            MailMessage message = new MailMessage
            {
                From = new MailAddress(from, null, encoding)
            };

            this.AddMailAddressCollection(message.To, to, encoding);
            this.AddMailAddressCollection(message.CC, cc, encoding);
            this.AddMailAddressCollection(message.Bcc, bcc, encoding);
            this.AddMailAddressCollection(message.ReplyToList, replyTo, encoding);

            message.IsBodyHtml = isBodyHtml;

            message.SubjectEncoding = encoding;
            message.BodyEncoding    = encoding;

            message.Subject = subject;
            message.Body    = body;

            message.Priority = priority;
            message.DeliveryNotificationOptions = deliveryNotificationOptions;

            if (attachments != null && attachments.Count > 0)
            {
                foreach (Attachment attachment in attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            message.Headers.Add("Atomus", "Atomus Framework");

            return(message);
        }
Ejemplo n.º 31
0
 MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, IAttachmentBytes attachmentBytes, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
 {
     return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml
                                            , new Attachment(new MemoryStream(attachmentBytes.Bytes), attachmentBytes.FileName, attachmentBytes.MediaType), priority, deliveryNotificationOptions));
 }
Ejemplo n.º 32
0
        MailMessage IMail.CreateMailMessage(string from, string to, string cc, string bcc, string replyTo, string subject, string body, Encoding encoding, bool isBodyHtml, FileInfo[] fileInfos, MailPriority priority, DeliveryNotificationOptions deliveryNotificationOptions)
        {
            List <Attachment> attachments;

            attachments = null;

            if (fileInfos != null && fileInfos.Length > 0)
            {
                attachments = new List <Attachment>();

                foreach (FileInfo fileInfo in fileInfos)
                {
                    if (fileInfo != null)
                    {
                        attachments.Add(new Attachment(new StreamReader(fileInfo.FullName).BaseStream, fileInfo.Name, MimeMapping.GetMimeMapping(fileInfo.FullName)));
                    }
                }
            }

            return(((IMail)this).CreateMailMessage(from, to, cc, bcc, replyTo, subject, body, encoding, isBodyHtml, attachments, priority, deliveryNotificationOptions));
        }