Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 5

Problems with Visual Studio 2010 VB.Net and Google.APIs

Solved! Go to Solution.

I have developed a GMailAPI Class that I include in .Net Projects that need to send EMail.  Everything came together fine and I included my Class in several VB.Net Windows Applications.  My HD crashed and had to be rebuilt from the ground up (Windows 7 x64 Pro).  It seems I now have some sort of .Net version issue..?  I believe .Net 3.5 was what my platform had prior to the crash.   After the rebuild I can nor enable .Net 3.5 (Windows Features On/Off) and seem to be stuck with/on .Net 4.0  I don't know if this is the cause of my problems but now references to several Google.Apis... are failing.

Untitled.pngImports, NuGet Packages and Error List

 I've tried NuGet Package Management but haven't found a fix.... any suggestions or help greatly appreciated!

 

Here's the Visual Studio info:

Untitled01.png

 

Here's the source code (Visual Studio 2010 Pro - VB.Net) for my GMailAPI Class:

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Gmail.v1
Imports Google.Apis.Gmail.v1.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports Google.Apis.Auth.OAuth2.Flows
Imports Google.Apis.Auth.OAuth2.Responses

'   https://github.com/andyedinborough/aenetmail
Imports AE.Net.Mail

Imports System
Imports System.Data
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Imports System.Collections
Imports System.Diagnostics
Imports System.Globalization
Imports System.Resources

Imports Microsoft.Win32
Imports Microsoft.VisualBasic.ApplicationServices

Imports IBM.Data
Imports IBM.Data.DB2
Imports IBM.Data.DB2.iSeries

