0

我正在尝试使用 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)
}
}

我是否必须发送一个字节数组输入流,或者我可以发送一个字节数组吗?

4

0 回答 0