Gmail Java API を使用してメールを送信しようとしています。次のエラーが発生しています。
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(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:314)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1060)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
at GMailLibrary.SendEmail.sendMessage(SendEmail.java:32)
at GMailLibrary.GMailAuthentication.main(GMailAuthentication.java:85)
Java Result: 1
認証コードを使用して認証部分を実行しました。以下は、電子メールを送信するための私のコードです。
public class SendEmail
{
public static void sendMessage(Gmail service, String userId, MimeMessage email)throws MessagingException, IOException
{
Message message = createMessageWithEmail(email);
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
}
public static Message createMessageWithEmail(MimeMessage email)throws MessagingException, IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
email.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
public static MimeMessage createEmail(String to, String from, String subject, String bodyText) throws MessagingException
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
//InternetAddress tAddress = new InternetAddress(to);
//InternetAddress fAddress = new InternetAddress(from);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
public static MimeMessage createEmailWithAttachment(String to, String from, String subject,String bodyText, String fileDir, String filename) throws MessagingException, IOException
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(from);
email.setFrom(fAddress);
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(bodyText, "text/plain");
mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
mimeBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileDir + filename);
mimeBodyPart.setDataHandler(new DataHandler(source));
mimeBodyPart.setFileName(filename);
String contentType = Files.probeContentType(FileSystems.getDefault()
.getPath(fileDir, filename));
mimeBodyPart.setHeader("Content-Type", contentType + "; name=\"" + filename + "\"");
mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(mimeBodyPart);
email.setContent(multipart);
return email;
}
}
必要な情報を渡して、メイン クラスから SendMail メソッドを呼び出しています。
MimeMessage email = SendEmail.createEmail(to, from, subject, bodytext);
SendEmail.sendMessage(service, USER, email);
私を助けてください。私はすでに最善を尽くしました。さらに情報が必要な場合はお知らせください。