Public Class GMailAPI_Class
    Private _JSONFileName As String
    Public Property JSONFileName() As String
        Get
            JSONFileName = Me._JSONFileName
        End Get
        Set(value As String)
            Me._JSONFileName = value
        End Set
    End Property
    Private _JSONPath As String ' ex:   "client_secret.json"
    Public Property JSONPath() As String
        Get
            JSONPath = Me._JSONPath
        End Get
        Set(value As String)
            Me._JSONPath = value
        End Set
    End Property

    Private _From As String
    Public Property From() As String
        Get
            From = Me._From
        End Get
        Set(value As String)
            Me._From = value
        End Set
    End Property
    Private _ReplyTo As String
    Public Property ReplyTo() As String
        Get
            ReplyTo = Me._ReplyTo
        End Get
        Set(value As String)
            Me._ReplyTo = value
        End Set
    End Property
    Private _ToCSV As String
    Public Property ToCSV() As String
        Get
            ToCSV = Me._ToCSV
        End Get
        Set(value As String)
            Me._ToCSV = value
            Me._ToAL = Me.CSVtoAL(value)
        End Set
    End Property
    Private _ToAL As ArrayList
    Public Property ToAL() As ArrayList
        Get
            ToAL = Me._ToAL
        End Get
        Set(value As ArrayList)
            Me._ToAL = value
            Me._ToCSV = Me.ALtoCSV(value)
        End Set
    End Property
    Private _CcCSV As String
    Public Property CcCSV() As String
        Get
            CcCSV = Me._CcCSV
        End Get
        Set(value As String)
            Me._CcCSV = value
            Me._CcAL = Me.CSVtoAL(value)
        End Set
    End Property
    Private _CcAL As ArrayList
    Public Property CcAL() As ArrayList
        Get
            CcAL = Me._CcAL
        End Get
        Set(value As ArrayList)
            Me._CcAL = value
            Me._CcCSV = Me.ALtoCSV(value)
        End Set
    End Property
    Private _BccCSV As String
    Public Property BccCSV() As String
        Get
            BccCSV = Me._BccCSV
        End Get
        Set(value As String)
            Me._BccCSV = value
            Me._BccAL = Me.CSVtoAL(value)
        End Set
    End Property
    Private _BccAL As ArrayList
    Public Property BccAL() As ArrayList
        Get
            BccAL = Me._BccAL
        End Get
        Set(value As ArrayList)
            Me._BccAL = value
            Me._BccCSV = Me.ALtoCSV(value)
        End Set
    End Property
    Private _Subject As String
    Public Property Subject() As String
        Get
            Subject = Me._Subject
        End Get
        Set(value As String)
            Me._Subject = value
        End Set
    End Property
    Private _BodyText As String
    Public Property BodyText() As String
        Get
            BodyText = Me._BodyText
        End Get
        Set(value As String)
            Me._BodyText = value
        End Set
    End Property
    Private _AttachmentPathsCSV As String
    Public Property AttachmentPathsCSV() As String
        Get
            AttachmentPathsCSV = Me._AttachmentPathsCSV
        End Get
        Set(value As String)
            Me._AttachmentPathsCSV = value
            Me._AttachmentPathsAL = Me.CSVtoAL(value)
        End Set
    End Property
    Private _AttachmentPathsAL As ArrayList
    Public Property AttachmentPathsAL() As ArrayList
        Get
            AttachmentPathsAL = Me._AttachmentPathsAL
        End Get
        Set(value As ArrayList)
            Me._AttachmentPathsAL = value
            Me._AttachmentPathsCSV = Me.ALtoCSV(value)
        End Set
    End Property

    Private _RequestID As Object
    Public Property RequestID() As Object
        Get
            RequestID = Me._RequestID
        End Get
        Set(value As Object)
            Me._RequestID = value
        End Set
    End Property
    Private _StatusText As String
    Public Property StatusText() As String
        Get
            StatusText = Me._StatusText
        End Get
        Set(value As String)
            Me._StatusText = value
        End Set
    End Property

    Private OAuth_Scopes As String() = {GmailService.Scope.GmailCompose}
    Private OAuth_AppName As String = "GMailSendEMail"
    Private OAuth_User As String = "me"
    Private OAuth_UserCredential As Google.Apis.Auth.OAuth2.UserCredential
    Private OAuth_ClientSecrets As ClientSecrets
    Private OAuth_FileDataStore As FileDataStore
    Private OAuth_JSONPath As String = Nothing
    Private OAuth_CredentialPath As String = Nothing
    Private OAuth_CredentialsSaved As Boolean = False

    Private GMail_Service As GmailService
    Private AE_MailMessage As AE.Net.Mail.MailMessage

    Public Sub New()
        Me._From = Nothing
        Me._JSONFileName = Nothing
        Me._JSONPath = Nothing
        Me._ToCSV = Nothing
        Me._ToAL = Nothing
        Me._ReplyTo = Nothing
        Me._CcCSV = Nothing
        Me._CcAL = Nothing
        Me._BccCSV = Nothing
        Me._BccAL = Nothing
        Me._Subject = Nothing
        Me._BodyText = Nothing
        Me._AttachmentPathsCSV = Nothing
        Me._AttachmentPathsAL = Nothing

        Me._RequestID = Nothing
        Me._StatusText = "<Initialized><empty>"
    End Sub
    Public Sub New(ByVal inFrom As String, _
                   ByVal inJSONFileName As String, _
                   ByVal inJSONPath As String, _
                   ByVal inReplyTo As String, _
                   ByVal inToCSV As String, _
                   ByVal inCcCSV As String, _
                   ByVal inBccCSV As String, _
                   ByVal inSubject As String, _
                   ByVal inBodyText As String, _
                   ByVal inAttachmentPathsCSV As String)

        Me._From = inFrom
        Me._JSONFileName = inJSONFileName
        Me._JSONPath = inJSONPath
        Me._ReplyTo = inReplyTo
        Me._ToCSV = inToCSV
        Me._ToAL = CSVtoAL(inToCSV)
        Me._CcCSV = inCcCSV
        Me._CcAL = CSVtoAL(inCcCSV)
        Me._BccCSV = inBccCSV
        Me._BccAL = CSVtoAL(inBccCSV)
        Me._Subject = inSubject
        Me._BodyText = inBodyText
        Me._AttachmentPathsCSV = inAttachmentPathsCSV
        Me._AttachmentPathsAL = CSVtoAL(inAttachmentPathsCSV)

        Me._RequestID = Nothing
        Me._StatusText = "<Initialized><populated>"
    End Sub
    Public Sub New(ByVal inFrom As String, _
                   ByVal inJSONFileName As String, _
                   ByVal inJSONPath As String, _
                   ByVal inReplyTo As String, _
                   ByVal inToAL As ArrayList, _
                   ByVal inCcAL As ArrayList, _
                   ByVal inBccAL As ArrayList, _
                   ByVal inSubject As String, _
                   ByVal inBodyText As String, _
                   ByVal inAttachmentPathsAL As ArrayList)

        Me._From = inFrom
        Me._JSONFileName = inJSONFileName
        Me._JSONPath = inJSONPath
        Me._ReplyTo = inReplyTo
        Me._ToAL = inToAL
        Me._ToCSV = ALtoCSV(inToAL)
        Me._CcAL = inCcAL
        Me._CcCSV = ALtoCSV(inCcAL)
        Me._BccAL = inBccAL
        Me._BccCSV = ALtoCSV(inBccAL)
        Me._Subject = inSubject
        Me._BodyText = inBodyText
        Me._AttachmentPathsAL = inAttachmentPathsAL
        Me._AttachmentPathsCSV = ALtoCSV(inAttachmentPathsAL)

        Me._RequestID = Nothing
        Me._StatusText = "<Initialized><populated>"
    End Sub
    Public Sub New(ByVal inFrom As String, _
                   ByVal inJSONFileName As String, _
                   ByVal inJSONPath As String, _
                   ByVal inReplyTo As String, _
                   ByVal inToSA() As String, _
                   ByVal inCcSA() As String, _
                   ByVal inBccSA() As String, _
                   ByVal inSubject As String, _
                   ByVal inBodyText As String, _
                   ByVal inAttachmentPathsSA() As String)

        Me._From = inFrom
        Me._JSONFileName = inJSONFileName
        Me._JSONPath = inJSONPath
        Me._ReplyTo = inReplyTo
        Me._ToAL = SAtoAL(inToSA)
        Me._ToCSV = SAtoCSV(inToSA)
        Me._CcAL = SAtoAL(inCcSA)
        Me._CcCSV = SAtoCSV(inCcSA)
        Me._BccAL = SAtoAL(inBccSA)
        Me._BccCSV = SAtoCSV(inBccSA)
        Me._Subject = inSubject
        Me._BodyText = inBodyText
        Me._AttachmentPathsAL = SAtoAL(inAttachmentPathsSA)
        Me._AttachmentPathsCSV = SAtoCSV(inAttachmentPathsSA)

        Me._RequestID = Nothing
        Me._StatusText = "<Initialized><populated>"
    End Sub
    Public Function Send() As Integer
        Dim thisResult As Integer = 0
        Me._StatusText = "Unknown Error"
        Dim thisStep As String = Nothing

        thisStep = "Get GMail Authentication"
        Me.OAuth_CredentialsSaved = False
        Try
            ' Get GMail Authentication
            Me.OAuth_JSONPath = Path.Combine(Me._JSONPath, Me._JSONFileName)
            Using stream = New FileStream(Me.OAuth_JSONPath, FileMode.Open, FileAccess.Read)
                OAuth_CredentialPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
                OAuth_CredentialPath = Path.Combine(OAuth_CredentialPath, ".credentials/eKanbanWhite.json")

                Me.OAuth_ClientSecrets = GoogleClientSecrets.Load(stream).Secrets
                Me.OAuth_FileDataStore = New FileDataStore(OAuth_CredentialPath, True)

                Me.OAuth_UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(Me.OAuth_ClientSecrets, _
                                                                                      Me.OAuth_Scopes, _
                                                                                      Me.OAuth_User, _
                                                                                      CancellationToken.None, _
                                                                                      Me.OAuth_FileDataStore).Result

                Me.OAuth_CredentialsSaved = True
            End Using
        Catch ex As Exception
            Me._StatusText = String.Format("{0}: EMail will not be sent.{1}{1}Exception: {2}", thisStep, vbNewLine, ex.Message)
            thisResult = -9
            Return thisResult
            Exit Function
        End Try

        thisStep = "Create Gmail Email"
        Try
            ' Create Gmail API service.
            GMail_Service = New GmailService(New BaseClientService.Initializer() With {.HttpClientInitializer = Me.OAuth_UserCredential, _
                                                                                       .ApplicationName = Me.OAuth_AppName})
            ' Create the message
            '   http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/
            '
            Me.AE_MailMessage = New AE.Net.Mail.MailMessage()
            Dim messageStringWriter As New StringWriter
            Dim byteAttachment As Byte() = Nothing
            Dim AEAttachment As AE.Net.Mail.Attachment

            With Me.AE_MailMessage
                .From = New MailAddress(Me._From.Trim)
                Try
                    Dim thisReplyTo As New MailAddress(Me.ReplyTo)
                    .ReplyTo.Add(thisReplyTo)
                Catch ex As Exception

                    .ReplyTo.Add(.From)
                End Try
                .Sender = .From
                For Each thisTo As String In Me._ToAL
                    .To.Add(New MailAddress(thisTo.Trim))
                Next
                For Each thisCc As String In Me._CcAL
                    .Cc.Add(New MailAddress(thisCc.Trim))
                Next
                For Each thisBcc As String In Me._BccAL
                    .Bcc.Add(New MailAddress(thisBcc.Trim))
                Next
                .Subject = Me._Subject.Trim
                .Body = Me._BodyText
                For Each thisAttachment As String In Me._AttachmentPathsAL
                    ' Load attachment into Byte Array
                    byteAttachment = File.ReadAllBytes(thisAttachment)
                    AEAttachment = New AE.Net.Mail.Attachment(byteAttachment, _
                                                                Path.GetFileName(thisAttachment), _
                                                                Path.GetFileName(thisAttachment), _
                                                                True)
                    .Attachments.Add(AEAttachment)
                Next
                .Save(messageStringWriter)
            End With

            ' Format it
            Dim GMail_Message As New Google.Apis.Gmail.v1.Data.Message()
            With GMail_Message
                .Raw = Base64UrlEncode(messageStringWriter.ToString)
            End With

            thisStep = "Send Gmail Email"
            Dim request As Object = GMail_Service.Users.Messages.Send(GMail_Message, Me.OAuth_User).Execute()

            Me._StatusText = String.Format(If(String.IsNullOrEmpty(request.Id), _
                                              "{0}: Issue sending, returned id: {1}", _
                                              "{0}: Email looks good, id populated: {1}"), thisStep, request.Id)
            thisResult = 0

        Catch ex As Exception
            Me._StatusText = String.Format("{0}: Exception encountered: {1}", thisStep, ex.Message)
            thisResult = -9
            Return thisResult
            Exit Function
        End Try

        Return thisResult
    End Function
    ''' <summary>
    ''' Revoke Authentication
    ''' </summary>
    ''' <remarks>You should always revoke tokens on application exit</remarks>
    Public Sub RevokeToken()
        Dim thisInitializer As New GoogleAuthorizationCodeFlow.Initializer() _
                                   With {.ClientSecrets = New ClientSecrets() _
                                                              With {.ClientId = Me.OAuth_ClientSecrets.ClientId, _
                                                                    .ClientSecret = Me.OAuth_ClientSecrets.ClientSecret _
                                                                   } _
                                        }

        Dim flow As New GoogleAuthorizationCodeFlow(thisInitializer)
        Dim token As TokenResponse = flow.LoadTokenAsync(Me.OAuth_User, CancellationToken.None).Result

        thisInitializer.DataStore = OAuth_FileDataStore

        If Not (token Is Nothing) Then
            ' Revoking an expired token is not possible, we need to refresh it first.
            If token.IsExpired(flow.Clock) Then
                token = flow.RefreshTokenAsync(Me.OAuth_User, token.RefreshToken, CancellationToken.None).Result
            End If

            flow.RevokeTokenAsync(Me.OAuth_User, token.AccessToken, CancellationToken.None).Wait()
        End If

        If Directory.Exists(OAuth_CredentialPath) Then
            Directory.Delete(OAuth_CredentialPath, True)
        End If
    End Sub

    Private Function CSVtoAL(ByVal inCSV As String) As ArrayList
        Dim _comma As Char() = ","
        Dim theseValues() As String = inCSV.Split(_comma, StringSplitOptions.RemoveEmptyEntries)

        CSVtoAL = New ArrayList
        CSVtoAL.Clear()

        If inCSV.Length > 0 Then
            For Each thisValue As String In theseValues
                CSVtoAL.Add(thisValue)
            Next
        End If
    End Function
    Private Function ALtoCSV(ByVal inAL As ArrayList) As String
        ALtoCSV = Nothing

        If inAL.Count > 0 Then
            For Each thisValue As String In inAL
                ALtoCSV += String.Format("{0},", thisValue)
            Next
            ALtoCSV = ALtoCSV.Substring(0, ALtoCSV.Length - 1)
        End If
    End Function
    Private Function SAtoAL(ByVal inSA() As String) As ArrayList
        SAtoAL = New ArrayList
        SAtoAL.Clear()

        If inSA.Length > 0 Then
            For Each thisValue As String In inSA
                SAtoAL.Add(thisValue)
            Next
        End If
    End Function
    Private Function SAtoCSV(ByVal inSA() As String) As String
        SAtoCSV = Nothing

        If inSA.Length > 0 Then
            For Each thisValue As String In inSA
                SAtoCSV += String.Format("{0},", thisValue)
            Next
            SAtoCSV = SAtoCSV.Substring(0, SAtoCSV.Length - 1)
        End If
    End Function
    Private Function Base64UrlEncode(input As String) As String
        Dim inputBytes = System.Text.Encoding.UTF8.GetBytes(input)
        ' Special "url-safe" base64 encode.
        Return Convert.ToBase64String(inputBytes).Replace("+"c, "-"c).Replace("/"c, "_"c).Replace("=", "")
    End Function

