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

81945c2e0a52ef471ae5d48b0d929712?s=47 kikutaro
October 23, 2017

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

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

81945c2e0a52ef471ae5d48b0d929712?s=128

kikutaro

October 23, 2017
Tweet

Transcript

  1. None
  2. ❖ ❖ ❖

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. 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("想定していないコードが返ってきた") } } } }
  16. 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>" } ] } """
  17. val from = Email("test@example.com") val subject = "Sending with SendGrid

    is Fun" val to = Email("test@example.com") 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 }
  18. None
  19. None