本文介紹了在將郵件發(fā)送為中添加其他電子郵件地址的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我想使用Google Gmail API在Send mail as部分添加另一個(gè)電子郵件地址。我試過這個(gè)代碼,有人能幫幫我嗎?
SendAs primaryAlias = null;
ListSendAsResponse aliases = service.users().settings().sendAs().list("me").execute();
for (SendAs alias: aliases.getSendAs()) {
if (alias.getIsPrimary()) {
primaryAlias = alias;
break;
}
}
System.out.println("Updated signature for " + primaryAlias.getDisplayName());
SendAs aliasSettings = new SendAs().setSignature("I heart cats.")
.setSendAsEmail("j.ohnast.baby@gmail.com");
SendAs r = service.users().settings().sendAs().create("me", aliasSettings).execute();
System.out.println("Updated signature for " + r.getDisplayName());
線程”Main”中出現(xiàn)異常
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Access restricted to service accounts that have been delegated domain-wide authority",
"reason" : "forbidden"
} ],
"message" : "Access restricted to service accounts that have been delegated domain-wide authority"
}
推薦答案
別名只能由具有全域權(quán)限的服務(wù)帳號(hào)使用Users.settings.sendAs: create
創(chuàng)建,可以從documentation中撤回。
請(qǐng)參閱here如何創(chuàng)建服務(wù)帳戶。您需要?jiǎng)?chuàng)建creadentials并將其存儲(chǔ)在驅(qū)動(dòng)器上,例如作為.p12
文件。
以下是如何在Java中設(shè)置服務(wù)帳戶的示例:
public class ServiceAccountJava
{
private static final String APPLICATION_NAME = "XXX";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List<String> SCOPES = Collections.singletonList(XXX);
public static void main(String... args) throws IOException, GeneralSecurityException
{
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
File pk12=new File("XXX.p12");
String serviceAccount="XXX@XXX-XXX.iam.gserviceaccount.com";
// Builder necessary for the ability to refresh tokens
GoogleCredential getCredentials = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccount)
.setServiceAccountPrivateKeyFromP12File(pk12) //RESOURCES_CLIENT_SECRETS_JSON, SRC_RESOURCES_KEY_P12
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser("XXX") //the user whom you want to impersonate
.build();
...
}
}
這篇關(guān)于在將郵件發(fā)送為中添加其他電子郵件地址的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,