Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

c# - Create a draft mail using google gmail API with asp.net WebForms (without libraries)

I'm trying to use the Gmail API to create and send an email draft. I've looked the documentation and some forum threads trying to solve this problem over a week, but I don't know what I am doing wrong.

Here's the last modified code that I am using to do this job:

string URL = "https://gmail.googleapis.com/upload/gmail/v1/users/" + usrEmail + "/drafts?access_token=" + hdfToken.Value;
        string mBody = "From: " + txbMail.Text + "
To: " + txbDest.Text + "
Subject: " + txbAssunto.Text + "

" + txbMsg.Text + "";
        string payload = "{"message": {"raw": "" + Base64Encode(mBody) + ""}}";
        using (var client = new HttpClient())
        {
            var content = new StringContent(payload, Encoding.Default, "message/rfc822");

            var response = client.PostAsync($"" + URL, content).Result;
            string resultContent = response.Content.ReadAsStringAsync().Result;
            JObject jsonObject = JObject.Parse(resultContent);
            string draftID = jsonObject["id"].ToString();
            hdfID.Value = draftID;
            lblID.Text = "#ID: |" + draftID + "|<br>";
            //return resultContent;
        }

And here is the method to encode the content sent:

public static string Base64Encode(string plainText)
    {
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
        return System.Convert.ToBase64String(plainTextBytes).Replace('+', '-').Replace('/', '_').Replace("=", "");
    }

I am able to create the draft, but it always is created empty. But if I copy the content sent (string named "payload") and put it in the Google test page (https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create) the draft is created correctly -> with sender e-mail address, E-mail subject, e-mail body and receiver e-mail address I've sent. I can't figure out what I am doing wrong here, so I came here asking for your help.

This code is executed in and ASP.NET C# webforms page. Any Ideas?

***updated content below: I've created a simple form to test the API and did the form submit. Let's see the data: Simple Form created to test

On my draft folder, this is the result: Empty Draft Message created by my form

An empty draft... If I copy the content sent and paste it on the test form: Google Test API

The draft is created with the fields filled in correctly: Draft Message with filled fields

And it's driving me crazy lol.

question from:https://stackoverflow.com/questions/65846988/create-a-draft-mail-using-google-gmail-api-with-asp-net-webforms-without-librar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just found the problem. I was using a wrong url to post my data. The correct url to do this is: https://gmail.googleapis.com/gmail/v1/users/{userId}/drafts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...