Exemple #1
0
        public static string SendMail(string mailFrom, string mailTo, string replyTo, string cc, string bcc, string subject, string body)
        {
            DotNetNuke.Services.Mail.MailPriority priority = DotNetNuke.Services.Mail.MailPriority.Normal;
            MailFormat        bodyFormat         = MailFormat.Html;
            Encoding          bodyEncoding       = Encoding.UTF8;
            List <Attachment> attachments        = new List <Attachment>();
            string            smtpServer         = Host.SMTPServer;
            string            smtpAuthentication = Host.SMTPAuthentication;
            string            smtpUsername       = Host.SMTPUsername;
            string            smtpPassword       = Host.SMTPPassword;
            bool smtpEnableSsl = Host.EnableSMTPSSL;

            string res = Mail.SendMail(mailFrom,
                                       mailTo,
                                       cc,
                                       bcc,
                                       replyTo,
                                       priority,
                                       subject,
                                       bodyFormat,
                                       bodyEncoding,
                                       body,
                                       attachments,
                                       smtpServer,
                                       smtpAuthentication,
                                       smtpUsername,
                                       smtpPassword,
                                       smtpEnableSsl);

            //Mail.SendEmail(replyTo, mailFrom, mailTo, subject, body);
            return(res);
        }
        /// <summary>
        /// Sends an email message.
        /// </summary>
        /// <remarks>
        /// This is a modified version of the DNN Core's <see cref="Mail.SendMail(string,string,string,string,DotNetNuke.Services.Mail.MailPriority,string,DotNetNuke.Services.Mail.MailFormat,System.Text.Encoding,string,string[],string,string,string,string,bool)"/> method.
        /// It does not provide a
        /// </remarks>
        /// <param name="from">The email address from which the email is being sent.</param>
        /// <param name="to">The email address(es) to which the email is being sent.</param>
        /// <param name="cc">The email address(es) on the carbon copy line.</param>
        /// <param name="bcc">The email address(es) on the blind carbon copy line.</param>
        /// <param name="replyTo">The email address to which replies should be directed, if not <paramref name="from"/>.</param>
        /// <param name="priority">The priority of the email message.</param>
        /// <param name="subject">The subject of the email message.</param>
        /// <param name="bodyFormat">The format of <paramref name="body"/>.</param>
        /// <param name="bodyEncoding">The encoding of <paramref name="body"/>.</param>
        /// <param name="body">The body of the email message.</param>
        /// <param name="attachments">A list of the text to include with this email message as an attachment named "Appointment.ics".</param>
        /// <param name="smtpServer">The SMTP server to use to send the email, if not the server defined in the Host Settings of the website.</param>
        /// <param name="smtpAuthentication">The SMTP authentication type to use to send the email, if not the type defined in the Host Settings of the website.</param>
        /// <param name="smtpUserName">The SMTP username to use to send the email, if not the username defined in the Host Settings of the website.</param>
        /// <param name="smtpPassword">The SMTP password to use to send the email, if not the password defined in the Host Settings of the website.</param>
        /// <param name="smtpEnableSsl">if set to <c>true</c> sends the message to the SMTP server over SSL.</param>
        /// <returns><see cref="string.Empty"/>, or an error message if an error occurs.</returns>
        public static string SendMail(
            string from,
            string to,
            string cc,
            string bcc,
            string replyTo,
            DotNetNuke.Services.Mail.MailPriority priority,
            string subject,
            MailFormat bodyFormat,
            Encoding bodyEncoding,
            string body,
            string[] attachments,
            string smtpServer,
            string smtpAuthentication,
            string smtpUserName,
            string smtpPassword,
            bool smtpEnableSsl)
        {
            if (to == null)
            {
                throw new ArgumentNullException("to", "to must not be null");
            }

            if (attachments == null)
            {
                throw new ArgumentNullException("attachments", "attachments must not be null");
            }

            var returnMessage = string.Empty;

            // SMTP server configuration
            smtpServer         = GetValueOrHostSetting(smtpServer, "SMTPServer");
            smtpAuthentication = GetValueOrHostSetting(smtpAuthentication, "SMTPAuthentication");
            smtpUserName       = GetValueOrHostSetting(smtpUserName, "SMTPUsername");
            smtpPassword       = GetValueOrHostSetting(smtpPassword, "SMTPPassword");

            // translate semi-colon delimiters to commas as ASP.NET 2.0 does not support semi-colons
            using (MailMessage objMail = new MailMessage(from, to.Replace(";", ",")))
            {
                MemoryStream attachmentStream = null;
                try
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(cc))
                        {
                            objMail.CC.Add(cc.Replace(";", ","));
                        }

                        if (!string.IsNullOrEmpty(bcc))
                        {
                            objMail.Bcc.Add(bcc.Replace(";", ","));
                        }

                        if (!string.IsNullOrEmpty(replyTo))
                        {
                            objMail.ReplyTo = new MailAddress(replyTo);
                        }

                        objMail.Priority   = (System.Net.Mail.MailPriority)priority;
                        objMail.IsBodyHtml = bodyFormat == MailFormat.Html;

                        foreach (string attachment in attachments)
                        {
                            if (!string.IsNullOrEmpty(attachment))
                            {
                                ////objMail.Attachments.Add(new Attachment(myAtt));
                                attachmentStream = new MemoryStream(Encoding.Default.GetBytes(attachment));
                                objMail.Attachments.Add(new Attachment(attachmentStream, "Appointment.ics"));
                            }
                        }

                        // message
                        objMail.SubjectEncoding = bodyEncoding;
                        objMail.Subject         = HtmlUtils.StripWhiteSpace(subject, true);
                        objMail.BodyEncoding    = bodyEncoding;

                        objMail.Body = body;

                        // added support for multipart html messages - removed in this modified version
                        // add text part as alternate view - removed in this modified version
                        ////var PlainView = AlternateView.CreateAlternateViewFromString(ConvertToText(body), null, "text/plain");
                        ////objMail.AlternateViews.Add(PlainView);

                        // if body contains html, add html part
                        ////if (IsHTMLMail(body))
                        ////{
                        ////    var HTMLView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                        ////    objMail.AlternateViews.Add(HTMLView);
                        ////}
                    }
                    catch (Exception objException)
                    {
                        // Problem creating Mail Object
                        returnMessage = to + ": " + objException.Message;
                        Exceptions.LogException(objException);
                    }

                    // external SMTP server alternate port
                    int smtpPort = Null.NullInteger;
                    int portPos  = smtpServer.IndexOf(":", StringComparison.Ordinal);
                    if (portPos > -1)
                    {
                        smtpPort   = int.Parse(smtpServer.Substring(portPos + 1, smtpServer.Length - portPos - 1), CultureInfo.InvariantCulture);
                        smtpServer = smtpServer.Substring(0, portPos);
                    }

                    var smtpClient = new SmtpClient();

                    try
                    {
                        if (!string.IsNullOrEmpty(smtpServer))
                        {
                            smtpClient.Host = smtpServer;
                            if (smtpPort > Null.NullInteger)
                            {
                                smtpClient.Port = smtpPort;
                            }

                            switch (smtpAuthentication)
                            {
                            // basic
                            case "1":
                                if (!string.IsNullOrEmpty(smtpUserName) && !string.IsNullOrEmpty(smtpPassword))
                                {
                                    smtpClient.UseDefaultCredentials = false;
                                    smtpClient.Credentials           = new NetworkCredential(smtpUserName, smtpPassword);
                                }

                                break;

                            // NTLM
                            case "2":
                                smtpClient.UseDefaultCredentials = true;
                                break;
                            }
                        }

                        smtpClient.EnableSsl = smtpEnableSsl;

                        smtpClient.Send(objMail);
                        returnMessage = string.Empty;
                    }
                    catch (SmtpFailedRecipientException exc)
                    {
                        returnMessage = string.Format(CultureInfo.CurrentCulture, Localization.GetString("FailedRecipient"), exc.FailedRecipient);
                        Exceptions.LogException(exc);
                    }
                    catch (SmtpException exc)
                    {
                        returnMessage = Localization.GetString("SMTPConfigurationProblem");
                        Exceptions.LogException(exc);
                    }
                    catch (Exception objException)
                    {
                        // mail configuration problem
                        if (objException.InnerException != null)
                        {
                            returnMessage = string.Concat(objException.Message, Environment.NewLine, objException.InnerException.Message);
                            Exceptions.LogException(objException.InnerException);
                        }
                        else
                        {
                            returnMessage = objException.Message;
                            Exceptions.LogException(objException);
                        }
                    }
                }
                catch (Exception objException)
                {
                    returnMessage = objException.Message;
                    Exceptions.LogException(objException);
                }
                finally
                {
                    if (attachmentStream != null)
                    {
                        attachmentStream.Dispose();
                    }
                }

                return(returnMessage);
            }
        }