コード例 #1
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);
 }
コード例 #2
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;
        }