{"id":3060,"date":"2020-11-13T11:30:00","date_gmt":"2020-11-13T02:30:00","guid":{"rendered":"https:\/\/ec2-54-238-200-5.ap-northeast-1.compute.amazonaws.com\/blog\/?p=3060"},"modified":"2025-04-03T16:17:47","modified_gmt":"2025-04-03T07:17:47","slug":"sendgrid%e3%81%8b%e3%82%89%e6%90%ba%e5%b8%af%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e5%90%91%e3%81%91%e3%81%ab%e3%83%a1%e3%83%bc%e3%83%ab%e3%82%92%e9%80%81%e4%bf%a1%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/sendgrid.kke.co.jp\/blog\/?p=3060","title":{"rendered":"SendGrid\u304b\u3089\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u5411\u3051\u306b\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2020\/11\/2dbba63b65be5b241a1087c427d95864.png\" alt=\"SendGrid\u304b\u3089\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u5411\u3051\u306b\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u65b9\u6cd5\" width=\"100%\" class=\"noshadow aligncenter size-full wp-image-13292\" srcset=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2020\/11\/2dbba63b65be5b241a1087c427d95864.png 592w, https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2020\/11\/2dbba63b65be5b241a1087c427d95864-300x185.png 300w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/p>\n<h4>\u306f\u3058\u3081\u306b<\/h4>\n<p>\u300cSendGrid\u306f\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\uff1f\u300d\u3088\u304f\u3053\u3093\u306a\u304a\u554f\u3044\u5408\u305b\u3092\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\uff01<br \/>\nSendGrid\u306f\u7c73\u56fd\u767a\u306e\u30b5\u30fc\u30d3\u30b9\u3067\u3059\u304c\u3001\u65e5\u672c\u306e\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u306b\u3082\u554f\u984c\u306a\u304f\u30e1\u30fc\u30eb\u3092\u5c4a\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3067\u3082\u3001\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u306b\u306f\u69d8\u3005\u306a\u5236\u9650\u304c\u3042\u308b\u305f\u3081\u3001\u4e00\u822c\u7684\u306a\u30e1\u30fc\u30eb\u30d7\u30ed\u30d0\u30a4\u30c0\u306b\u9001\u4fe1\u3059\u308b\u306e\u3068\u6bd4\u3079\u3066\u3001\u6c17\u3092\u3064\u3051\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3053\u3068\u304c\u5897\u3048\u3066\u3057\u307e\u3046\u306e\u3082\u4e8b\u5b9f\u3067\u3059\u3002\u305d\u3053\u3067\u4eca\u56de\u306f\u3001\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u5b9b\u3066\u306b\u30e1\u30fc\u30eb\u3092\u9001\u308b\u969b\u306b\u6c17\u3092\u3064\u3051\u308b\u3079\u304d\u30dd\u30a4\u30f3\u30c8\u3068\u3001Java\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u9001\u4fe1\u3059\u308b\u305f\u3081\u306e\u5177\u4f53\u7684\u306a\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h4>\u524d\u63d0\u6761\u4ef6<\/h4>\n<p>\u4eca\u56de\u3001\u52d5\u4f5c\u78ba\u8a8d\u306b\u4f7f\u7528\u3057\u305f\u74b0\u5883\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30c9\u30b3\u30e2\uff1aSH-02K\u3001SO-51A(Android 10)<\/li>\n<li>au\uff1aW62K<\/li>\n<li>\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\uff1a202SH<\/li>\n<\/ul>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8a66\u3059\u524d\u306b\u4ee5\u4e0b\u306e\u74b0\u5883\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u5b8c\u4e86\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<ul>\n<li>Java 1.8<\/li>\n<li>Gradle 6.7<\/li>\n<li>git<\/li>\n<li><a href=\"https:\/\/sendgrid.kke.co.jp\/?p=signup.form\" rel=\"noopener\" target=\"_blank\">SendGrid\u30a2\u30ab\u30a6\u30f3\u30c8<\/a><\/li>\n<\/ul>\n<h4>\u30e1\u30fc\u30eb\u306e\u5f62\u5f0f\uff08\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u3068\u30c7\u30b3\u30e1\uff09<\/h4>\n<p>\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u304c\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u30e1\u30fc\u30eb\u306e\u5f62\u5f0f\u306b\u306f\u300c\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u300d\u306e\u4ed6\u3001\u3044\u308f\u3086\u308b\u300c\u30c7\u30b3\u30e1\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u30c7\u30b3\u30e1\u306f\u30c7\u30b3\u30e1\u30fc\u30eb\u3084\u3001\u30c7\u30b3\u30ec\u30e1\u30fc\u30eb\u306a\u3069\u5404\u793e\u3067\u547c\u3073\u540d\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb<br \/>\n\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u306f\u672c\u6587\u304ctext\/plain\u3067\u69cb\u6210\u3055\u308c\u308b\u6700\u3082\u30b7\u30f3\u30d7\u30eb\u306a\u30e1\u30fc\u30eb\u3067\u3059\u3002<\/li>\n<li>\u30c7\u30b3\u30e1<br \/>\n\u30c7\u30b3\u30e1\u306f\u672c\u6587\u304c\u30c6\u30ad\u30b9\u30c8\u30d1\u30fc\u30c8\u3068HTML\u30d1\u30fc\u30c8\u304b\u3089\u69cb\u6210\u3055\u308c\u308b\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u30e1\u30fc\u30eb\u306e\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002\u30d1\u30fc\u30c8\u306e\u69cb\u9020\u306f\u5404\u793e\u3054\u3068\u306b\u4ed5\u69d8\u304c\u7570\u306a\u308b\u305f\u3081\u3001\u30ad\u30e3\u30ea\u30a2\u3054\u3068\u306b\u69cb\u9020\u3092\u5909\u3048\u3066\u9001\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u753b\u50cf\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\u3082\u30ad\u30e3\u30ea\u30a2\u3054\u3068\u306b\u7570\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<h4>\u9001\u4fe1\u65b9\u6cd5\u3068\u6587\u5b57\u30b3\u30fc\u30c9<\/h4>\n<p>SendGrid\u3067\u306f<a href=\"https:\/\/sendgrid.kke.co.jp\/docs\/Tutorials\/A_Transaction_Mail\/send_transaction_mail.html#-Send-Via-Web-Api\" rel=\"noopener\" target=\"_blank\">Web API<\/a>\u3001<a href=\"https:\/\/sendgrid.kke.co.jp\/docs\/Tutorials\/A_Transaction_Mail\/send_transaction_mail.html#-Send-Via-Smtp\" rel=\"noopener\" target=\"_blank\">SMTP<\/a>\u3001<a href=\"https:\/\/sendgrid.kke.co.jp\/docs\/Tutorials\/B_Marketing_Mail\/marketing_campaigns0.html\" rel=\"noopener\" target=\"_blank\">\u30de\u30fc\u30b1\u30c6\u30a3\u30f3\u30b0\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u6a5f\u80fd<\/a>\u306e3\u3064\u306e\u65b9\u6cd5\u3067\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002Web API\u3068\u30de\u30fc\u30b1\u30c6\u30a3\u30f3\u30b0\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u6a5f\u80fd\u306f\u3001\u7c21\u5358\u306b\u9001\u4fe1\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u69cb\u9020\u3092\u81ea\u7531\u306b\u5909\u66f4\u3067\u304d\u306a\u3044\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u65b9\u3001SMTP\u306f\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u69cb\u9020\u3092\u81ea\u7531\u306b\u6307\u5b9a\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u306e\u5206\u8a2d\u5b9a\u304c\u96e3\u3057\u304f\u306a\u308a\u307e\u3059\u3002\u30c7\u30b3\u30e1\u306e\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u69cb\u9020\u306b\u5bfe\u5fdc\u3057\u305f\u30e1\u30fc\u30eb\u3092\u9001\u308b\u306b\u306fSMTP\u3067\u9001\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u6587\u5b57\u30b3\u30fc\u30c9\u306b\u3064\u3044\u3066\u306f\u3001Web API\u3068\u30de\u30fc\u30b1\u30c6\u30a3\u30f3\u30b0\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u6a5f\u80fd\u306e\u5834\u5408\u3001UTF-8\u306e\u307f\u6307\u5b9a\u53ef\u80fd\u3067\u3059\u3002SMTP\u306e\u5834\u5408\u3001UTF-8\u3001ISO-2022-JP\u306a\u3069\u304c\u6307\u5b9a\u53ef\u80fd\u3067\u3059\u304c\u3001SendGrid\u5074\u3067\u81ea\u52d5\u7684\u306bUTF-8\u307e\u305f\u306fus-ascii\u306b\u5909\u63db\u3057\u3066\u9001\u4fe1\u3057\u307e\u3059\u3002<\/p>\n<table border=1 style=\"padding:8px;border:solid 1px #555555;\">\n<thead style=\"background-color:#333333;\">\n<tr>\n<th style=\"padding:8px;\"><b>\u5f62\u5f0f<\/b><\/th>\n<th style=\"padding:8px;\"><b>\u9001\u4fe1\u65b9\u6cd5<\/b><\/th>\n<th style=\"padding:8px;\"><b>\u6587\u5b57\u30b3\u30fc\u30c9<\/b><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th style=\"padding:8px;\" rowspan=\"3\"><b>\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb<\/b><\/th>\n<td style=\"padding:8px;\">SMTP<\/td>\n<td style=\"padding:8px;\">UTF-8\u3001ISO-2022-JP\u3001Shift_JIS\u306a\u3069\u304c\u6307\u5b9a\u53ef\u80fd\u3060\u304c\u3001\u9001\u4fe1\u6642\u306b\u81ea\u52d5\u7684\u306bUTF-8\u307e\u305f\u306fus-ascii\u306b\u5909\u63db\u3055\u308c\u308b<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:8px;\">Web API<\/td>\n<td style=\"padding:8px;\">UTF-8\u306e\u307f<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:8px;\">\u30de\u30fc\u30b1\u30c6\u30a3\u30f3\u30b0\u30e1\u30fc\u30eb\u6a5f\u80fd<\/td>\n<td style=\"padding:8px;\">UTF-8\u306e\u307f<\/td>\n<\/tr>\n<tr>\n<th style=\"padding:8px;\"><b>\u30c7\u30b3\u30e1<\/b><\/th>\n<td style=\"padding:8px;\">SMTP<\/td>\n<td style=\"padding:8px;\">UTF-8\u3001ISO-2022-JP\u3001Shift_JIS\u306a\u3069\u304c\u6307\u5b9a\u53ef\u80fd\u3060\u304c\u3001\u9001\u4fe1\u6642\u306b\u81ea\u52d5\u7684\u306bUTF-8\u307e\u305f\u306fus-ascii\u306b\u5909\u63db\u3055\u308c\u308b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u6587\u5b57\u5316\u3051<\/h4>\n<p>\u65e5\u672c\u8a9e\u30e1\u30fc\u30eb\u3092\u9001\u308b\u969b\u3001\u6c17\u306b\u306a\u308b\u306e\u306f\u6587\u5b57\u30b3\u30fc\u30c9\u3068\u6587\u5b57\u5316\u3051\u3067\u3059\u304c\u3001\u4eca\u56de\u78ba\u8a8d\u3057\u305f\u6a5f\u7a2e\u3067\u306f\u3044\u305a\u308c\u3082\u6587\u5b57\u5316\u3051\u305b\u305a\u306b\u5c4a\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u5f8c\u8ff0\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u6587\u5b57\u5316\u3051\u304c\u767a\u751f\u3059\u308b\u5834\u5408\u3001\u53d7\u4fe1\u5074\u306e\u643a\u5e2f\u96fb\u8a71\u306e\u5236\u9650\u306e\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<a href=\"https:\/\/qiita.com\/awwa\/items\/0eb6a5e7f9722ffe4579\" rel=\"noopener\" target=\"_blank\">\u3053\u3061\u3089<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u5207\u308a\u5206\u3051\u3092\u884c\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h4>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h4>\n<p>\u4eca\u56de\u306fJavaMail\u3092\u4f7f\u3063\u3066<a href=\"https:\/\/github.com\/SendGridJP\/sendgrid-smtp-java-example\" rel=\"noopener\" target=\"_blank\">\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/a>\u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u65b9\u6cd5\u306f<a href=\"https:\/\/github.com\/SendGridJP\/sendgrid-smtp-java-example\/blob\/master\/README.md\" rel=\"noopener\" target=\"_blank\">README<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f9d\u5b58\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/java.net\/projects\/javamail\/pages\/Home\" rel=\"noopener\" target=\"_blank\">JavaMail<\/a> 1.6.2<\/li>\n<li><a href=\"https:\/\/github.com\/sendgrid\/smtpapi-java\" rel=\"noopener\" target=\"_blank\">smtpapi-java<\/a>\u30001.2.6<\/li>\n<\/ul>\n<p>\u8a73\u3057\u304f\u306f\u30ea\u30dd\u30b8\u30c8\u30ea\u5185\u306ebuild.gradle\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5404\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u3088\u308a\u8a73\u3057\u3044\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u306f\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h4>\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h4>\n<p>\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002\u5b9b\u540d\u306e\u5dee\u3057\u8fbc\u307f\u3068\u30ab\u30c6\u30b4\u30ea\u306e\u6307\u5b9a\u306e\u307f\u884c\u3063\u305f\u30b7\u30f3\u30d7\u30eb\u306a\u5185\u5bb9\u3067\u3059\u3002\u8907\u6570\u306e\u5b9b\u5148\u3092\u6307\u5b9a\u3057\u3066\u4e00\u6589\u9001\u4fe1\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.github.sendgridjp;\r\n\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.util.List;\r\nimport java.util.Properties;\r\nimport javax.mail.Authenticator;\r\nimport javax.mail.Message;\r\nimport javax.mail.MessagingException;\r\nimport javax.mail.PasswordAuthentication;\r\nimport javax.mail.Session;\r\nimport javax.mail.Transport;\r\nimport javax.mail.internet.MimeMessage;\r\nimport javax.mail.internet.MimeUtility;\r\nimport javax.mail.internet.InternetAddress;\r\nimport io.github.cdimascio.dotenv.Dotenv;\r\n\r\nimport com.sendgrid.smtpapi.SMTPAPI;\r\n\r\npublic class JavaMailTextExample {\r\n  \/\/ \u8a2d\u5b9a\u60c5\u5831\r\n  static Dotenv dotenv = Dotenv.load();\r\n  static final String USERNAME = dotenv.get(&quot;SENDGRID_USERNAME&quot;);\r\n  static final String PASSWORD = dotenv.get(&quot;SENDGRID_PASSWORD&quot;);\r\n  static final String&#x5B;] TOS    = dotenv.get(&quot;TOS&quot;).split(&quot;,&quot;);\r\n  static final String&#x5B;] NAMES  = dotenv.get(&quot;NAMES&quot;).split(&quot;,&quot;);\r\n  static final String FROM     = dotenv.get(&quot;FROM&quot;);\r\n  static final String SUBJECT  = dotenv.get(&quot;SUBJECT&quot;);\r\n  static final String CHARSET  = dotenv.get(&quot;CHARSET&quot;);\r\n  static final String ENCODE   = dotenv.get(&quot;ENCODE&quot;);\r\n\r\n  public static void send()\r\n  throws MessagingException, UnsupportedEncodingException {\r\n    \/\/ SMTP\u63a5\u7d9a\u60c5\u5831\r\n    Properties props = new Properties();\r\n    props.put(&quot;mail.transport.protocol&quot;, &quot;smtps&quot;);\r\n    props.put(&quot;mail.smtp.host&quot;, &quot;smtp.sendgrid.net&quot;);\r\n    props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);\r\n    props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);\r\n    props.put(&quot;mail.smtp.starttls.required&quot;, &quot;true&quot;);\r\n    props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);\r\n    Authenticator auth = new SMTPAuthenticator();\r\n    Session mailSession = Session.getDefaultInstance(props, auth);\r\n    mailSession.setDebug(true); \/\/ console log for debug\r\n\r\n    \/\/ \u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u69cb\u7bc9\r\n    MimeMessage message = new MimeMessage(mailSession);\r\n\r\n    \/\/ \u30c0\u30df\u30fc\u306e\u5b9b\u5148(X-SMTPAPI\u306e\u5b9b\u5148\u304c\u512a\u5148\u3055\u308c\u308b)\r\n    message.addRecipient(Message.RecipientType.TO, new InternetAddress(FROM));\r\n\r\n    \/\/ From\r\n    message.setFrom(FROM);\r\n\r\n    \/\/ Subject\r\n    message.setSubject(SUBJECT, CHARSET);\r\n\r\n    \/\/ Body\r\n    String body = &quot;\u3053\u3093\u306b\u3061\u306f\u3001name\u3055\u3093\\r\\n\u3088\u3046\u3053\u305d\u301c\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u306e\u4e16\u754c\u3078\uff01&quot;;\r\n    message.setText(body, CHARSET, &quot;plain&quot;);\r\n    message.setHeader(&quot;Content-Transfer-Encoding&quot;, ENCODE);\r\n\r\n    \/\/ X-SMTPAPI\u30d8\u30c3\u30c0\r\n    String smtpapi = createSmtpapi(TOS, NAMES);\r\n    smtpapi = MimeUtility.encodeText(smtpapi);\r\n    message.setHeader(&quot;X-SMTPAPI&quot;, MimeUtility.fold(76, smtpapi));\r\n\r\n    \/\/ \u9001\u4fe1\r\n    mailSession.getTransport().send(message);\r\n  }\r\n\r\n  \/\/ X-SMTPAPI\u30d8\u30c3\u30c0\u306b\u8a2d\u5b9a\u3059\u308b\u5024\u306e\u751f\u6210\r\n  private static String createSmtpapi(String&#x5B;] tos, String&#x5B;] names) {\r\n    SMTPAPI smtpapi = new SMTPAPI();\r\n    smtpapi.setTos(tos);\r\n    smtpapi.addSubstitutions(&quot;name&quot;, names);\r\n    smtpapi.addCategory(&quot;category1&quot;);\r\n    return smtpapi.rawJsonString();\r\n  }\r\n\r\n  private static class SMTPAuthenticator extends javax.mail.Authenticator {\r\n    public PasswordAuthentication getPasswordAuthentication() {\r\n      return new PasswordAuthentication(USERNAME, PASSWORD);\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<h4>\u30c7\u30b3\u30e1\u3092\u9001\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h4>\n<p>\u6b21\u306f\u30c7\u30b3\u30e1\u3092\u9001\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\u3002\u30ad\u30e3\u30ea\u30a2\u306b\u4f9d\u5b58\u3057\u306a\u3044\u3088\u3046\u5b9b\u5148\u30c9\u30e1\u30a4\u30f3\u3054\u3068\u306b\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u306e\u69cb\u9020\u3092\u5207\u308a\u66ff\u3048\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.github.sendgridjp;\r\n\r\nimport java.io.IOException;\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.nio.file.Path;\r\nimport java.nio.file.FileSystems;\r\nimport java.nio.file.Files;\r\n\r\nimport java.util.Properties;\r\nimport javax.activation.DataHandler;\r\nimport javax.activation.DataSource;\r\nimport javax.mail.Authenticator;\r\nimport javax.mail.Message;\r\nimport javax.mail.MessagingException;\r\nimport javax.mail.PasswordAuthentication;\r\nimport javax.mail.Session;\r\nimport javax.mail.Transport;\r\nimport javax.mail.Multipart;\r\nimport javax.mail.internet.MimeMessage;\r\nimport javax.mail.internet.MimeBodyPart;\r\nimport javax.mail.internet.MimeMultipart;\r\nimport javax.mail.internet.MimeUtility;\r\nimport javax.mail.internet.InternetAddress;\r\nimport javax.mail.util.ByteArrayDataSource;\r\nimport io.github.cdimascio.dotenv.Dotenv;\r\n\r\nimport com.sendgrid.smtpapi.SMTPAPI;\r\n\r\npublic class JavaMailDecoExample {\r\n  \/\/ \u8a2d\u5b9a\u60c5\u5831\r\n  static Dotenv dotenv = Dotenv.load();\r\n  static final String USERNAME = dotenv.get(&quot;SENDGRID_USERNAME&quot;);\r\n  static final String PASSWORD = dotenv.get(&quot;SENDGRID_PASSWORD&quot;);\r\n  static final String&#x5B;] TOS    = dotenv.get(&quot;TOS&quot;).split(&quot;,&quot;);\r\n  static final String&#x5B;] NAMES  = dotenv.get(&quot;NAMES&quot;).split(&quot;,&quot;);\r\n  static final String FROM     = dotenv.get(&quot;FROM&quot;);\r\n  static final String SUBJECT  = dotenv.get(&quot;SUBJECT&quot;);\r\n  static final String CHARSET  = dotenv.get(&quot;CHARSET&quot;);\r\n  static final String ENCODE   = dotenv.get(&quot;ENCODE&quot;);\r\n\r\n  public static void send()\r\n  throws IOException, MessagingException, UnsupportedEncodingException {\r\n    \/\/ SMTP\u63a5\u7d9a\u60c5\u5831\r\n    Properties props = new Properties();\r\n    props.put(&quot;mail.transport.protocol&quot;, &quot;smtps&quot;);\r\n    props.put(&quot;mail.smtp.host&quot;, &quot;smtp.sendgrid.net&quot;);\r\n    props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);\r\n    props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);\r\n    props.put(&quot;mail.smtp.starttls.required&quot;, &quot;true&quot;);\r\n    props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);\r\n    Authenticator auth = new SMTPAuthenticator();\r\n    Session mailSession = Session.getDefaultInstance(props, auth);\r\n    mailSession.setDebug(true); \/\/ console log for debug\r\n\r\n    \/\/ \u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u69cb\u7bc9\r\n    MimeMessage message = new MimeMessage(mailSession);\r\n\r\n    \/\/ \u30c0\u30df\u30fc\u306e\u5b9b\u5148(X-SMTPAPI\u306e\u5b9b\u5148\u304c\u512a\u5148\u3055\u308c\u308b)\r\n    message.addRecipient(Message.RecipientType.TO, new InternetAddress(FROM));\r\n\r\n    \/\/ From\r\n    message.setFrom(FROM);\r\n\r\n    \/\/ Subject\r\n    message.setSubject(SUBJECT, CHARSET);\r\n\r\n    \/\/ Body\r\n    \/\/ Root part\r\n    MimeMultipart rootPart = new MimeMultipart();\r\n    if (TOS&#x5B;0].contains(&quot;@ezweb.ne.jp&quot;)) {\r\n      rootPart.setSubType(&quot;mixed&quot;);\r\n    } else {\r\n      rootPart.setSubType(&quot;related&quot;);\r\n    }\r\n    \/\/ Alternative part\r\n    MimeMultipart altPart = new MimeMultipart();\r\n    altPart.setSubType(&quot;alternative&quot;);\r\n    \/\/ Text part\r\n    String body = &quot;\u3053\u3093\u306b\u3061\u306f\u3001name\u3055\u3093\\r\\n\u3088\u3046\u3053\u305d\u301c\u30c6\u30ad\u30b9\u30c8\u30e1\u30fc\u30eb\u306e\u4e16\u754c\u3078\uff01&quot;;\r\n    MimeBodyPart textBodyPart = new MimeBodyPart();\r\n    textBodyPart.setText(body, CHARSET, &quot;plain&quot;);\r\n    textBodyPart.setHeader(&quot;Content-Transfer-Encoding&quot;, ENCODE);\r\n    altPart.addBodyPart(textBodyPart);\r\n    \/\/ Html part\r\n    String htmlBody =\r\n      &quot;&lt;html&gt;&quot; +\r\n      &quot;&lt;body bgcolor=\\&quot;#d9edf7\\&quot; style=\\&quot;background-color: #d9edf7;\\&quot;&gt;&quot; +\r\n      &quot;\u3053\u3093\u306b\u3061\u306f\u3001name\u3055\u3093&lt;br&gt;\u3088\u3046\u3053\u305d\u301c\u30c7\u30b3\u30e1\u30fc\u30eb\u306e\u4e16\u754c\u3078\uff01&lt;br&gt;&quot; +\r\n      &quot;&lt;img src=\\&quot;cid:123@456\\&quot;&gt;&quot; +\r\n      &quot;&lt;\/body&gt;&lt;\/html&gt;&quot;;\r\n    MimeBodyPart htmlBodyPart = new MimeBodyPart();\r\n    htmlBodyPart.setText(htmlBody, CHARSET, &quot;html&quot;);\r\n    htmlBodyPart.setHeader(&quot;Content-Transfer-Encoding&quot;, ENCODE);\r\n    altPart.addBodyPart(htmlBodyPart);\r\n    \/\/ Alternative part\r\n    MimeBodyPart altBodyPart = new MimeBodyPart();\r\n    altBodyPart.setContent(altPart);\r\n    rootPart.addBodyPart(altBodyPart, 0);\r\n    \/\/ Attachment part\r\n    MimeBodyPart attachmentPart = getMimeBodyPart(\r\n      &quot;.\/src\/main\/resources\/SG_Twilio_Lockup_RGBx2_small.gif&quot;, &quot;SG_Twilio_Lockup_RGBx2_small.gif&quot;, &quot;image\/gif&quot;, &quot;123@456&quot;\r\n    );\r\n    rootPart.addBodyPart(attachmentPart);\r\n\r\n    message.setContent(rootPart);\r\n\r\n    \/\/ X-SMTPAPI\u30d8\u30c3\u30c0\r\n    String smtpapi = createSmtpapi(TOS, NAMES);\r\n    smtpapi = MimeUtility.encodeText(smtpapi);\r\n    message.setHeader(&quot;X-SMTPAPI&quot;, MimeUtility.fold(76, smtpapi));\r\n\r\n    \/\/ \u9001\u4fe1\r\n    mailSession.getTransport().send(message);\r\n  }\r\n\r\n  \/\/ File\u304b\u3089MimeBodyPart\u3092\u751f\u6210\r\n  private static MimeBodyPart getMimeBodyPart(\r\n    String path, String name, String type, String cid\r\n  )\r\n  throws IOException, MessagingException, UnsupportedEncodingException {\r\n    MimeBodyPart attachment = new MimeBodyPart();\r\n    byte&#x5B;] bytes = Files.readAllBytes(FileSystems.getDefault().getPath(path));\r\n    DataSource dataSource = new ByteArrayDataSource(bytes, type);\r\n    DataHandler dataHandler = new DataHandler(dataSource);\r\n    attachment.setDataHandler(dataHandler);\r\n    attachment.setFileName(MimeUtility.encodeWord(name));\r\n    attachment.setContentID(&quot;&lt;&quot; + cid + &quot;&gt;&quot;);\r\n    if (TOS&#x5B;0].contains(&quot;@docomo.ne.jp&quot;)) {\r\n      attachment.setDisposition(MimeBodyPart.INLINE);\r\n    } else {\r\n      attachment.setDisposition(MimeBodyPart.ATTACHMENT);\r\n    }\r\n    return attachment;\r\n  }\r\n\r\n  \/\/ X-SMTPAPI\u30d8\u30c3\u30c0\u306b\u8a2d\u5b9a\u3059\u308b\u5024\u306e\u751f\u6210\r\n  private static String createSmtpapi(String&#x5B;] tos, String&#x5B;] names) {\r\n    SMTPAPI smtpapi = new SMTPAPI();\r\n    smtpapi.setTos(tos);\r\n    smtpapi.addSubstitutions(&quot;name&quot;, names);\r\n    smtpapi.addCategory(&quot;category1&quot;);\r\n    return smtpapi.rawJsonString();\r\n  }\r\n\r\n  \/\/ SMTP\r\n  private static class SMTPAuthenticator extends javax.mail.Authenticator {\r\n    public PasswordAuthentication getPasswordAuthentication() {\r\n      return new PasswordAuthentication(USERNAME, PASSWORD);\r\n    }\r\n  }\r\n<\/pre>\n<h4>\u307e\u3068\u3081<\/h4>\n<p>\u4eca\u56de\u306f\u3001SendGrid\u3092\u4f7f\u3063\u3066\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u5411\u3051\u306b\u30e1\u30fc\u30eb\u3092\u5c4a\u3051\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u4ed6\u306b\u3082\u3001\u8ff7\u60d1\u30e1\u30fc\u30eb\u30d5\u30a3\u30eb\u30bf\u306b\u95a2\u3057\u3066\u6c17\u3092\u3064\u3051\u308b\u3079\u304d\u3053\u3068\u3068\u3057\u3066\u3001<a href=\"https:\/\/sendgrid.kke.co.jp\/blog\/?p=3269\" rel=\"noopener\" target=\"_blank\">\u6982\u8981\u7de8<\/a>\u3068<a href=\"https:\/\/sendgrid.kke.co.jp\/blog\/?p=10875\" rel=\"noopener\" target=\"_blank\">\u306a\u308a\u3059\u307e\u3057\u898f\u5236\u7de8<\/a>\u306e\u8a18\u4e8b\u304c\u3042\u308b\u306e\u3067\u30c1\u30a7\u30c3\u30af\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h4>\u3055\u3044\u3054\u306b<\/h4>\n<p>\u4eca\u56de\u3054\u7d39\u4ecb\u3057\u305f\u30b3\u30fc\u30c9\u306e\u3046\u3061\u3001\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u69cb\u9020\u306e\u5207\u308a\u66ff\u3048\u3068\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u57cb\u3081\u8fbc\u307f\u90e8\u5206\u306f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u69d8\u306e\u30d6\u30ed\u30b0\u8a18\u4e8b\u300c<a href=\"http:\/\/dev.classmethod.jp\/cloud\/amazon-ses-deco-mail\/\" rel=\"noopener\" target=\"_blank\">Amazon SES\u3092\u4f7f\u3063\u3066\u56fd\u5185\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u5bfe\u5fdc\u306e\u30c7\u30b3\u30e1\u30fc\u30eb\u3092\u9001\u308b<\/a>\u300d\u306e\u5185\u5bb9\u3092\u53c2\u8003\u306b\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3057\u305f\u3002<\/p>\n<h4>\u53c2\u8003\u8a18\u4e8b<\/h4>\n<ul>\n<li><a href=\"https:\/\/www.nttdocomo.co.jp\/service\/developer\/make\/content\/deco_mail\/\" rel=\"noopener\" target=\"_blank\">NTT docomo \u30c7\u30b3\u30e1\u30fc\u30eb<\/a><\/li>\n<li><a href=\"https:\/\/www.au.com\/mobile\/service\/decoration\/\" rel=\"noopener\" target=\"_blank\">au \u30c7\u30b3\u30ec\u30fc\u30b7\u30e7\u30f3\u30e1\u30fc\u30eb<\/a><\/li>\n<li><a href=\"http:\/\/creation.mb.softbank.jp\/mc\/tech\/tech_mail\/mail_decore.html\" rel=\"noopener\" target=\"_blank\">\u30bd\u30d5\u30c8\u30d0\u30f3\u30af \u30c7\u30b3\u30ec\u30e1\u30fc\u30eb<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u300cSendGrid\u306f\u643a\u5e2f\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\uff1f\u300d\u3088\u304f\u3053\u3093\u306a\u304a\u554f\u3044\u5408\u305b\u3092\u3044\u305f\u3060\u304d\u307e\u3059\u3002 \u3082\u3061\u308d\u3093\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\uff01 SendGrid\u306f\u7c73\u56fd\u767a\u306e\u30b5\u30fc\u30d3\u30b9\u3067\u3059\u304c\u3001\u65e5\u672c\u306e\u30ad\u30e3\u30ea\u30a2\u30e1\u30fc\u30eb\u306b\u3082\u554f\u984c\u306a\u304f\u30e1\u30fc\u30eb\u3092\u5c4a\u3051 <a class=\"read-more\" href=\"https:\/\/sendgrid.kke.co.jp\/blog\/?p=3060\">&#8230;\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":6366,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-3060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3060"}],"version-history":[{"count":7,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3060\/revisions"}],"predecessor-version":[{"id":16597,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3060\/revisions\/16597"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/media\/6366"}],"wp:attachment":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}