コード例 #1
0
        /// <summary>
        /// Sends a plain text email with an attached file.
        /// </summary>
        ///
        /// <param name="MessageSettings">
        /// Contains information about the email including to address, from address, subject text and body text.
        /// </param>
        ///
        /// <param name="AttachmentPath">
        /// The full name and path to the file that is to be attached.
        /// </param>
        ///
        /// <param name="ServerSettings">
        /// Contains information about the email server.
        /// </param>
        ///
        /// <returns>
        /// True if the email was sent successfully.
        /// </returns>
        ///
        public static bool SendEmailWithAttachment(Emails.Message MessageSettings, string AttachmentPath, Emails.Server ServerSettings)
        {
            Attachment attachment = null;

            if (!string.IsNullOrEmpty(AttachmentPath) && File.Exists(AttachmentPath))
            {
                attachment = new Attachment(AttachmentPath);
            }

            return(SendEmailWithAttachment(MessageSettings, attachment, ServerSettings));
        }
コード例 #2
0
        /// <summary>
        /// Sends a plain text email with an attached file.
        /// </summary>
        ///
        /// <param name="MessageSettings">
        /// Contains information about the email including to address, from address, subject text and body text.
        /// </param>
        ///
        /// <param name="EmailAttachment">
        /// The file contents of the attachment for the email.
        /// </param>
        ///
        /// <param name="ServerSettings">
        /// Contains information about the email server.
        /// </param>
        ///
        /// <returns>
        /// True if the email was sent successfully.
        /// </returns>
        ///
        public static bool SendEmailWithAttachment(Emails.Message MessageSettings, Attachment EmailAttachment, Emails.Server ServerSettings)
        {
            bool emailSent = false;

            if (!string.IsNullOrEmpty(MessageSettings.From))
            {
                if (!string.IsNullOrEmpty(MessageSettings.To))
                {
                    if (!string.IsNullOrEmpty(MessageSettings.Body))
                    {
                        //  Set up the server first.

                        var smtpServer = new SmtpClient(ServerSettings.Name)
                        {
                            Port      = ServerSettings.Port,
                            EnableSsl = ServerSettings.EnableSSL
                        };

                        //  Add credentials only if a user has been specified.

                        if (!string.IsNullOrEmpty(ServerSettings.User))
                        {
                            smtpServer.Credentials = new System.Net.NetworkCredential(ServerSettings.User, ServerSettings.Password);
                        }

                        //  Set up the email message.

                        var mail = new MailMessage
                        {
                            IsBodyHtml = false,
                            From       = new MailAddress(MessageSettings.From),
                            Subject    = MessageSettings.Subject,
                            Body       = MessageSettings.Body
                        };
                        mail.To.Add(MessageSettings.To);

                        if (!string.IsNullOrEmpty(MessageSettings.CC))
                        {
                            mail.CC.Add(MessageSettings.CC);
                        }
                        if (!string.IsNullOrEmpty(MessageSettings.BCC))
                        {
                            mail.Bcc.Add(MessageSettings.BCC);
                        }

                        if (EmailAttachment != null)
                        {
                            mail.Attachments.Add(EmailAttachment);
                        }

                        //  Send the email message.

                        try
                        {
                            smtpServer.Send(mail);
                            emailSent = true;
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            ErrorMessage = "To field did not contain a recipient.";
                        }
                        catch (SmtpFailedRecipientsException failedEx)
                        {
                            ErrorMessage = "Email could not be delivered.  " + failedEx.Message;
                        }
                        catch (SmtpException smtpEx)
                        {
                            ErrorMessage = smtpEx.ToString();
                        }
                    }
                    else
                    {
                        ErrorMessage = "Body of email cannot be empty for email messages.";
                    }
                }
                else
                {
                    ErrorMessage = "To field cannot be empty for email messages.";
                }
            }
            else
            {
                ErrorMessage = "From field cannot be empty for email messages.";
            }

            return(emailSent);
        }
コード例 #3
0
 /// <summary>
 /// Sends a plain text email.
 /// </summary>
 ///
 /// <param name="MessageSettings">
 /// Contains information about the email including to address, from address, subject text and body text.
 /// </param>
 ///
 /// <param name="ServerSettings">
 /// Contains information about the email server.
 /// </param>
 ///
 /// <returns>
 /// True if the email was sent successfully.
 /// </returns>
 ///
 public static bool SendEmail(Emails.Message MessageSettings, Emails.Server ServerSettings)
 {
     return(SendEmailWithAttachment(MessageSettings, "", ServerSettings));
 }