Пример #1
0
        public static void SendMail(string mailTo, string mailFrom, string mailSubject, string mailBody)
        {
            SmtpEmailer emailer = new SmtpEmailer();

            emailer.Host = ConfigSettings.SMTPServer;
            try
            {
                emailer.Port = int.Parse(ConfigSettings.SMTPPort);
            }
            catch
            {
                emailer.Port = 25;
            }
            if (mailFrom == null)
            {
                emailer.From = ConfigSettings.TustenaMainMail;
            }
            else
            {
                emailer.From = mailFrom;
            }
            emailer.Subject = mailSubject;
            if (Regex.Match(mailBody, "</?\\w+\\s+[^>]*>").Success)
            {
                emailer.SendAsHtml = true;
            }
            else
            {
                emailer.SendAsHtml = false;
            }

            if (mailTo.IndexOf(';') > 0)
            {
                string[] to = mailTo.Split(';');
                foreach (string t in to)
                {
                    if (t.Length > 0)
                    {
                        emailer.To.Add(t);
                    }
                }
            }
            else
            {
                emailer.To.Add(mailTo);
            }
            emailer.Body = mailBody;
            switch (ConfigSettings.SpoolFormat)
            {
            case "mssmtp":
                emailer.SendIISSMTPMessage(ConfigSettings.MailSpoolPath);
                break;

            case "xmail":
                emailer.SendXMailMessage(ConfigSettings.MailSpoolPath);
                break;

            default:
                emailer.Host = ConfigSettings.SMTPServer;
                emailer.AuthenticationMode = AuthenticationType.Plain;
                if (ConfigSettings.SMTPAuthRequired)
                {
                    emailer.User     = ConfigSettings.SMTPUser;
                    emailer.Password = ConfigSettings.SMTPPassword;
                }
                emailer.SendMessageAsync();
                break;
            }
        }
Пример #2
0
        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            SmtpEmailer emailer = new SmtpEmailer();

            emailer.From       = MailAddressFrom.Text;
            emailer.Subject    = MailObject.Text;
            emailer.SendAsHtml = true;

            if (MailAddressTo.Text.Length > 0)
            {
                if (MailAddressTo.Text.IndexOf(';') > 1)
                {
                    string[] MailAddress = MailAddressTo.Text.Split(';');
                    foreach (string to in MailAddress)
                    {
                        emailer.To.Add(to);
                    }
                }
                else
                {
                    emailer.To.Add(MailAddressTo.Text);
                }
            }

            if (MailAddressCc.Text.Length > 0)
            {
                if (MailAddressCc.Text.IndexOf(';') > 1)
                {
                    string[] MailCc = MailAddressCc.Text.Split(';');
                    foreach (string to in MailCc)
                    {
                        emailer.CC.Add(to);
                    }
                }
                else
                {
                    emailer.To.Add(MailAddressCc.Text);
                }
            }

            if (MailAddressCcn.Text.Length > 0)
            {
                if (MailAddressCcn.Text.IndexOf(';') > 1)
                {
                    string[] MailCcn = MailAddressCcn.Text.Split(';');
                    foreach (string to in MailCcn)
                    {
                        emailer.BCC.Add(to);
                    }
                }
                else
                {
                    emailer.To.Add(MailAddressCcn.Text);
                }
            }



            emailer.Body = MailMessage.Text.Replace("\r", "").Replace("\n", "<br>");
            string filetodelete = "";

            if (this.IDDocument.Text.Length > 0)
            {
                DataTable dtAttach;
                dtAttach = DatabaseConnection.CreateDataset(String.Format("SELECT GUID,FILENAME FROM FILEMANAGER WHERE ID={0}", int.Parse(IDDocument.Text))).Tables[0];
                if (dtAttach.Rows.Count > 0)
                {
                    try
                    {
                        string dirPath;
                        string filePath;
                        dirPath  = ConfigSettings.DataStoragePath + Path.DirectorySeparatorChar + dtAttach.Rows[0]["guid"].ToString();
                        filePath = ConfigSettings.DataStoragePath + Path.DirectorySeparatorChar + dtAttach.Rows[0]["filename"].ToString();
                        FileFunctions.CheckDir(ConfigSettings.DataStoragePath, true);
                        if (File.Exists(dirPath + Path.GetExtension(dtAttach.Rows[0]["filename"].ToString())))
                        {
                            File.Copy(dirPath + Path.GetExtension(dtAttach.Rows[0]["filename"].ToString()), filePath);
                        }
                        else
                        {
                            File.Copy(dirPath, filePath);
                        }

                        SmtpAttachment att = new SmtpAttachment();
                        att.FileName    = filePath;
                        att.ContentType = "application/octet-stream";
                        att.Location    = AttachmentLocation.Attachment;
                        emailer.Attachments.Add(att);
                        filetodelete = filePath;
                    }
                    catch {}
                }
            }

            switch (ConfigSettings.SpoolFormat)
            {
            case "mssmtp":
                emailer.SendIISSMTPMessage(ConfigSettings.MailSpoolPath);
                break;

            case "xmail":
                emailer.SendXMailMessage(ConfigSettings.MailSpoolPath);
                break;

            default:
                emailer.Host = ConfigSettings.SMTPServer;
                emailer.AuthenticationMode = AuthenticationType.Plain;
                if (ConfigSettings.SMTPAuthRequired)
                {
                    emailer.User     = ConfigSettings.SMTPUser;
                    emailer.Password = ConfigSettings.SMTPPassword;
                }
                emailer.SendMessageAsync();
                break;
            }

            if (filetodelete.Length > 0)
            {
                File.Delete(filetodelete);
            }


            if (CreateActivity.Checked && MailAddressToID.Text.Length > 0)
            {
                ActivityInsert ai = new ActivityInsert();
                string         A  = "";
                string         C  = "";
                string         L  = "";

                switch (CrossWith.SelectedValue)
                {
                case "0":
                    A = MailAddressToID.Text;
                    C = "";
                    L = "";
                    break;

                case "1":
                    C = MailAddressToID.Text;
                    A = "";
                    L = "";
                    break;

                case "2":
                    L = MailAddressToID.Text;
                    A = "";
                    C = "";
                    break;
                }
                if (A.Length > 0 || C.Length > 0 || L.Length > 0)
                {
                    ai.InsertActivity("5", "", UC.UserId.ToString(), C, A, L, this.MailObject.Text, this.MailMessage.Text, UC.LTZ.ToUniversalTime(DateTime.Now), UC, 1);
                }
            }
            if (Session["fromtopbar"] != null)
            {
                Session.Remove("fromtopbar");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + Root.rm.GetString("Mailtxt19") + "');self.close();</script>");
            }
            else if (Session["fromquick"] != null)
            {
                Session.Remove("fromquick");
                OnSendMail(true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + Root.rm.GetString("Mailtxt19") + "');location.href='/mailinglist/webmail/webmail.aspx?m=46&dgb=1&si=63';</script>");
            }
        }