sendgrid/java-http-client

Attachment Name does not show Norwegian Characters

PrinceVarshney opened this issue · 26 comments

Issue Summary

I am new to SendGrid, I wanted to send a mail with attachment having Norwegian characters in the name. Receiver is receiving the mail but the name contains Å instead of Ø. Saw the Client class , it contains the change for UTF characters

sendgrid-java v3.1.0 is being used. Not using the java-http-client. I visited few related issues but was not able to get which api version include this change. Its urgent for me as we are going in production in two weeks.

Please help!

Hello @PrinceVarshney,

I believe you are asking for us to update the dependency in sendgrid-java to support v4.1.0 of this library. Correct?

If so, here is the ticket for that: sendgrid/sendgrid-java#162 -- I will add your vote to that issue to grant it higher priority.

With Best Regards,

Elmer

Hei @thinkingserious
I am not able to get why its converting Ø -->Å , because the Client.java class has the UTF-8 support then why I am facing the issue.. ? Is this because of the dependency thats not included in sendgrid-java v3.1.0 ,I can externally include the dependency of httpClient if its required. But will it help. ? I need a urgent fix as we are moving in production in few days.

Please help!!

Hello @PrinceVarshney,

I'm not sure what is causing that issue, I am attempting to clarify with you.

Let me make sure my assumptions are correct:

  1. You are using the sendgrid-java SDK v3.1.0.
  2. I think that a similar issue was fixed with this PR: #5

Also, can you please provide me with the sample file so that I can try to re-create the issue?

Thanks!

Elmer

@thinkingserious

Yeah I am using sendgrid-java SDK v3.1.0.

We are creating runtime content and the name of the pdf visible to the user is
Pris og oppgjørsvilkår.pdf

that should be
Pris og oppgjørsvilkår.pdf

This is where I am attaching the file

Attachments attachments=new Attachments();
Charset.forName("UTF-8").encode(attachmentName);
attachments.setFilename(attachmentName);
attachments.setType(type);
attachments.setDisposition("attachment");
attachments.setContent(Base64.encodeBase64String(attachment));
List attachList = new ArrayList();
attachList.add(attachments);
emailDTO.setAttachment(attachList);

While sending it through sendgrid :
Attached the file for the reference-
sampleFile.txt

Hello @PrinceVarshney,

I was unable to reproduce the error, here is the code I used:

import com.sendgrid.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Example {

  public static Mail buildHelloEmail() throws IOException {
    Email from = new Email("dx@sendgrid.com");
    String subject = "Hello World from the SendGrid Java Library";
    Email to = new Email("elmer.thomas@sendgrid.com");
    Content content = new Content("text/plain", "some text here");
    Mail mail = new Mail(from, subject, to, content);

    Attachments attachments = new Attachments();
    attachments.setContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12");
    attachments.setType("application/pdf");
    attachments.setFilename("Pris og oppgjørsvilkår.pdf");
    attachments.setDisposition("attachment");
    attachments.setContentId("Balance Sheet");
    mail.addAttachments(attachments);

    return mail;
  }

  public static void baselineExample() throws IOException {
    SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
    Request request = new Request();
    Mail helloWorld = buildHelloEmail();
    try {
      request.method = Method.POST;
      request.endpoint = "mail/send";
      request.body = helloWorld.build();
      Response response = sg.api(request);
      System.out.println(response.statusCode);
    } catch (IOException ex) {
      throw ex;
    }
  }

  public static void main(String[] args) throws IOException {
    baselineExample();
  }
}

Perhaps the value in attachmentName is getting changed before you pass it into the setFilename function.

Hey @thinkingserious
I hardcoded the value in the attachment name, then also I was facing the same error. You are using the same version of sendgrid-java. ?

@PrinceVarshney,

Yes, I am using the latest version 3.1.0.

Can you check the encoding on your source code editor?

@thinkingserious

Will it matter. ? I think you are talking about eclipse. ?

Whats the use of below line ?

sg.addRequestHeader("X-Mock", "true");

That line can be ignored, it is for when we use our local testing server. I just tried without that line with the same result.

For Eclipse: http://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse

My request in the logs is printed correctly :
"type":"application/pdf","filename":"Pris og oppgjørsvilkår.pdf","disposition":"attachment"}

so does the eclipse encoding will matter.. ?

I'm not sure.

Can you please try sending your test email to elmer.thomas@sendgrid.com?

@thinkingserious

I have sent one mail on this id. One thing I observed that the issue is with MicrosoftOutlook only, in gmail its fine.

Hello @PrinceVarshney,

I have received the email correctly in GMail. Since the filename is correct on your server and in GMail, it appears the issue is with Outlook and that it might be a known problem.

I'm going to leave this issue open for further investigation. Perhaps there is some header that Microsoft Outlook requires to ensure UTF-0 encoding.

Hey @thinkingserious

I need to have some workaround for the same, as most of the clients of our projects are using Outlook only.

Hello @PrinceVarshney,

I have added this to our backlog for further review.

Hi @PrinceVarshney,

Can you please try:

attachments.setFilename("=?utf-8?Q?Pris=20og=20oppgj=C3=B8rsvilk=C3=A5r.pdf?=");

Thanks!

Elmer

Hi @thinkingserious

Will try this and will inform you regarding the same.

Thanks!!

Hey @thinkingserious

This is not working. This is giving =utf-8QPris=20og=20oppgj=C3=B8rsvilk=C3=A5r.pdf

Thanks

Hello @PrinceVarshney,

What version of Outlook are you using?

@PrinceVarshney,

Can you email me with your Outlook email address so I can try sending a test to you?

@thinkingserious
version of outlook : 14.0.7177.5000

@thinkingserious
I have sent you a mail

@thinkingserious

Hey Its working

I attached this MimeUtility.encodeText("string", "UTF-8", "Q")
while setting the attachmentName :

mail.setAttachmentName(MimeUtility.encodeText("Pris og oppgjørsvilkår.pdf", "UTF-8", "Q"))

First we have to encode it as per Q-encoded
Thanks

@PrinceVarshney,

That's awesome and thanks for sharing the solution! I'm going to keep this ticket open so that we can add this to the SDK.

For your efforts, we would like to offer you some swag. Please fill out this form for us :)

@thinkingserious

Thank you !! Will fill the form once outside office network :)

Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!