質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
SendGrid

SendGridは、米SendGrid社のクラウド型メール配信サービス。アカウントを作成するだけですぐに利用することが可能です。さらに到達率向上のための送信ドメイン認証対応や、柔軟性のあるスケーラビリティなど多くの機能を有します。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

メール

メールは、コンピュータネットワークを利用し、 情報等を交換する手段のことです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

4345閲覧

SendGridで日本語でWebAPIを使ってメール送信しようとすると400番(Bad Request)エラーになってしまう。

pice

総合スコア409

SendGrid

SendGridは、米SendGrid社のクラウド型メール配信サービス。アカウントを作成するだけですぐに利用することが可能です。さらに到達率向上のための送信ドメイン認証対応や、柔軟性のあるスケーラビリティなど多くの機能を有します。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

メール

メールは、コンピュータネットワークを利用し、 情報等を交換する手段のことです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/05/23 03:31

編集2021/05/23 04:28

SendGridで日本語でWebAPIを使ってメール送信しようとすると文字化けしてしまい、400番(Bad Request)エラーになってしまいます。

System.out

1I/System.out: HTTP Exception 400 Bad Request 2 com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:84) 3 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)(中略) 4 Caused by: HTTP Exception 400 Bad Request 5 com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86) 6 Caused by: com.github.kittinunf.fuel.core.HttpException: HTTP Exception 400 Bad Request 7 com.github.kittinunf.fuel.core.requests.SuspendableRequest.prepareResponse(SuspendableRequest.kt:32) 8 com.github.kittinunf.fuel.core.requests.SuspendableRequest.awaitResult(SuspendableRequest.kt:43) 9 com.github.kittinunf.fuel.core.DeserializableKt.awaitResponseResult(Deserializable.kt:276) 10 com.example.rworksample00026.ui.dailyreports.DailyReportsSendConfirmActivity$mailSend$1$invokeSuspend$$inlined$awaitStringResponseResult$default$1.invokeSuspend(Coroutines.kt:79) 11I/System.out: kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 12 kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) 13 kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 14 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) 15 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 16 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 17I/System.out: --> POST https://api.sendgrid.com/v3/mail/send 18 Body : 19 { 20 "personalizations": [ 21 { 22 "to": [ 23 { 24 "email": "xxxxxxxx@gmail.com" 25I/System.out: } 26 ] 27 } 28 ], 29 "from": { 30 "email": "xxxxxxxxxxxxxx@gmail.com", 31 "name": "������������" 32 }, 33 "subject": "���������������", 34 "content": [ 35 { 36 "type": "text/plain", 37I/System.out: "value": "Kotlin���������������" 38 }, 39 { 40 "type": "text/html", 41 "value": "<html><body>������: 42 ���������2021/4/28 43 ��������������� 44 ----------------------- 45 ������������������������������������ 46 ���������������10 : 00���15 : 00 47 ������������������������ 48I/System.out: ��������������������� 49 ��������� 50 ��������������� 51 ������������ 52I/chatty: uid=10273(com.example.rworksample00026) identical 1 line 53I/System.out: ������������ 54 ��������������������������� 55 ��������������������������������������������������������� 56 ---------------------- 57 ��������������������������������������������������������������������� 58 ������������������ 59 ������������������������������ 60 </body></html>" 61I/System.out: } 62 ] 63 } 64 65 Headers : (2) 66 Content-Type : application/json 67 Authorization : Bearer 68xx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 69I/System.out: <-- 400 https://api.sendgrid.com/v3/mail/send 70 Response : Bad Request 71I/System.out: Length : 63 72 Body : {"errors":[{"message":"Bad Request","field":null,"help":null}]} 73 74

kotlin

