コード例 #1
0
        /// <summary>
        /// Send a quick email message.  Use this method if not using the class properties
        /// </summary>
        /// <param name="from">Sender email address</param>
        /// <param name="fromDisplayName">Sender full name</param>
        /// <param name="to">Receiver email address</param>
        /// <param name="toDisplayName">Receiver full name</param>
        /// <param name="subject">Message subject</param>
        /// <param name="body">Message body</param>
        /// <param name="priority">Message priority</param>
        /// <param name="attachmentsUrls">Lisft of URLs or paths of the files to attach</param>
        /// <param name="useSSL">Use Secure Socket Layer (true or false)</param>
        /// <param name="serverAddress">Mail server's IP address or host name</param>
        /// <param name="userName">Sender's mail account username (usually the email address)</param>
        /// <param name="password">Sender's mail account password</param>
        /// <param name="port">SMTP port</param>
        /// <returns>True if successful; False if not</returns>
        public bool SendMail(string from, string fromDisplayName, string to, string toDisplayName, string subject,
                             string body, MailPriority priority, List<string> attachmentsUrls, bool useSSL,
                             string serverAddress, string userName, string password, int port, bool useTLS)
        {
            try
            {
                List<Recipient> recipient = new List<Recipient>(1);

                recipient.Add(new Recipient(to,toDisplayName));

                SmtpClient client = BuildSmtp(serverAddress, userName, password, port, useSSL);
                MailMessage mail = BuildMail(from, fromDisplayName, recipient, subject, @body, priority, attachmentsUrls);

                if (useTLS)
                {
                    client.TargetName = "STARTTLS/" + serverAddress;
                }    
                client.Send(mail);
                mail.Dispose();
                client.Dispose();

                return true;
            }
            catch (SmtpException smtp)
            {
                error = new ErrorManager();
                error.Message = smtp.Message;
                error.Source = smtp.Source;
                error.InnerException = smtp.InnerException;
                errorList.Add(error);

                return false;
            }
            catch (Exception ex)
            {
                error = new ErrorManager();
                error.Message = ex.Message;
                error.Source = ex.Source;
                error.InnerException = ex.InnerException;
                errorList.Add(error);

                return false;
            }
        }
コード例 #2
0
        /// <summary>
        /// Send a mail message.  Use this method with the class properties
        /// </summary>
        /// <returns>True if successful, otherwise False</returns>
        public bool SendMail()
        {
            try
            {
                SmtpClient client = BuildSmtp(SMTPHost, SMTPUsername, SMTPPassword, SMTPPort, EnableSSL);
                MailMessage mail = BuildMail(Sender, SenderDisplayName, Recipients, Subject, @Body, Priority, AttachmentUrls);                

                if (UseTLS)
                {
                    client.TargetName = "STARTTLS/" + SMTPHost;
                }
                client.Send(mail);
                mail.Dispose();
                client.Dispose();

                return true;
            }
            catch (SmtpException smtp)
            {
                error = new ErrorManager();
                error.Message = smtp.Message;
                error.Source = smtp.Source;
                error.InnerException = smtp.InnerException;
                errorList.Add(error);

                return false;
            }
            catch (Exception ex)
            {
                error = new ErrorManager();
                error.Message = ex.Message;
                error.Source = ex.Source;
                error.InnerException = ex.InnerException;
                errorList.Add(error);

                return false;
            }
        }        
コード例 #3
0
 /// <summary>
 /// Main constructor
 /// </summary>
 public MailManager()
 {
     error = null;
     errorList = new List<ErrorManager>();
     DownloadedMessages = new dsTables();
     Recipients = new List<Recipient>();
     AttachmentUrls = new List<string>();
     EnableSSL = true;
     UseTLS = false;
 }