Ejemplo n.º 1
0
        public int Insert(STMail data, out string msg)
        {
            int ret = 0;

            msg = null;

            SqlConnection connect;
            Log           log = new Log(LogPath);

            try
            {
                connect = new SqlConnection(ConnectionString);
                connect.Open();
                if (connect.State == ConnectionState.Open)
                {
                    toTags(data, out data.recvalue, out msg);
                    string     query = "INSERT INTO dbo.Mails (RecValue) VALUES (@1)";
                    SqlCommand cmd   = new SqlCommand(query, connect);
                    cmd.Parameters.Add(crp(SqlDbType.VarChar, "@1", data.recvalue, false));
                    SqlDataReader reader = cmd.ExecuteReader();
                    connect.Close();
                }
                else
                {
                    return(1);
                }
            }
            catch (Exception ex) { log.Write(LogType.Error, ex.Message); ret = -1; msg = ex.Message; }
            return(ret);
        }
Ejemplo n.º 2
0
        private bool CreateMailMessage(STMail mail, out MailMessage message, out string msg)
        {
            message = new MailMessage();
            msg     = null;

            string to;

            try
            {
                // Читаем шаблон
                string body = null;
                string path = Path.Combine(TemplatePath, mail.tamplate);
                body = File.ReadAllText(path);

                if (!File.Exists(path))
                {
                    msg = string.Format("Hasnt't found template {0}", path);
                    return(false);
                }

                // От кого и кому
                if (!string.IsNullOrEmpty(mail.to))
                {
                    to = mail.to;
                }
                else
                {
                    to = ExtractTag(body, "to");
                }

                string[] toArray = to.Split(';');

                message = new MailMessage(new MailAddress(From), new MailAddress(toArray[0]));
                for (int i = 1; i < toArray.Length; i++)
                {
                    message.To.Add(new MailAddress(toArray[i]));
                }
                // Тема
                message.Subject    = ExtractTag(body, "subject");
                message.IsBodyHtml = true;

                body = ExtractTag(body, "body");

                // Заменяем все метки в теле письма
                body = body.Replace("[MASKEDPAN]", mail.pan);
                body = body.Replace("[PWD]", mail.fleetpwd);
                body = body.Replace("[LINKKEY]", mail.linkkey);
                body = body.Replace("[LOGIN]", mail.login);

                // Вложения
                if (mail.attachment != null)
                {
                    string[] mass = mail.attachment.Split(';');
                    foreach (string s in mass)
                    {
                        Attachment attachData = new Attachment(s);
                        message.Attachments.Add(attachData);
                    }
                }

                // Поиск картинок
                int    count   = 0;
                string oldChar = ExtractImages(body, ref count);
                Random RGen    = new Random();
                List <LinkedResource> listimages = new List <LinkedResource>();
                while (oldChar != "")
                {
                    var image = new LinkedResource(Path.Combine(ImagesPath, oldChar), "image/jpg");
                    image.ContentId        = Guid.NewGuid().ToString();
                    image.TransferEncoding = TransferEncoding.Base64;
                    listimages.Add(image);

                    body = body.Replace("{" + oldChar + "}", "cid:" + image.ContentId);

                    oldChar = ExtractImages(body, ref count);
                }
                message.Body = body;

                if (listimages.Count > 0)
                {
                    AlternateView html_view = AlternateView.CreateAlternateViewFromString(message.Body, null, "text/html");
                    foreach (LinkedResource lrs in listimages)
                    {
                        html_view.LinkedResources.Add(lrs);
                    }
                    message.AlternateViews.Add(html_view);
                }
            }
            catch (Exception e) { msg = e.Message; return(false); }

            return(true);
        }
Ejemplo n.º 3
0
        private int toTags(STMail data, out string retvalue, out string msg)
        {
            int ret = 0;

            retvalue = null;
            msg      = null;

            string tag = null;

            Log log = new Log(LogPath);

            try
            {
                tag       = string.Format("<CDT={0}>", data.dtcreate);
                retvalue += tag;

                tag       = string.Format("<TOA={0}>", data.to);
                retvalue += tag;

                tag       = string.Format("<TMP={0}>", data.tamplate);
                retvalue += tag;

                if (data.pan != null)
                {
                    tag = string.Format("<PAN={0}>", data.pan);
                }
                else
                {
                    tag = null;
                }
                retvalue += tag;

                if (data.linkkey != null)
                {
                    tag = string.Format("<LIN={0}>", data.linkkey);
                }
                else
                {
                    tag = null;
                }
                retvalue += tag;

                if (data.fleetpwd != null)
                {
                    tag = string.Format("<PWD={0}>", data.fleetpwd);
                }
                else
                {
                    tag = null;
                }
                retvalue += tag;

                if (data.login != null)
                {
                    tag = string.Format("<LGN={0}>", data.login);
                }
                else
                {
                    tag = null;
                }
                retvalue += tag;

                if (data.attachment != null)
                {
                    tag = string.Format("<ATC={0}>", data.attachment);
                }
                else
                {
                    tag = null;
                }
                retvalue += tag;
            }
            catch (Exception ex) { log.Write(LogType.Error, ex.Message); }
            return(ret);
        }
Ejemplo n.º 4
0
        private bool read(SqlDataReader reader, out STMail data, out string msg)
        {
            bool ret = true;

            data = new STMail();
            msg  = null;
            try
            {
                data.recid = reader.GetValue(0).ToString();
                if (!reader.IsDBNull(1))
                {
                    data.recvalue = reader.GetString(1);
                }
                else
                {
                    data.recvalue = null;
                }
                if (!reader.IsDBNull(2))
                {
                    data.to = reader.GetString(2);
                }
                else
                {
                    data.to = null;
                }
                if (!reader.IsDBNull(3))
                {
                    data.tamplate = reader.GetString(3);
                }
                else
                {
                    data.tamplate = null;
                }
                if (!reader.IsDBNull(4))
                {
                    data.pan = reader.GetString(4);
                }
                else
                {
                    data.pan = null;
                }
                if (!reader.IsDBNull(5))
                {
                    data.linkkey = reader.GetString(5);
                }
                else
                {
                    data.linkkey = null;
                }
                if (!reader.IsDBNull(6))
                {
                    data.fleetpwd = reader.GetString(6);
                }
                else
                {
                    data.fleetpwd = null;
                }
                if (!reader.IsDBNull(7))
                {
                    data.login = reader.GetString(7);
                }
                else
                {
                    data.login = null;
                }
                if (!reader.IsDBNull(8))
                {
                    data.attachment = reader.GetString(8);
                }
                else
                {
                    data.attachment = null;
                }
            }
            catch (Exception ex) { msg = ex.Message; ret = false; }
            return(ret);
        }