Friday, 10 May 2013

Download Attachments From Email In Outlook To Local System.


                                          Download Attachments From Email
You can download the  attached item of emails in outlook and save it  some where. Just try this
code in your local system.
     
        Dim objOutlook As New Outlook.Application
        Dim objNS As Outlook.NameSpace = objOutlook.GetNamespace("MAPI")
        Dim objInboxFolder As Outlook.MAPIFolder = Nothing
        Dim objXYZFolder As Outlook.MAPIFolder = Nothing
        Dim objFailedFolder As Outlook.MAPIFolder = Nothing
        Dim emails As Items
        Dim email As MailItem
        Dim Num_mails As Integer = 0
        Dim email_attachment As Attachment
        Dim extension As String

        objInboxFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        objXYZFolder = objInboxFolder.Folders("Valuehire")
        objFailedFolder = objXYZFolder.Folders("Failed")

        emails = objXYZFolder.Items
        Num_mails = emails.Count

        Dim MyDocumentsDir As String = vbNull
        Dim DownloadDirName As String = vbNull
       
        MyDocumentsDir = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        DownloadDirName = MyDocumentsDir + "\Outlook Files\Valuehire\Downloads"

        If Num_mails > 0 Then
            For Each email In emails
                If email.Class = OlObjectClass.olMail Then
                    For Each email_attachment In email.Attachments
                        Dim filename As String = DownloadDirName + "\" + email_attachment.FileName
                        extension = Path.GetExtension(email_attachment.FileName)

                        If (extension = ".doc" Or extension = ".docx" Or extension = ".pdf" Or extension = ".rtf") Then
                            Try
                                email_attachment.SaveAsFile(filename)                               
                                MsgBox("File is downloaded")
                                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("File is not downloaded")                                   
                                End If
                                Return
                            End Try
                        End If
                    Next email_attachment
                End If
            Next email
        End If

Notes- You can see above, in the first underline is pointed  to destination place where the file
 is store and second one for save the file in destination folder.

Friday, 3 May 2013

Connecting And Testing To A FTP Server


                            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 outlook
add_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