Stay organized with collections Save and categorize content based on your preferences.
The Mail service API for Java supports the JavaMail (javax.mail
) interface for sending email messages.
Register your sender emails as authorized senders. For more information, see who can send email.
Sending email messagesTo send email messages, use the JavaMail classes included with the App Engine SDK.
When you create a JavaMail Session, if you do not provide any SMTP server configuration, App Engine uses the Mail service for sending messages. Alternatively, add SMTP configuration for supported third-party mail providers such as Mailgun, Mailjet, or SendGrid.
To send a message:
Create a message using a JavaMail Session
object.
Create a MimeMessage
object.
To set the message sender and recipient, use the InternetAddress
class.
Identify the sender by calling the setFrom()
method on the MimeMessage
object. Optionally, you can provide a personal name as a string in the second parameter.
Identify the recipient by passing a recipient type and an address to the addRecipient()
method. The recipient type can be Message.RecipientType.TO
, Message.RecipientType.CC
or Message.RecipientType.BCC
.
The InternetAddress
constructor raises an AddressException
if the email address appears to be invalid.
To set a "reply to" address, use the setReplyTo()
method.
Establish the contents of the message by calling methods on the MimeMessage
object. Set the subject with setSubject()
and set the plaintext body content with setText()
.
To send the message, use the static method send()
on the Transport
class.
The Mail service allows you to specify a limited set of headers on outgoing email messages. For more information, see Optional headers you can use.
The following code sample demonstrates how to send mail:
Calls to the Mail service are asynchronous and return immediately. The Mail service manages the process of contacting the recipients' mail servers and delivering the message. If there is a problem sending the message to any recipient, or if a recipient's mail server returns a "bounce" message, the error message goes to the sender.
Sending multi-part messagesYou can send multi-part messages, such as a message with file attachments, or a message with a plaintext message body and an HTML message body.
To send a multi-part message:
Create a MimeMultipart
object to contain the parts, then create a MimeBodyPart
object for each attachment or alternate message body and add it to the container.'
Assign the container to the content for MimeMessage
.
The following code sample demonstrates how to send a multi-part message:
For security purposes, message parts and attachments must be of one of several allowed types, and attachment filenames must end in a recognized filename extension for the type. For a list of allowed types and filename extensions, see Mail with attachments.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["The Java Mail service API supports the `javax.mail` interface for sending email messages within first-generation runtimes, with migration options for second-generation runtimes."],["To send emails, you need to create a JavaMail `Session` object and a `MimeMessage` object, and configure the sender and recipients using the `InternetAddress` class."],["App 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."],["You can send multi-part messages, including file attachments and messages with both plaintext and HTML bodies, by using `MimeMultipart` and `MimeBodyPart` objects."],["The Mail service handles contacting recipients' mail servers and delivering messages asynchronously, with error messages or bounces going to the sender."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4