Search code examples
c#google-drive-apigmailgmail-api

C# Sending gmail with uploading large file on Google drive attachment


I need to create a program in c# that can upload to google drive and attach it to email(gmail) and send it to recipients. The file is more than 50mb that is why we need google drive to send. Is there a google api available to do those task all in one? Is that possible? I have been reading solutions here but mostly uploading file to google drive and sending email are separate solutions and I am having a hard time combining those two task. Can somebody direct me if there is a proper code or api to do those task.

The program will provide the following information

sender email: username

sender email: password

upload directory and file

recipient or email receiver


Solution

  • @Mandz Yeah, I'm not sure if there is an automatic way of attaching the file to the Gmail's e-mail, however, the way Gmail handles links is quite interesting: enter image description here

    As you can see, it attaches the link to the attachment, making it in some way automatic. Here's the link to the GitHub repository: link

    I've decided to make it GUI using WPF and MVVM pattern (using Caliburn.Micro). Hopefully, it won't be too difficult to run the sample app on your end. Here are the steps that you need to do in order to run the app:

    1. Have a Google account.
    2. Login into https://console.developers.google.com/ (It is a platform where you handle all your tokens etc.)
    3. Create a new project - top left, click on Select a project, new window should appear, click on New Project, name it whatever you want, when that's done, click on Navigation menu (top left), APIs & Services -> Library, Search for Google Drive and Gmail API, enable them both. Then go to Credentials (In APIs & Services), click Create credentials, choose OAuth client ID, you might need to configure consent screen (but I think it's pretty easy, you have to choose either Internal or External and provide the Application Name), in Create OAuth client ID, choose Other, name it whatever you want, Click Ok on the new window, then Download JSON on Credentials Page (on the right), place the file in the same dictionary where the .exe app is. Then create another credential for Gmail token (the same way as above I believe). Place it again in the same dictionary where the .exe app is.
    4. To run the app, you will need .NET Core 3.1 https://dotnet.microsoft.com/download (or just new version of Visual Studio)
    5. Build the app, Click the Choose button to select a file, then Click Upload button.
    6. If the tokens are in the same dictionary, a new website should launch and ask you to log in to an appropriate account (I would choose the same Gmail account where you created these tokens) then if you give access to this app, agree to that. When that's done, a token folder should appear in the same dictionary as the .exe file. The reason why the token folder is created is so you won't have to repeat the 6th step.
    7. A downloadable link should appear, copy it and paste in the Text field in the E-Mail tab.
    8. Type the E-mail address (I would suggest your alt account or something so you can just test it).
    9. A subject field can be empty.
    10. Click Send-Email (now just redo the 6th step)

    Some side notes:

    1. I had to create two tokens, for Drive and Gmail APIs, I dunno if it's the quickest way of doing this, I tried to just use 1 token but it didn't want to work.
    2. MainView.xaml is just the things that you see, so it doesn't really matter in this case.
    3. MainViewModel.cs is the file where all the important things for you are.
    4. I would look at UploadFileAsync method, it does upload to the root folder (the main folder of Google Drive), then puts the file to the public so anyone can access the file and download it, then gets the public link to the file.
    5. SendEmail method sends the E-Mail.
    6. I put these methods asynchronous so the app won't just freeze (hopefully it won't be a problem for you).
    7. The code is not perfect but I think it will do as learning material.

    Feel free to post any questions about it, below. Good luck.

    Edit: If you want link like this instead of full link, you change in string plainText to "<p><a href=\"https://www.youtube.com/\">test</a><p>"