Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在Java中使用Gmail API发送电子邮件时出错_Java_Email_Gmail_Gmail Api - Fatal编程技术网

尝试在Java中使用Gmail API发送电子邮件时出错

尝试在Java中使用Gmail API发送电子邮件时出错,java,email,gmail,gmail-api,Java,Email,Gmail,Gmail Api,我想用Gmail API(不是JavaMail)发送电子邮件。我在论坛上读过很多类似的话题,但我仍然有一个问题 首先我读到: 我实现的方法有:mimessage createEmail、messagecreatemessagewithemail和messagesendmessage 然后我注意到我没有对象Gmail服务的定义,所以我需要一个类Gmail QuickStart,它在这里: 我实现了:类GmailQuickstart、方法credentialauthorize()和方法Gmail g

我想用Gmail API(不是JavaMail)发送电子邮件。我在论坛上读过很多类似的话题,但我仍然有一个问题

首先我读到:

我实现的方法有:mimessage createEmail、messagecreatemessagewithemail和messagesendmessage

然后我注意到我没有对象Gmail服务的定义,所以我需要一个类Gmail QuickStart,它在这里:

我实现了:类GmailQuickstart、方法credentialauthorize()和方法Gmail getGmailService()

最后我写了一篇主要的文章:

public static void main(final String[] args) throws MessagingException, IOException {
    String APPLICATION_NAME = "gmailProject";
    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    Credential credential = GmailQuickstart.authorize();

    String to = "frommail@gmail.com";
    String from = "tomail@gmail.com";
    String subject = "Subject";
    String bodyText = "Body";
    MimeMessage emailcontent = createEmail(to, from, subject, bodyText);
    createMessageWithEmail(emailcontent);
    Gmail service = new com.google.api.services.gmail.Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();
    sendMessage(service, "me", emailcontent);
}
在此之后,我有一长串错误:

Exception in thread "main"com.google.api.client.googleapis. json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
  "domain" : "global",
  "message" : "Insufficient Permission",
  "reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJs nResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at org.actuarlib.outsourcing.mail.Mail.sendMessage(Mail.java:78)
    at org.actuarlib.outsourcing.mail.Mail.main(Mail.java:195)
我知道在堆栈上有很多关于类似错误的主题,但我不知道如何纠正它。
任何人都可以告诉我出了什么问题,或者你知道使用Gmail API发送电子邮件的另一种方式吗?

问题是你没有足够的权限进行授权。这行代码就是问题所在:

private static final List<String> SCOPES =
        Arrays.asList(GmailScopes.GMAIL_LABELS);

您在哪里提供授权凭据(
client_secret.json
)?错误表明您的凭据丢失或无效。我的classI更改了您写的行的同一文件夹中的此文件,但我有相同的错误。请确保删除存储的凭据,因为这些凭据仍将使用错误的作用域。非常感谢@没问题:)很乐意帮忙。
private static final List<String> SCOPES =
        Arrays.asList(GmailScopes.MAIL_GOOGLE_COM);