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()
메서드를 호출하여 발신자를 확인합니다. 선택사항으로 두 번째 매개변수에 개인 이름을 문자열로 제공할 수 있습니다.
수신자 유형과 주소를 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
의 콘텐츠에 컨테이너를 할당합니다.
다음 샘플 코드는 여러 부분으로 구성된 메시지를 보내는 방법을 보여줍니다.
보안상의 이유로 메시지 부분과 첨부파일에는 허용된 유형을 사용해야 하며, 첨부파일 이름은 해당 유형으로 인식 가능한 파일 이름 확장자로 끝나야 합니다. 허용된 유형과 파일 이름 확장자 목록을 보려면 첨부파일이 있는 메일을 참조하세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)."]]