Пример #1
0
        public void PushFileFTP(string localPath)
        {
            // Get the object used to communicate with the server.

            string distantPath = string.Format("ftp://{0}{1}",
                                                ConfigurationManager.AppSettings["ftpServer"],
                                                ConfigurationManager.AppSettings["ftpFilePath"].Replace("#Date#", DateTime.Now.ToString("yyyyMMdd")));
            Program.log("Distant Path : " + distantPath);
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(distantPath);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["ftpLogin"],
                                                            ConfigurationManager.AppSettings["ftpPass"]);

                // Copy the contents of the file to the request stream.

                StreamReader sourceStream = new StreamReader(localPath, true);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                fileContents = Encoding.UTF8.GetPreamble().Concat(fileContents).ToArray();
                //byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;

                Stream requestStream = request.GetRequestStream();
                //requestStream.
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Program.log("response : " + response.StatusCode.ToString() + " " + response.StatusDescription);
                //Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();

                MailHelper mh = new MailHelper(MailType.BasicText);
                mh.MailSubject = "Morning Service Collecte Canal";
                mh.MailType = MailType.BasicText;
                mh.Recipients = new List<MailAddress>{new MailAddress("*****@*****.**")};
                mh.Sender = new MailAddress("*****@*****.**");
                mh.ReplyTo = new MailAddress("*****@*****.**");
                mh.SetContentDirect("Ok pour ce matin.");
                mh.Send();
            }
            catch (Exception e)
            {
                Program.log("Exception envoi FTP : " + e.Message + " /// " + e.StackTrace);
            }
        }