コード例 #1
0
        /// <summary>
        /// Send an email message from the sender to the receiver.
        /// </summary>
        /// <param name="sender">The sender of the email with the credentials initialized.</param>
        /// <param name="recipient">The receiver of the email.</param>
        /// <param name="message">The message to send.</param>
        public void Send(MEmailClient sender, List <MEmailClient> recipients, MEmailMessage message)
        {
            // init mail message object
            MailMessage mailMessage = new MailMessage();

            mailMessage.From       = new MailAddress(sender.EmailID, sender.Password);
            mailMessage.Subject    = message.Subject;
            mailMessage.Body       = message.Body;
            mailMessage.IsBodyHtml = message.IsBodyHtml;

            // include all recipients to our mail message object
            foreach (MEmailClient recipient in recipients)
            {
                mailMessage.To.Add(recipient.EmailID);
            }

            // set SMTP client object
            SmtpClient smtpClient = new SmtpClient(sender.Provider.Host, sender.Provider.Port);

            smtpClient.EnableSsl             = sender.Provider.EnableSSL;
            smtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;

            // set timeout for the SMTP server to respond
            smtpClient.Timeout = (int)TimeSpan.FromSeconds(10).TotalMilliseconds;

            smtpClient.Send(mailMessage);
        }
コード例 #2
0
        /// <summary>
        /// Run an emailing task right now, regardless of the scheduled time of the task.
        /// </summary>
        /// <param name="task">The emailing task to run.</param>
        /// <returns>Returns the number of emails successfully sent.</returns>
        public int Run(MEmailTask task)
        {
            // initialize the integer value which counts the number of emails sent
            int sendCount = 0;

            // get the essential details out of the task
            MEmailClient        sender     = task.Sender;
            List <MEmailClient> recipients = task.Recipients;
            MEmailMessage       message    = task.Message;

            // send the message from the sender to each recipient
            foreach (MEmailClient recipient in recipients)
            {
                // send the message to the recipient
                bool isSuccess = Send(sender, recipient, message);

                // increment the send count if the message is sent to the recipient successfully
                if (isSuccess)
                {
                    sendCount++;
                }
            }

            // return the feedback of how many emails are completed successfully
            return(sendCount);
        }
コード例 #3
0
        /// <summary>
        /// Send an email message from the sender to the receiver.
        /// </summary>
        /// <param name="sender">The sender of the email, with the credentials initialized.</param>
        /// <param name="recipient">The receiver of the email.</param>
        /// <param name="message">The message to send.</param>
        /// <returns>Returns true if email is successfully sent, false otherwise.</returns>
        public bool Send(MEmailClient sender, MEmailClient recipient, MEmailMessage message)
        {
            // initialize the boolean which serves as a feedback whether this task is successful/not
            bool isSuccess = false;

            try
            {
                // set the mail message to be sent with From, To, Subject, etc.
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(recipient.ClientAddress);

                // set the sender details (email ID and display name of the sender)
                mailMessage.From = new MailAddress(sender.ClientAddress, sender.ClientName);

                // get the email subject and message
                mailMessage.Subject = message.Subject;
                mailMessage.Body    = message.Message;

                // set the mail message to render the text as HTML / Non-HTML
                mailMessage.IsBodyHtml = message.IsBodyHtml;

                // set an SMTP client object (which defines the protocol used to send email messages)
                SmtpClient smtpClient = new SmtpClient(sender.Provider.Host, sender.Provider.Port);
                smtpClient.EnableSsl             = true; // HTTPS
                smtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;

                // set the sender credentials
                smtpClient.Credentials = new NetworkCredential(sender.ClientAddress, sender.ClientSecret);

                // MAX timeout to wait for the server to respond (set to 10 seconds)
                smtpClient.Timeout = 10000;

                // send the email message
                smtpClient.Send(mailMessage);

                // smtpClient.Send will generate an exception in case of failure
                // hence, if it reaches here, the email has been successful sent
                isSuccess = true;
            }
            catch (Exception)
            {
                // An exception occurs, hence, the email has not been sent.
                isSuccess = false;
            }

            // return the feedback whether the task is completed successfully/not
            return(isSuccess);
        }
コード例 #4
0
 /// <summary>
 /// Send an email message from the sender to the receiver.
 /// </summary>
 /// <param name="recipient">The receiver of the email to send.</param>
 /// <param name="message">The message to send.</param>
 /// <returns>Returns true if email is successfully sent, false otherwise.</returns>
 public bool SendWithDefaultSender(MEmailClient recipient, MEmailMessage message)
 {
     return(Send(Sender, recipient, message));
 }
コード例 #5
0
 /// <summary>
 /// Constructor to initialize object with default sender.
 /// </summary>
 public EmailService(MEmailClient defaultSender)
 {
     this.DefaultSender = defaultSender;
 }