Java 8 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Java 8 アプリケーションをデプロイできなくなります。既存の Java 8 アプリケーションは、
非推奨になる日付以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Java に移行することをおすすめします。
Mail API でメールを送信する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Java 用の Mail サービス API では、メール メッセージを送信する JavaMail(javax.mail
)インターフェースがサポートされています。
始める前に
送信者のメールアドレスを承認済み送信者として登録します。詳細については、メールを送信できるユーザーをご覧ください。
メールを送信する
メール メッセージを送信するには、App Engine SDK に含まれている JavaMail クラスを使用します。
JavaMail セッションの作成時に SMTP サーバー構成を指定しないと、App Engine は Mail サービスを使用してメッセージを送信します。または、サポートされているサードパーティのメール プロバイダ(Mailgun、Mailjet、SendGrid など)の SMTP 構成を追加します。
メールを送信するには:
JavaMail Session
オブジェクトを使用してメッセージを作成します。
MimeMessage
オブジェクトを作成します。
InternetAddress
クラスを使用して、メッセージの送信者と受信者を設定します。
MimeMessage
オブジェクトで setFrom()
メソッドを呼び出し、送信者を指定します。2 番目のパラメータに文字列として個人名を指定することもできます。
受信者の種類とアドレスを addRecipient()
メソッドに渡すことで、受信者を識別します。受信者のタイプは Message.RecipientType.TO
、Message.RecipientType.CC
、または Message.RecipientType.BCC
です。
メールアドレスが無効な場合、InternetAddress
コンストラクタが AddressException
を発生させます。
返信先のアドレスを設定するには、setReplyTo()
メソッドを使用します。
MimeMessage
オブジェクトでメソッドを呼び出して、メッセージの内容を作成します。setSubject()
で件名を設定し、setText()
で平文形式のメッセージ本文を設定します。
メッセージを送信するには、Transport
クラスの静的メソッド send()
を使用します。
Mail サービスでは、送信するメールに特定のヘッダーを使用できます。詳細については、使用可能なオプション ヘッダーをご覧ください。
以下に、メールを送信するコードサンプルを示します。
Mail サービスの呼び出しは非同期で、すぐに制御を返します。Mail サービスは、受信者のメールサーバーに接続し、メッセージを送信するプロセスを管理します。メッセージの送信で問題が発生した場合や、メールサーバーがバウンス メッセージを返した場合、エラー メッセージが送信者に表示されます。
マルチパート メッセージを送信する
ファイルが添付されたメッセージや、プレーン テキストの本文と HTML メッセージの本文から構成されるメッセージは、マルチパート メッセージとして送信できます。
マルチパート メッセージを送信するには:
パートを格納する MimeMultipart
オブジェクトを作成して、添付ファイルまたは別のメッセージ本文を格納する MimeBodyPart
オブジェクトを作成し、それをコンテナに追加します。
MimeMessage
のコンテンツにコンテナを割り当てます。
以下に、マルチパート メッセージを送信するコードサンプルを示します。
セキュリティ上の理由から、メッセージ部分と添付ファイルには許可されたタイプを使用してください。また、ファイル名の最後には、そのタイプで認識されている拡張子を付けてください。許可されるタイプとファイル名の拡張子の一覧については、添付ファイルを含むメールをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThe Java Mail service API supports the \u003ccode\u003ejavax.mail\u003c/code\u003e interface for sending email messages within first-generation runtimes, with migration options for second-generation runtimes.\u003c/p\u003e\n"],["\u003cp\u003eTo send emails, you need to create a JavaMail \u003ccode\u003eSession\u003c/code\u003e object and a \u003ccode\u003eMimeMessage\u003c/code\u003e object, and configure the sender and recipients using the \u003ccode\u003eInternetAddress\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine can use its own Mail service for sending messages if no SMTP server is configured, but it also supports third-party providers like Mailgun, Mailjet, and SendGrid.\u003c/p\u003e\n"],["\u003cp\u003eYou can send multi-part messages, including file attachments and messages with both plaintext and HTML bodies, by using \u003ccode\u003eMimeMultipart\u003c/code\u003e and \u003ccode\u003eMimeBodyPart\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eThe Mail service handles contacting recipients' mail servers and delivering messages asynchronously, with error messages or bounces going to the sender.\u003c/p\u003e\n"]]],[],null,["# Sending Mail with the Mail API\n\nThe Mail service API for Java supports the\n[JavaMail](http://www.oracle.com/technetwork/java/javamail/index.html) (`javax.mail`) interface for\nsending email messages.\n\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| java-gen2\n|\n| /services/access). If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nBefore you begin\n----------------\n\nRegister your sender emails as authorized senders. For more information, see\n[who can send email](/appengine/docs/legacy/standard/java/mail#who_can_send_mail).\n\nSending email messages\n----------------------\n\nTo send email messages, use the JavaMail classes included with the App Engine\nSDK.\n\nWhen you create a JavaMail Session, if you do not provide any SMTP server\nconfiguration, App Engine uses the Mail service for sending messages.\nAlternatively, add SMTP configuration for supported third-party mail\nproviders such as [Mailgun](/appengine/docs/legacy/standard/java/mail/mailgun),\n[Mailjet](/appengine/docs/legacy/standard/java/mail/mailjet), or\n[SendGrid](/appengine/docs/legacy/standard/java/mail/sendgrid).\n\nTo send a message:\n\n1. Create a message using a JavaMail `Session` object.\n\n2. Create a `MimeMessage` object.\n\n3. To set the message sender and recipient, use the `InternetAddress` class.\n\n 1. Identify the sender by calling the `setFrom()` method on the\n `MimeMessage` object. Optionally, you can provide a personal name as a\n string in the second parameter.\n\n 2. Identify the recipient by passing a recipient type and an address to the\n `addRecipient()` method. The recipient type can be\n `Message.RecipientType.TO`, `Message.RecipientType.CC` or\n `Message.RecipientType.BCC`.\n\n The `InternetAddress` constructor raises an `AddressException` if the email\n address appears to be invalid.\n4. To set a \"reply to\" address, use the `setReplyTo()` method.\n\n5. Establish the contents of the message by calling methods on the\n `MimeMessage` object. Set the subject with `setSubject()` and set the\n plaintext body content with `setText()`.\n\n6. To send the message, use the static method `send()` on the `Transport`\n class.\n\nThe Mail service allows you to specify a limited set of headers on outgoing\nemail messages. For more information, see [Optional headers you can\nuse](/appengine/docs/legacy/standard/java/mail/mail-with-headers-attachments#optional_headers_you_can_use).\n\nThe following code sample demonstrates how to send mail: \n\n Properties props = new Properties();\n Session session = Session.getDefaultInstance(props, null);\n\n try {\n Message msg = new MimeMessage(session);\n msg.setFrom(new InternetAddress(\"admin@example.com\", \"Example.com Admin\"));\n msg.addRecipient(Message.RecipientType.TO,\n new InternetAddress(\"user@example.com\", \"Mr. User\"));\n msg.setSubject(\"Your Example.com account has been activated\");\n msg.setText(\"This is a test\");\n Transport.send(msg);\n } catch (AddressException e) {\n // ...\n } catch (MessagingException e) {\n // ...\n } catch (UnsupportedEncodingException e) {\n // ...\n }\n\nCalls to the Mail service are asynchronous and return immediately. The Mail\nservice manages the process of contacting the recipients' mail servers and\ndelivering the message. If there is a problem sending the message to any\nrecipient, or if a recipient's mail server returns a \"bounce\" message, the error\nmessage goes to the sender.\n\nSending multi-part messages\n---------------------------\n\nYou can send multi-part messages, such as a message with file attachments, or a\nmessage with a plaintext message body and an HTML message body.\n\nTo send a multi-part message:\n\n1. Create a `MimeMultipart` object to contain the parts, then create a\n `MimeBodyPart` object for each attachment or alternate message body and add\n it to the container.'\n\n2. Assign the container to the content for `MimeMessage`.\n\nThe following code sample demonstrates how to send a multi-part message: \n\n String htmlBody = \"\"; // ...\n byte[] attachmentData = null; // ...\n Multipart mp = new MimeMultipart();\n\n MimeBodyPart htmlPart = new MimeBodyPart();\n htmlPart.setContent(htmlBody, \"text/html\");\n mp.addBodyPart(htmlPart);\n\n MimeBodyPart attachment = new MimeBodyPart();\n InputStream attachmentDataStream = new ByteArrayInputStream(attachmentData);\n attachment.setFileName(\"manual.pdf\");\n attachment.setContent(attachmentDataStream, \"application/pdf\");\n mp.addBodyPart(attachment);\n\n msg.setContent(mp);\n\nFor security purposes, message parts and attachments must be of one of several\nallowed types, and attachment filenames must end in a recognized filename\nextension for the type. For a list of allowed types and filename extensions, see\n[Mail with attachments](/appengine/docs/legacy/standard/java/mail/mail-with-headers-attachments#mail_with_attachments)."]]