尝试使用Java中的Gmail API发送电子邮件时出错 [英] Error when trying to send an email using Gmail API in Java
问题描述
我想使用Gmail API(而不是JavaMail)发送电子邮件。我在论坛上阅读了很多类似的主题,但我仍然有一个问题。
I want to send an email using Gmail API (not JavaMail). I read many similar topics on forums, but I have still a problem.
首先我读到: https://developers.google.com/gmail/api/guides/sending
并且我引用了方法:MimeMessage createEmail,Message createMessageWithEmail和Message sendMessage
and I impelemented methods: MimeMessage createEmail, Message createMessageWithEmail and Message sendMessage
然后我注意到我没有对象Gmail服务的定义,所以我需要一个类GmailQuickstart,在这里: https://developers.google.com/gmail/api/quickstart/java
Then I noticed that I don't have definition of object Gmail service, so I needed a class GmailQuickstart, which is here: https://developers.google.com/gmail/api/quickstart/java
我实现了:类GmailQuickstart,方法Credential Authorize()和方法Gmail getGmailService()
and I implemented: class GmailQuickstart, method Credential authorize() and method Gmail getGmailService()
最后我写了一个main:
Finally I wrote a main:
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发送电子邮件的另一种方式?
I know that on stack there are many topics about similar error, but I don't know how to correct it. Anyone can tell me what's wrong or do you know another way to send an email using Gmail API?
推荐答案
问题是您在授权不足的情况下进行授权。这行代码是问题:
The problem is that you have authorized with insufficient permission. This line of code is the problem:
private static final List<String> SCOPES =
Arrays.asList(GmailScopes.GMAIL_LABELS);
如果您查看列出标签,您将看到权限 https://www.googleapis.com /auth/gmail.labels
是足够的。
If you check the reference for listing labels, you will see that the permission https://www.googleapis.com/auth/gmail.labels
is enough.
然而,这不足以用于发送消息。您可以在开发过程中更改您的SCOPES以包含 https://mail.google.com/
,直到您确切知道所需的权限:
This is however not sufficient for sending messages. You can change your SCOPES to include https://mail.google.com/
while developing, until you know exactly what permission you need:
private static final List<String> SCOPES =
Arrays.asList(GmailScopes.MAIL_GOOGLE_COM);
这篇关于尝试使用Java中的Gmail API发送电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!