0

I'm trying to send an email using Gmail-API, but I receive Insufficient Permission. Here is my code:

var msg = new AE.Net.Mail.MailMessage
            {
                Subject = "[ENC] " + textBox2.Text,
                From = [MyEmailHere],
                Body = textBox6.Text
            };

            msg.To.Add(new MailAddress(textBox1.Text));
            msg.ReplyTo.Add(msg.From);

 var msgStr = MimeMessage.CreateFromMailMessage(msg);

 var gmailMessage = new Google.Apis.Gmail.v1.Data.Message
            {
                Raw = Base64UrlEncode(msgStr.ToString())
            };
            UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, "me");
            request.Execute();

The exception appears in last line.

My example RAW to send:

RnJvbTogcHBhbmFraW5AZ21haWwuY29tDQpSZXBseS1UbzogcHBhbmFraW5AZ21haWwuY29tDQpUbzogcHBhbmFraW5AZ21haWwuY29tDQpTdWJqZWN0OiBbRU5DXSBhZGRzDQpDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgNCg0Kw6_Cv8K9PcOvwr_CvVrDr8K_wr0Jw6_Cv8K9w4LCtMOvwr_CvTobF8Ovwr_CvTw

Additional question: How to get my email address from credentials or authorization?

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
Triti
  • 131
  • 1
  • 8
  • *Where* do you get the exception? Have you tried debugging? Where does the debugger stop? At least post the full exception including its call stack, as returned by `Exception.ToString()`. Right now, you don't provide the information that could help find the problem – Panagiotis Kanavos Mar 14 '17 at 09:49
  • @PanagiotisKanavos his second question is valid. If I edit it can we agree to remove the duplicate? How to get my email address from credentials or authorization? – Linda Lawton - DaImTo Mar 14 '17 at 09:57
  • @DaImTo the second question should be posted as a different question. It's not valid either - it's too broad. You don't get anything from the credentials, you authenticate the user and get whatever you want from the user's profile. Where that is or how it's stored, depends on the application itself. ASP.NET offers its own user profile mechanism. Business applications typically pull such information from Active Directory. – Panagiotis Kanavos Mar 14 '17 at 09:59
  • I receive: `Object reference not set to an instance of an object` – Triti Mar 14 '17 at 09:59
  • 1
    @Triti read the duplicate please. It explains what that exception is and how to debug the problem. *Have* you tried debugging at all? Where does the debugger stop? Also, what you posted is the exception message, *not* the output of `Exception.ToString()`. The full exception includes the call stack, ie in which method and which row the exception was thrown. – Panagiotis Kanavos Mar 14 '17 at 10:02
  • I disagree with it being to broad. There are serval ways to return the current authenticated users email address. However I wonder why he would need it when sending me should work fine. Unless he intends to use the current authenticated users email address as the from in his new mail. @Triti I am currently testing your code give me a few minutes. – Linda Lawton - DaImTo Mar 14 '17 at 10:03
  • @DalmTo different questions, different posts. Besides, you can't disagree that it is too broad, then say `There are several ways to return the current authenticated users email address.`. That's the definition of `too broad`. It would require at least one full article to describe them, even if you only covered the built-in methods – Panagiotis Kanavos Mar 14 '17 at 10:04
  • @PanagiotisKanavos I see your definition of to broad is a lot more broad then mine. – Linda Lawton - DaImTo Mar 14 '17 at 10:06
  • `Exception.ToString()` shows what I told you before. The exception appears on line 115 which is the last line of code. I won't paste it because it is written in my national language – Triti Mar 14 '17 at 10:10
  • @Triti here you go http://stackoverflow.com/q/42783192/1841839 let me know if you have any issues. – Linda Lawton - DaImTo Mar 14 '17 at 10:18
  • @DalmTo `GetProfile()` works great, Thank You – Triti Mar 14 '17 at 10:28
  • I've followed your advice and modified the code. Now I get: `"The service gmail has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError Insufficient Permission [403 Errors Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global in Google.Apis.Requests.ClientServiceRequest.Execute() ` – Triti Mar 14 '17 at 10:41
  • Please [edit](http://stackoverflow.com/posts/42782477/edit) your question to include the code and error messages in the question. Do not expect people to read all the comments to understand your question. – Dour High Arch Mar 15 '17 at 01:10

0 Answers0