Thursday, September 20, 2012

FTP Downloads in .NET

There are many situation in which you would want to connect to an FTP server and perform downloading of FTP Files. here is a helper class to perorm that.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Yournamespace.Core.FTP
{
    public class FTPManager
    {
        private string _FTPHost;
        private string _FTPUser;
        private string _FTPPassword;

        public string FTPHost
        {
            get { return _FTPHost; }
            set { _FTPHost = value; }
        }

        public string FTPUser
        {
            get { return _FTPUser; }
            set { _FTPUser = value; }
        }

        public string FTPPassword
        {
            get { return _FTPPassword; }
            set { _FTPPassword = value; }
        }

        public void Upload(string filename)
        {

        }

        public void Download(string filename)
        {
            FileStream outputStream = new FileStream(filename, FileMode.Create);
            FtpWebRequest request = null;
            FtpWebResponse response = null;
            Stream ftpStream = null;

            try
            {
                request = (FtpWebRequest)FtpWebRequest.Create(new
                  Uri("ftp://" + this.FTPHost + "/" + filename));
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(this.FTPUser, this.FTPPassword);
                response = (FtpWebResponse)request.GetResponse();
                ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }

        }
    }
}

Email Manager Class

There are many situations in which you would want to send emails from your application. This class just manages that.

see the code snippet below and please feel free to copy and implement in your application.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yournamespace.Core.Emails
{
    public static class EmailManager
    {
        public static void SendEmail(string emailTo, string emailFrom, string emailSubject, string emailMessage)
        {
            List<System.Net.Mail.MailAddress> toList = new List<System.Net.Mail.MailAddress>();
            System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(emailTo);

            toList.Add(address);
            SendEmail(toList, null, emailFrom, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> list, string emailSubject, string emailMessage)
        {
            SendEmail(list, null, null, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> toList, List<System.Net.Mail.MailAddress> ccList, string emailFrom, string emailSubject, string emailMessage, List<System.Net.Mail.Attachment> attachments)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

            try
            {
                //setup client
                client.Host = Configuration.ConfigManager.GetSMTPServer();

                //add all to email addresses.
                if (toList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in toList)
                    {
                        msg.To.Add(mailAddrs);
                    }
                }

                //add all cc email addresses.
                if (ccList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in ccList)
                    {
                        msg.CC.Add(mailAddrs);
                    }
                }

                if (emailFrom != null)
                {
                    msg.From = new System.Net.Mail.MailAddress(emailFrom);
                }
                else
                {
                    msg.From = new System.Net.Mail.MailAddress(Configuration.ConfigManager.GetDefaultFromAddress());
                }

                msg.Subject = emailSubject;
                msg.Body = emailMessage;

                //add attachments
                if (attachments != null)
                {
                    foreach (System.Net.Mail.Attachment attach in attachments)
                    {
                        msg.Attachments.Add(attach);
                    }
                }

                try
                {
                    client.Send(msg);
                }
                catch (Exception e)
                {
                    throw new Exception(EmailErrors.EmailError_SendEmailFailed.ToString() + " - " + (int)EmailErrors.EmailError_SendEmailFailed + " - " + e.Message);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                client.Dispose();
                msg.Dispose();
            }
        }
    }
}