用于发送电子邮件无效的Javascript gmail api代码

时间:2015-10-21 15:46:13

标签: javascript gmail-api domino-designer-eclipse

1.使用此链接中的代码:Sending email from gmail api not received but shown in sent folder 2.我在Domino Designer 9本地使用domino服务器 3.确保我能够使用我的谷歌客户端ID使用Gmail API进行授权(从gmail注销,运行要求我再次登录的代码) 上述代码的修改版本无效。

我的代码或设置有什么问题。 这是完整的代码。

    // Your Client ID can be retrieved from your project in the Google
      // Developer Console, https://console.developers.google.com
   //  function assignval(cl,sc){
        // var CLIENT_ID = '261056497849-8kj87m3pjmqko8iot7kpdee2htmaf29a.apps.googleusercontent.com';
        //   var CLIENT_ID = cl;
        //var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
        //   var SCOPES = sc;
   //  }

        var CLIENT_ID = '204856067483-0ib90ohcb1itdvho93cf33pc8g83t4lp.apps.googleusercontent.com';

        var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly','https://mail.google.com/','https://www.googleapis.com/auth/gmail.modify','https://www.googleapis.com/auth/gmail.compose','https://www.googleapis.com/auth/gmail.send'];

      /**
       * Check if current user has authorized this application.
       */
        function auth() {
            var config = {
              'client_id': CLIENT_ID,
              'scope': SCOPES
            };
            gapi.auth.authorize(config, function() {
              console.log('login complete');
              console.log(gapi.auth.getToken());
            });
          }

      function checkAuth() {
        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
          }, handleAuthResult);
      }

      /**
       * Handle response from authorization server.
       *
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
         authorizeDiv.style.display = 'none';
          loadGmailApi();
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
      }

      /**
       * Initiate auth flow in response to user clicking authorize button.
       *
       * @param {Event} event Button click event.
       */
      function handleAuthClick(event) {
        gapi.auth.authorize(
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
          handleAuthResult);
        return false;
      }

      /**
       * Load Gmail API client library. List labels once client library
       * is loaded.
       */
      function loadGmailApi() {
        gapi.client.load('gmail', 'v1',send());
      }


        function sendMessage(email, callback) {
            //auth();
            gapi.client.load('gmail', 'v1',function(){
                var base64EncodedEmail = btoa(email).replace(/\//g,'_').replace(/\+/g,'-');
                  alert("Message sending" + base64EncodedEmail);
                  var request = gapi.client.gmail.users.messages.send({
                    'userId': 'me',
                    'message': {
                      'raw': base64EncodedEmail
                    }
                  });
                  request.execute(callback);
            });


        }

        function send() {
            var to = 'mohan_gangan@yahoo.com',
             subject = 'Gmail API sendmessage test',
             content = 'send a Gmail using domino server'
             var email ="From: 'me'\r\n"+
                        "To:  "+ to +"\r\n"+
                        "Subject: "+subject+"\r\n"+
                        "\r\n"+
                        content;
            alert("Message sent to Gamil API");
            sendMessage(email, function (response) {
                //console.log("Handed to Gmail API for sending");
                 {console.log(response)}
            });
            alert("Message sent");
        }

0 个答案:

没有答案