我尝试使用SendGrid从我的Android应用程序发送电子邮件。由于依赖性冲突,我无法使用Danny Santiago的Sendgrid库,因此我尝试使用OKHttp。 OKHttp唯一的问题是不允许发送输入流,这是Danny Santiago的库用来发送附件的。 OKhttp将允许字节数组或文件,但发送文件似乎不起作用。这是我到目前为止所拥有的。
fun sendEmail(email: Email) {
val formBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
if (email.getAttachments().size > 0) {
val it = email.getAttachments().entries.iterator()
val MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
while (it.hasNext()) {
val entry = it.next() as Map.Entry<*, *>
formBody.addFormDataPart("File", String.format(PARAM_FILES, entry.key),
RequestBody.create(MEDIA_TYPE_JPEG,entry.value as File))
}
}
val request Request.Builder()
.url(url + endpoint)
.header("Authorization", "Bearer " + this.password)
.post(formBody.build() as RequestBody)
.build()
try {
val response = this.client.newCall(request).execute()
Timber.e("Status message", response.body())
return SendGrid.Response(response.code(), response.message())
} catch (e: IOException) {
throw SendGridException(e)
}
}
我是否必须发送字节数组输入流,还是可以通过发送字节数组来逃脱?