1package com.example.rworksample00026.ui.dailyreports 2 3import android.content.Intent 4import android.os.Bundle 5import android.util.Log 6import android.view.View 7import androidx.activity.viewModels 8import com.example.rworksample00026.BuildConfig 9import com.example.rworksample00026.databinding.ActivityDailyReportsSendConfirmBinding 10import com.example.rworksample00026.ui.UserViewModel 11import com.example.rworksample00026.ui.util.ScopedAppActivity 12import com.github.kittinunf.fuel.core.FuelManager 13import com.github.kittinunf.fuel.coroutines.awaitStringResponseResult 14import com.github.kittinunf.fuel.httpPost 15import kotlinx.coroutines.Dispatchers 16import kotlinx.coroutines.launch 17import java.lang.StringBuilder 18import java.util.* 19import com.github.kittinunf.result.Result 20import kotlinx.coroutines.runBlocking 21 22class DailyReportsSendConfirmActivity : ScopedAppActivity() { 23 24 private lateinit var binding: ActivityDailyReportsSendConfirmBinding 25 private val viewModel: DailyReportsViewModel by viewModels() 26 private val userViewModel : UserViewModel by viewModels() 27 private var frag = "" 28 private var title = StringBuilder() 29 30 override fun onCreate(savedInstanceState: Bundle?) { 31 super.onCreate(savedInstanceState) 32 binding = ActivityDailyReportsSendConfirmBinding.inflate(layoutInflater) 33 setContentView(binding.root) 34 35 launch (Dispatchers.IO){ 36 val data = viewModel.reportsData 37 launch(Dispatchers.Main) { 38 (中略) 39 binding.nextWeekChallengeAndGoals.text = data.map{it.nextWeeksChallengeAndGoals}[0].toString() 40 41 } 42 } 43 44 val body = StringBuilder() 45 //title = StringBuilder() 46 launch (Dispatchers.IO){ 47 val data = viewModel.reportsData 48 launch(Dispatchers.Main) { 49 body.append("件名:"+ data.map{it.title}[0].toString()) 50 (中略) 51 body.append("⑴振り返り:" + data.map{it.lookingBack}[0].toString()) 52 body.append("\n") 53 body.append("⑵次週の課題・目標:" + data.map { it.nextWeeksChallengeAndGoals }[0].toString() ) 54 body.append("\n") 55 56 } 57 } 58 body.toString().trimIndent() 59 60 binding.submit.setOnClickListener (object: View.OnClickListener{ 61 override fun onClick(v: View?) { 62 63 mailSend(body) 64 Log.d("[frag]", frag) 65 66 if (frag == true.toString()) { 67 val intent = Intent(this@DailyReportsSendConfirmActivity, DailyReportsSendResultActivity::class.java) 68 startActivity(intent) 69 } else if(frag == false.toString()) { 70 71 Log.d("[mailerror]", "XXX") 72 } else { 73 Log.d("[mailerror]", "XXX") 74 } 75 } 76 }) 77 78 binding.backBtn.setOnClickListener (object: View.OnClickListener{ 79 override fun onClick(v: View?) { 80 val intent = Intent(this@DailyReportsSendConfirmActivity, RequestDocumentsConfirmActivity::class.java) 81 startActivity(intent) 82 } 83 }) 84 } 85 86 fun mailSend(body :StringBuilder) { 87 88 var to = "xxxxxxxxxxxxx@gmail.com" 89 var from = "xxxxxxxxxxxx@gmail.com" 90 var fromName = "" 91 if(userViewModel.emailList.count() >= 1){ 92 fromName = userViewModel.emailList[0].name 93 println("$fromName") 94 } 95 96 97 var content = """ 98 { 99 "personalizations": [ 100 { 101 "to": [ 102 { 103 "email": "$to" 104 } 105 ] 106 } 107 ], 108 "from": { 109 "email": "$from", 110 "name": "$fromName" 111 }, 112 "subject": "日報報告書", 113 "content": [ 114 { 115 "type": "text/plain", 116 "value": "Kotlinから送った" 117 }, 118 { 119 "type": "text/html", 120 "value": "<html><body>$body</body></html>" 121 } 122 ] 123 } 124 """ 125 126 runBlocking { 127 FuelManager.instance.baseHeaders = mapOf("Content-Type" to "application/json", "Authorization" to "Bearer ${BuildConfig.API_KEY}") 128 val (req, res, result) = "https://api.sendgrid.com/v3/mail/send".httpPost().body(content).awaitStringResponseResult() 129 //https://github.com/kittinunf/fuel/tree/master/fuel-coroutines 130 when (result) { 131 is Result.Failure -> { 132 val ex = result.getException() 133 println(ex) 134 frag = false.toString() 135 } 136 is Result.Success -> { 137 val data = result.get() 138 println(data) 139 when (res.statusCode) { 140 202 -> frag = true.toString() 141 400, 401, 413 -> frag = false.toString() 142 else -> frag = false.toString() 143 } 144 145 } 146 } 147 println(req) 148 println(res) 149 println(result) 150 151 152 } 153 154 155 } 156 157}

ちなみにcontentの部分を以下のように文字列をべた書きにすると送信はできました。

kotlin

1var content = """ 2 { 3 "personalizations": [ 4 { 5 "to": [ 6 { 7 "email": "xxxxxxxxxxx@gmail.com" 8 } 9 ] 10 } 11 ], 12 "from": { 13 "email": "xxxxxxxxxxxx@gmail.com", 14 "name": "xxxxxx" 15 }, 16 "subject": "TEST", 17 "content": [ 18 { 19 "type": "text/plain", 20 "value": "Kotlinから送った" 21 }, 22 { 23 "type": "text/html", 24 "value": "<html><body>Kotlinから送った</body></html>" 25 } 26 ] 27 } 28 """

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

例えば":(コロン)"を:ではなくそのまま":"というように、HTMLで記述すべきところをJavaの文字列だと思い込んで記述していたのが原因でした。

参考
【みんなの知識 ちょっと便利帳】使いたいときの HTML特殊 ...
丸数字(丸付き数字)「①②③④⑤」の入力方法 | コリス

投稿2021/05/23 10:27

pice

総合スコア409

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問