SendMail() public method

Fully self contained mail sending method. Sends an email message by connecting and disconnecting from the email server.
public SendMail ( ) : bool
return bool
Exemplo n.º 1
0
        protected void SendMailRun()
        {
            // Create an extra reference to insure GC doesn't collect
            // the reference from the caller
            SmtpClientNative Email = this;

            Email.SendMail();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends email using the WebStoreConfig Email Configuration defaults.
        /// 
        /// <seealso>Class WebUtils</seealso>
        /// </summary>
        /// <param name="Subject">
        /// The subject of the message.
        /// </param>
        /// <param name="Message">
        /// The body of the message.
        /// </param>
        /// <param name="Recipient">
        /// The recipient as an email address.
        /// </param>
        /// <param name="SendName">
        /// Name descriptive of the sender.
        /// </param>
        /// <param name="SendEmail">
        /// The email address of the sender.
        /// </param>
        /// <param name="NoAsync">
        /// Whether this message is send asynchronously or not.
        /// </param>
        /// <param name="Boolean SendAsText">
        /// Determines whether the message is sent as Text or HTML.
        /// </param>
        /// <returns>Boolean</returns>
        public static bool SendEmail(string Subject, string Message, string Recipient, string SendName,
                                     string SendEmail, bool NoAsync, bool SendAsText, delSmtpNativeEvent OnErrorHandler)
        {
            try
            {
                SmtpClientNative Email = new SmtpClientNative();
                Email.MailServer = App.Configuration.MailServer;
                if (!string.IsNullOrEmpty(App.Configuration.MailServerUsername))
                {
                    Email.Username = App.Configuration.MailServerUsername;
                    Email.Password = App.Configuration.MailServerPassword;
                }
                Email.Recipient = Recipient;

                if (SendAsText)
                    Email.ContentType = "text/plain";
                else
                {
                    Email.ContentType = "text/html; charset=utf-8";
                    Email.Encoding = Encoding.UTF8;
                }


                if (SendName == null || SendName == "")
                {
                    Email.SenderEmail = App.Configuration.SenderEmailAddress;
                    Email.SenderName = App.Configuration.SenderEmailName;
                }
                else
                {
                    Email.SenderEmail = SendEmail;
                    Email.SenderName = SendName;
                }

                Email.Subject = Subject;
                Email.Message = Message;

                // *** Capture any error messages and log them
                if (OnErrorHandler == null)
                    Email.SendError += OnSmtpError;
                else
                    Email.SendError += OnErrorHandler;

                if (NoAsync)
                    return Email.SendMail();
                else
                    Email.SendMailAsync();
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }