通过 Gmail 发送电子邮件 API Google App Engine
Send email through Gmail API Google App Engine
正在尝试使用 Gmail API
将电子邮件发送到 GAE
。之前我在凭据页面中创建了一个服务帐户密钥,它生成了一个 .P12 file
,它位于 setServiceAccountPrivateKeyFromP12File
参数中。它有一个 ID 密钥连接到帐户 example@appspot.gserviceaccount.com
进入服务帐户页面。代码:
/* Application name. */
private static final String APPLICATION_NAME = "appnamefromappengine";
String emailAddress = "somename@appspot.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Set<String> scopes = new HashSet<String>();
scopes.add(GmailScopes.GMAIL_SEND);
scopes.add(GmailScopes.GMAIL_COMPOSE);
scopes.add(GmailScopes.MAIL_GOOGLE_COM);
scopes.add("https://www.googleapis.com/auth/admin.directory.user");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("/home/myuser/Test/src/main/webapp/resources/**somename**cd30e7118ad5.p12"))
.setServiceAccountScopes(scopes)
.setServiceAccountUser("somename@appspot.gserviceaccount.com")
.build();
Gmail gmail = new Gmail
.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("someemail@gmail.com"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("recipient@gmail.com"));
message.setSubject("Test Mail");
message.setText("Test Mail");
Message msg = createMessageWithEmail(message); //createMessageWithEmail function from Gmail API
msg = gmail.users().messages().send(emailAddress, msg).execute();
System.out.println("Mail was sent. Message id: " + msg.getId());
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它 returns 我这个错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400
Bad Request
{ "code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "failedPrecondition" } ],
"message" : "Bad Request" }
我不确定我在此处的代码或 Google 云控制台中设置了哪个参数错误。我还能尝试什么?
问题是我没有在管理控制台中配置向服务帐户委派全域权限。
Go to your Google Apps domain’s Admin console.
Select Security from the list of controls. If you don't see Security
listed, select More controls from the gray bar at the
bottom of the page, then select Security from the list of controls.
If you can't see the controls, make sure you're signed in as an
administrator for the domain.
Select Show more and then Advanced settings from the list of
options.
Select Manage API client access in the Authentication section.
In the Client Name field enter the service account's Client ID. You
can find your service account's client ID
in the
Service
accounts section of the Developers Console's Permissions page.
In the One or More API Scopes field enter the list of scopes that
your application should be granted access to.
For
example, if
your application needs domain-wide access to the Google Drive API
and the Google Calendar API, enter:
https://www.googleapis.com/auth/drive,
https://www.googleapis.com/auth/calendar.
Click Authorize.
所有文档:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
您可以选择使用像这样的库,它会为您完成所有设置:
https://github.com/3wks/spring-gae-gmail
正在尝试使用 Gmail API
将电子邮件发送到 GAE
。之前我在凭据页面中创建了一个服务帐户密钥,它生成了一个 .P12 file
,它位于 setServiceAccountPrivateKeyFromP12File
参数中。它有一个 ID 密钥连接到帐户 example@appspot.gserviceaccount.com
进入服务帐户页面。代码:
/* Application name. */
private static final String APPLICATION_NAME = "appnamefromappengine";
String emailAddress = "somename@appspot.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Set<String> scopes = new HashSet<String>();
scopes.add(GmailScopes.GMAIL_SEND);
scopes.add(GmailScopes.GMAIL_COMPOSE);
scopes.add(GmailScopes.MAIL_GOOGLE_COM);
scopes.add("https://www.googleapis.com/auth/admin.directory.user");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("/home/myuser/Test/src/main/webapp/resources/**somename**cd30e7118ad5.p12"))
.setServiceAccountScopes(scopes)
.setServiceAccountUser("somename@appspot.gserviceaccount.com")
.build();
Gmail gmail = new Gmail
.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("someemail@gmail.com"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("recipient@gmail.com"));
message.setSubject("Test Mail");
message.setText("Test Mail");
Message msg = createMessageWithEmail(message); //createMessageWithEmail function from Gmail API
msg = gmail.users().messages().send(emailAddress, msg).execute();
System.out.println("Mail was sent. Message id: " + msg.getId());
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它 returns 我这个错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400,
"errors" : [ { "domain" : "global", "message" : "Bad Request", "reason" : "failedPrecondition" } ],
"message" : "Bad Request" }
我不确定我在此处的代码或 Google 云控制台中设置了哪个参数错误。我还能尝试什么?
问题是我没有在管理控制台中配置向服务帐户委派全域权限。
Go to your Google Apps domain’s Admin console.
Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
Select Show more and then Advanced settings from the list of options.
Select Manage API client access in the Authentication section.
In the Client Name field enter the service account's Client ID. You can find your service account's client ID in the Service accounts section of the Developers Console's Permissions page.
In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
Click Authorize.
所有文档:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
您可以选择使用像这样的库,它会为您完成所有设置: https://github.com/3wks/spring-gae-gmail