End Class

 

 

4 Replies
Pilot Lvl 2
Message 2 of 5

Re: Problems with Visual Studio 2010 VB.Net and Google.APIs

See this for your .NET 3.5 access issues. Once you've made sure that .NET 3.5 is installed and accessible from VS2010, try again with your Google APIs.

Copilot Lvl 3
Message 3 of 5

Re: Problems with Visual Studio 2010 VB.Net and Google.APIs

fire-eggs:   "After the rebuild I can not enable .Net 3.5 (Windows Features On/Off) "

Pilot Lvl 2
Message 4 of 5

Re: Problems with Visual Studio 2010 VB.Net and Google.APIs

Right. Forgot that .net 35 was part of Win 7.

 

Try this instead.

Solution
Copilot Lvl 3
Message 5 of 5

Resolved (sort of): Problems with Visual Studio 2010 VB.Net and Google.APIs

For EMail I'm using the Google. APIs which I've encapsulated into a class (source code included with original posting).  This class is in turn included in Desktop application project by including the class project and adding a Reference to it.

I had a Windows 10 Virtual Machine on which I had been test-migrating VS 2010 projects up to VS 2015.  I chose for this several .Net applications including a couple that use NuGet Packages.  The Windows 10 VM is long gone but I have the VS 2015 project folders etc.   I copied my GMail Calss project from my \2015 project archive into my \2010 project folder overwriting the existing project and the errors have been cleared.  Running the application(s) in debug mode as well as installing to a client workstation function correctly....

 

That's about as deep as my insights go right now:  if there's any files or info. I can provide from either or both the failed project and the successful project LMK and I'll post them.