2016-07-26 78 views
2

我想使用Gmail API(不是JavaMail)发送电子邮件。我在论坛上看过很多类似的话题,但我仍然有问题。尝试使用Java API中的Gmail API发送电子邮件时出错

首先,我这样说的:https://developers.google.com/gmail/api/guides/sending

我impelemented方法:的MimeMessage createEmail,信息createMessageWithEmail和消息的sendMessage

然后我注意到,我没有对象Gmail服务的定义,所以我需要一个类GmailQuickstart,这是在这里:https://developers.google.com/gmail/api/quickstart/java

和我实现:类GmailQuickstart,方法凭证授权()和方法的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 = "[email protected]"; 
    String from = "[email protected]"; 
    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发送电子邮件的另一种方式吗?

+0

你在哪里提供的授权证书('client_secret.json')?错误表示您的凭据丢失或无效。 –

+0

这个文件我在同一个文件夹中,我有我的课 –

回答

4

问题是您的权限不足。这行代码的问题是:

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

如果您检查listing labels参考,你将看到许可https://www.googleapis.com/auth/gmail.labels就够了。

然而,这不足以用于sending messages。你可以改变你的SCOPES包括https://mail.google.com/同时发展,直到你知道你需要什么权限:

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

我改变了你写的行,但我有同样的错误。 –

+1

确保您删除了存储的凭据,因为这些凭据仍将使用错误的范围。 –

+1

非常感谢你! – Yster

相关问题