Ejemplo n.º 1
0
        static int Main(string[] args)
        {
            string appDirectory = AppDomain.CurrentDomain.BaseDirectory;

            try
            {
                string mailConfigPath = string.Format(@"{0}config.xml", appDirectory);

                MailConfig mailConfig = MailConfig.Load(mailConfigPath);
                if (mailConfig == null)
                {
                    Singleton.Instance.WriteLog("mailConfig is null", "WARN");
                    return(0);
                }

                MailMessage message = new MailMessage(mailConfig.From, mailConfig.To);
                if (mailConfig.CC != null && mailConfig.CC.Count > 0)
                {
                    foreach (MailAddress address in mailConfig.CC)
                    {
                        message.CC.Add(address);
                    }
                }

                message.Subject      = "Kết quả tool kiểm tra cập nhật game";
                message.BodyEncoding = Encoding.UTF8;
                message.IsBodyHtml   = true;

                BodyRender bodyRender = new BodyRender();

                string dataFile = string.Format(@"{0}data.xml", appDirectory);
                message.Body = bodyRender.Render(dataFile);

                if ("" == message.Body)
                {
                    Singleton.Instance.WriteLog("message.Body empty", "ERROR");
                    return(1);
                }

                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

                client.UseDefaultCredentials = false;
                client.EnableSsl             = true;
                client.Credentials           = new NetworkCredential(message.From.Address, mailConfig.Password);

                client.Send(message);

                //send log

                string logFile = GetLogFilePath(dataFile);
                if (logFile.Equals("") == false)
                {
                    String strLogBody = bodyRender.RenderLogContent(logFile);

                    MailMessage logMessage = new MailMessage(mailConfig.From, mailConfig.LogReceive);
                    logMessage.Subject = "Tool Log";
                    logMessage.Body    = strLogBody;

                    client.Send(logMessage);

                    //MailMessage logMessage = new MailMessage(mailConfig.From, mailConfig.LogReceive);
                    //logMessage.Subject = "Tool Log";
                    //logMessage.Body = "FYI";

                    //Attachment data = new Attachment(logFile, MediaTypeNames.Application.Octet);
                    //ContentDisposition disposition = data.ContentDisposition;

                    //disposition.CreationDate = System.IO.File.GetCreationTime(logFile);
                    //disposition.ModificationDate = System.IO.File.GetLastWriteTime(logFile);
                    //disposition.ReadDate = System.IO.File.GetLastAccessTime(logFile);

                    //logMessage.Attachments.Add(data);
                    //client.Send(logMessage);
                }
            }

            catch (System.Exception ex)
            {
                Singleton.Instance.WriteLog(ex.ToString(), "ERROR");
            }

            return(0);
        }
Ejemplo n.º 2
0
        public static MailConfig Load(string fileName)
        {
            try
            {
                MailConfig mailConfig = new MailConfig();

                XmlDocument document = new XmlDocument();
                document.Load(fileName);

                XmlNode mailNode = document.SelectSingleNode("//mail");

                XmlAttribute onAttribute = mailNode.Attributes["on"];
                if (null == onAttribute || onAttribute.Value != "1")
                {
                    Singleton.Instance.WriteLog("Turn off sending email", "WARN");
                    return(null);
                }
                XmlNode fromNode = document.SelectSingleNode("//from");

                XmlAttribute usernameAttribute = fromNode.Attributes["username"];
                XmlAttribute passwordAttribute = fromNode.Attributes["password"];

                mailConfig.From     = new MailAddress(usernameAttribute.Value);
                mailConfig.Password = passwordAttribute.Value;

                XmlNode directToNode = mailNode.SelectSingleNode("./to");
                usernameAttribute = directToNode.Attributes["username"];
                mailConfig.To     = new MailAddress(usernameAttribute.Value);

                XmlNode logReceiveNode = mailNode.SelectSingleNode("./log");
                if (logReceiveNode != null)
                {
                    usernameAttribute = logReceiveNode.Attributes["username"];
                    if (usernameAttribute != null)
                    {
                        mailConfig.LogReceive = new MailAddress(usernameAttribute.Value);
                    }
                }

                XmlNodeList ccNodeList = mailNode.SelectNodes("./cc/to");
                if (ccNodeList != null)
                {
                    mailConfig.CC = new List <MailAddress>();
                    foreach (XmlNode toNode in ccNodeList)
                    {
                        usernameAttribute = toNode.Attributes["username"];
                        if (usernameAttribute != null)
                        {
                            mailConfig.CC.Add(new MailAddress(usernameAttribute.Value));
                        }
                    }
                }


                return(mailConfig);
            }
            catch (System.Exception ex)
            {
                Singleton.Instance.WriteLog(ex.ToString(), "ERROR");
                return(null);
            }
        }