Search code examples
javasparkpost

How to set From as a name in SparkPost email


I'm using Sparkpost client library for java.

When I set a fromEmailand username to contentAttributes I expect to see Name Name instead of name@mydomain.com in email I have got. But it doesn't work. Is there any way to show Name instead of Email in message?

place where I need my name instead of email address

TemplateContentAttributes contentAttributes = new TemplateContentAttributes();

    AddressAttributes addressAttributes = new AddressAttributes();
    addressAttributes.setName(username);
    addressAttributes.setEmail(fromEmail);

    contentAttributes.setFrom(addressAttributes);
    contentAttributes.setSubject(subject);
    contentAttributes.setHtml(html);
    contentAttributes.setText(text);

Solution

  • maintainer of the Java SparkPost library here. I just modified this sample.

    I changed it to this:

    TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
    AddressAttributes fromAddress = new AddressAttributes(from);
    fromAddress.setName("My Name");
    contentAttributes.setFrom(fromAddress);
    

    And this is the result enter image description here

    Is it possible "username" is an empty string. If you look at the source in your email client do you see the friendly name?

    If you are still having problem please share the JSON that is sent to the server.