title: How to send Outlook messages by javascript SDK?
body: <p>I searched for a long time but I did not find a relevant solution.  </p>
  
  <p>When I send mail via Microsoft Graph API to get <code>403</code>, I do not know where the problem is </p>
  
  <pre class="lang-json prettyprint-override"><code>{
      statusCode: 403, 
      code: "ErrorAccessDenied", 
      message: "Access is denied. Check credentials and try again."
  }
  </code></pre>
  
  <p>Code:    </p>
  
  <p>makeSilentTokenRequest</p>
  
  <pre class="lang-js prettyprint-override"><code>//The static key authentication request
  function makeSilentTokenRequest(callback) {
  
      // Build up a hidden iframe
      var iframe = $('&lt;iframe/&gt;');
      iframe.attr('id', 'auth-iframe');
      iframe.attr('name', 'auth-iframe');
      iframe.appendTo('body');
      iframe.hide();
  
      iframe.load(function () {
          callback(localStorage.accessToken);
      });
  
      iframe.attr('src', buildAuthUrl() + '&amp;prompt=none&amp;domain_hint=' +
          localStorage.userDomainType + '&amp;login_hint=' +
          localStorage.userSigninName);
  
  }
  </code></pre>
  
  <p>getAccessToken     </p>
  
  <pre class="lang-js prettyprint-override"><code>//Verify refresh token function
  function getAccessToken(callback) {
      var now = new Date().getTime();
      var isExpired = now &gt; parseInt(localStorage.tokenExpires);
  
      // Do we have a token already?
      if (localStorage.accessToken &amp;&amp; !isExpired) {
          // Just return what we have
          if (callback) {
              callback(localStorage.accessToken);
          }
      } else {
          // Attempt to do a hidden iframe request
          makeSilentTokenRequest(callback);
      }
  }
  </code></pre>
  
  <p>getUserEmailAddress</p>
  
  <pre class="lang-js prettyprint-override"><code>function getUserEmailAddress(callback) {
      //I saved it in localStorage
      if (localStorage.userEmail) {
          return localStorage.userEmail;
      } else {
          //getAccessToken
          getAccessToken(function (accessToken) {
              if (accessToken) {
                  // Create a Graph client
                  var client = MicrosoftGraph.Client.init({
                      authProvider: (done) =&gt; {
                          // Just return the token
                          done(null, accessToken);
                      }
                  });
                  // Get the Graph /Me endpoint to get user email address
                  client
                      .api('/me')
                      .get((err, res) =&gt; {
                          if (err) {
                              callback(null, err);
                          } else {
                              callback(res.mail);
                          }
                      });
              } else {
                  var error = {
                      responseText: 'Could not retrieve access token'
                  };
                  callback(null, error);
              }
          });
      }
  }
  </code></pre>
  
  <p>sendMail</p>
  
  <pre class="lang-js prettyprint-override"><code>//sendMail
  function sendMail(message) {
      getUserEmailAddress(function (userEmail, error) {
          if (error) {
              renderError('getUserEmailAddress failed', error.responseText);
          } else {
              getAccessToken(function (accessToken) {
                  if (accessToken) {
                      // Create a Graph client
                      var client = MicrosoftGraph.Client.init({
                          authProvider: (done) =&gt; {
                              // Just return the token
                              done(null, accessToken);
                          }
                      });
                      client
                          .api('/me/sendMail')
                          .header('Authorization', 'Bearer ' + accessToken)
                          .header('Content-Type', "application/json")
                          .post(message, (err, res) =&gt; {
                              if (err) {
                                  console.log(err);
                                  //callback(null, err);
                              } else {
                                  console.log(res);
                                  //callback(res.value);
                              }
                          });
                  } else {
                      var error = {
                          responseText: 'Could not retrieve access token'
                      };
                      //callback(null, error);
                  }
              });
          }
      })
  }    
  </code></pre>
  
  <p>createMail    </p>
  
  <pre><code>function createMail(message,callback){
          getUserEmailAddress(function(userEmail, error) {
              if (error) {
                  renderError('getUserEmailAddress failed', error.responseText);
              } else {
                  getAccessToken(function(accessToken) {
                      if (accessToken) {
                          // Create a Graph client
                          var client = MicrosoftGraph.Client.init({
                              authProvider: (done) =&gt; {
                                  // Just return the token
                                  done(null, accessToken);
                              }
                          });
                          client
                              .api('/me/messages')
                              .header('Authorization', 'Bearer ' +accessToken)
                              .header('Content-Type', "application/json")
                              .post(message,(err, res) =&gt; {
                                  if (err) {
                                      callback(res);
                                      console.log("创建失败！");
                                  } else {
                                      callback(res);
                                      console.log("创建成功！");
                                  }
                              });
                      } else {
                          var error = { responseText: 'Could not retrieve access token' };
                          //callback(null, error);
                      }
                  });
              }
      })
      }    
  </code></pre>