Stuck trying to send pdf with SmtpApi
VivianaGarces opened this issue · 1 comments
VivianaGarces commented
Hi, thanks for this library,
I'm trying to send a PDF through Smtp, I have a template and successfully sent an email without attachment but when I try to add the attachment it gets stuck and never reaches the send part, this is the code I'm using (Already tested that file exists):
SmtpApi apiInstance = new SmtpApi();
SendSmtpEmail sendSmtpEmail = new SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email
sendSmtpEmail.setTemplateId(1L);
SendSmtpEmailTo remitent = new SendSmtpEmailTo();
remitent.email("mytestemail@gmail.com");
sendSmtpEmail.setTo(new ArrayList(Arrays.asList(remitent)));
SendSmtpEmailAttachment attachment = new SendSmtpEmailAttachment();
try {
File f = new File(Environment.getExternalStorageDirectory() + "/someFolder/test.pdf");
InputStream fis = new FileInputStream(f);
long length = f.length();
if (length > Integer.MAX_VALUE) {
Log.e("MainActivity", "cannnottt readddd");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
attachment.setContent(bytes);
} catch (Exception ex) {
ex.printStackTrace();
}
sendSmtpEmail.setAttachment(Collections.singletonList(attachment));
try {
CreateSmtpEmail result = apiInstance.sendTransacEmail(sendSmtpEmail);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling SmtpApi#sendTransacEmail");
e.printStackTrace();
}
sendSmtpEmail.setAttachment(Collections.singletonList(attachment));
is the problematic line
VivianaGarces commented
I had to add this code to make it work, I'm leaving it here in case someone gets this issue too.
byte[] bArr = Base64.getEncoder().encodeToString(bytes).getBytes();
attachment.setName("test.pdf");
attachment.setContent(bArr);