Exemplo n.º 1
0
        public static bool Send(MailProperties mailProperties, String Subject, string Body, bool ErrorMessage)
        {
            // TODO fertig machen von zertificaTEN falls notig | wie sieht es mit mail empfangen aus?
            try
            {
                MailMessage mail = new MailMessage();

                SmtpClient SmtpServer = new SmtpClient(mailProperties.SMTPclient); //SMTP Server von Hotmail und Outlook.
                SmtpServer.Port      = mailProperties.SMTPclientPort;
                SmtpServer.EnableSsl = mailProperties.EnableSsl;

                mail.From = new MailAddress(mailProperties.SenderAddress); //Absender

                for (int i = 0; i < mailProperties.RecipientAdresses.Length; i++)
                {
                    mail.To.Add(mailProperties.RecipientAdresses[i]); //Empfänger
                }
                mail.Subject = Subject;

                mail.Body = Body;

                //mail.IsBodyHtml = true; //Nur wenn Body HTML Quellcode ist

                if (mailProperties.SenderUserName != null && mailProperties.SenderPassword != null)
                {
                    SmtpServer.Credentials = new System.Net.NetworkCredential(mailProperties.SenderUserName, MailProperties.SimpleEncode(mailProperties.SenderPassword));
                }

                // ServicePointManager.ServerCertificateValidationCallback =
                //     delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                //     {
                //         return true;
                //     };

                SmtpServer.Send(mail);
                return(true);
            }
            catch (Exception ex)
            {
                if (ErrorMessage)
                {
                    MessageBox.Show("Error on sending Mail!" + Environment.NewLine + Environment.NewLine +
                                    "Sender:    " + mailProperties.SenderAddress + Environment.NewLine +
                                    "Subject:   " + Subject + Environment.NewLine +
                                    "Body:      " + Environment.NewLine +
                                    Body + Environment.NewLine + Environment.NewLine +
                                    "Error:     " + ex.Message, "Mail notification", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return(false);
            }
        }