Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin x SendGridでメール送信してみた

kikutaro
October 23, 2017

Kotlin x SendGridでメール送信してみた

2017.10.23
#m3kt どこでもKotlin #3 秋のLT大会

kikutaro

October 23, 2017
Tweet

More Decks by kikutaro

Other Decks in Programming

Transcript

  1. FuelManager.instance.baseHeaders = mapOf("Content-Type" to "application/json", "Authorization" to "Bearer *****") "https://api.sendgrid.com/v3/mail/send".httpPost().body((content)).response

    { req, res, result -> when (result) { is Result.Failure -> { println("APIのコールに失敗") } is Result.Success -> { when (res.httpStatusCode) { 202 -> println("メール送信成功") 400, 401, 413 -> println("SendGridから400番台のエラーコードが返ってきた") else -> println("想定していないコードが返ってきた") } } } }
  2. FuelManager.instance.baseHeaders = mapOf("Content-Type" to "application/json", "Authorization" to "Bearer *****") "https://api.sendgrid.com/v3/mail/send".httpPost().body((content)).response

    { req, res, result -> when (result) { is Result.Failure -> { println("APIのコールに失敗") } is Result.Success -> { when (res.httpStatusCode) { 202 -> println("メール送信成功") 400, 401, 413 -> println("SendGridから400番台のエラーコードが返ってきた") else -> println("想定していないコードが返ってきた") } } } } val content = """ { "personalizations": [ { "to": [ { "email": "$to" } ] } ], "from": { "email": "$from", "name": "$fromName" }, "subject": "Kotlinからの送信テスト(HTTP)", "content": [ { "type": "text/plain", "value": "Kotlinから送った" }, { "type": "text/html", "value": "<html><body>Kotlinから送った </body></html>" } ] } """
  3. val from = Email("[email protected]") val subject = "Sending with SendGrid

    is Fun" val to = Email("[email protected]") val content = Content("text/plain", "and easy to do anywhere, even with Kotlin") val mail = Mail(from, subject, to, content) val sg = SendGrid("*****") val request = Request() try { request.method = Method.POST request.endpoint = "mail/send" request.body = mail.build() val response = sg.api(request) //略 } catch (ex: IOException) { throw ex }