Connection To FTP Server From Vs2010
In this blog i discuss on connection for ftp server, here is a small code ,i tried this in outlookadd_ins with successful result.But before try this code you need server ftp address and path
where file is located.
Dim ftpRequest As System.Net.FtpWebRequest
Dim clsStream As System.IO.Stream
Dim bFile() As Byte
Dim AccountName_var As String
Dim Password_var As String
Dim email_attachment As Attachments
Dim filename As String = DownloadDirName + "\" + email_attachment.FileName
Try
email_attachment.SaveAsFile(filename)
ftpRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + "xxx.xxx.xxx.xx" + "/" +
EmailID_var + "/" + email_attachment.FileName), System.Net.FtpWebRequest)
ftpRequest.Credentials = New System.Net.NetworkCredential(AccountName_var, Password_var)
ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
bFile = System.IO.File.ReadAllBytes(filename)
clsStream = ftpRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
Catch ex As System.Exception
If ex.Message.Contains("530") Then
MsgBox("Your account is invalid. Please try again or contact your Administrator.")
Else
MsgBox(ex.Message)
End Try
Here the 'filename' is name of file with the file's path and 'xxx.xxx.xxx.xx' is replace by the ip address.
Testing The Connection To FTP Server
After the connection we just test the connection,is it working or not. you see the highlighted
yellow line ,this is the simple way for check the ftp connection.
Dim ValuehireRegKey As RegistryKey
Dim AccountName_var As String
Dim Password_var As String
Dim EmailID_var As String
ValuehireRegKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\
Outlook\Addins\Valuehire", True)
AccountName_var = ValuehireRegKey.GetValue("Account")
EmailID_var = ValuehireRegKey.GetValue("Email")
Password_var = ValuehireRegKey.GetValue("Password")
ValuehireRegKey.Close()
Dim ftpRequest As System.Net.FtpWebRequest
ftpRequest = DirectCast(System.Net.WebRequest.Create("ftp://118.139.182.77/" +
EmailID_var + "/"), System.Net.FtpWebRequest)
ftpRequest.Credentials = New System.Net.NetworkCredential(AccountName_var, Password_var)
ftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
Try
ftpRequest.GetResponse()
Return True
Catch ex As System.Exception
If ex.Message.Contains("530") Then
MsgBox("Your account is invalid. Please try again or contact your Administrator.")
Dim sf As New SettingsForm
sf.ShowDialog()
Else
If ex.Message.Contains("Unable to connect to the remote server") Then
MsgBox("Could not connect to the server. Please try again or contact your Administrator.")
Else
MsgBox(ex.Message)
End If
End If
Return False
End Try
End Functionb
Dim AccountName_var As String
Dim Password_var As String
Dim EmailID_var As String
ValuehireRegKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\
Outlook\Addins\Valuehire", True)
AccountName_var = ValuehireRegKey.GetValue("Account")
EmailID_var = ValuehireRegKey.GetValue("Email")
Password_var = ValuehireRegKey.GetValue("Password")
ValuehireRegKey.Close()
Dim ftpRequest As System.Net.FtpWebRequest
ftpRequest = DirectCast(System.Net.WebRequest.Create("ftp://118.139.182.77/" +
EmailID_var + "/"), System.Net.FtpWebRequest)
ftpRequest.Credentials = New System.Net.NetworkCredential(AccountName_var, Password_var)
ftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
Try
ftpRequest.GetResponse()
Return True
Catch ex As System.Exception
If ex.Message.Contains("530") Then
MsgBox("Your account is invalid. Please try again or contact your Administrator.")
Dim sf As New SettingsForm
sf.ShowDialog()
Else
If ex.Message.Contains("Unable to connect to the remote server") Then
MsgBox("Could not connect to the server. Please try again or contact your Administrator.")
Else
MsgBox(ex.Message)
End If
End If
Return False
End Try
End Functionb
No comments:
Post a Comment