How to update the html content of Email template in Eloqua through java
This post explains how to update the html content of the Eloqua email through java.Create the model class for Email:
public class Email{
public int emailGroupId;
public RawHtmlContent htmlContent;
public Integer id;
public boolean isPlainTextEditable;
public String name;
public String plainText;
public boolean sendPlainTextOnly;
public String subject;
}
public class RawHtmlContent
{
public String type;
public String html;
}
Create the html template with place holder for dynamic content and place the template in the same location of the below java classes.
sample-template.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
${content}
</body>
</html>
RestClient class to connect to Eloqua
import java.io.BufferedReader;import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class RestClient
{
private String authToken;
private String baseUrl;
public RestClient(String user, String password, String url)
{
baseUrl = url;
String authString = user + ":" + password;
authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());
}
public String execute(String uri, String method, String body) throws Exception
{
String response ="";
try
{
URL url = new URL(baseUrl + uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod(method.toString());
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", authToken);
if (method == "POST" || method == "PUT")
{
if(null != body){
conn.setDoOutput(true);
final OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
}
}
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader( is));
String line;
while ((line = rd.readLine()) != null)
{
response += line;
}
rd.close();
conn.disconnect();
}
catch (Exception e)
{
throw e;
}
return response;
}
}
The Helper class to update the email
Change the Eloqua credential accordingly.import java.io.BufferedInputStream;
import java.io.InputStream;
import models.Email;
import models.RawHtmlContent;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class EmailHelper
{
private RestClient client;
public EmailHelper(String site, String user, String password, String baseUrl)
{
client = new RestClient(site + "\\" + user, password, baseUrl);
}
public void updateHtmlContent(String emailId,String content) throws Exception{
try {
String response = null;
InputStream stream = EmailHelper.class.getResourceAsStream("sample-template.html");
StringBuffer buffer = new StringBuffer();
BufferedInputStream input = new BufferedInputStream(stream);
int data = input.read();
while (data != -1) {
buffer.append((char) data);
data = input.read();
}
String templateString = buffer.toString();
templateString = templateString.replace("${content}", content);
System.out.println("templateString"+templateString);
Gson gson = new GsonBuilder().serializeNulls().create();
response = client.execute("/assets/email/"+emailId,"GET",null);
if(null!=response){
Email email = gson.fromJson(response, Email.class);
RawHtmlContent rawHtmlContent = new RawHtmlContent();
rawHtmlContent.type = "RawHtmlContent";
rawHtmlContent.html = templateString;
email.htmlContent = rawHtmlContent;
String requestbody = gson.toJson(email);
response = client.execute("/assets/email/"+emailId,"PUT", requestbody);
if(null == response){
throw new Exception("Not able to update the Template");
}
}
} catch (Exception e) {
throw e;
}
}
public static void main(String[] args) {
EmailHelper helper=new EmailHelper("companyname", "username", "password", "https://secure.p03.eloqua.com/api/rest/2.0");
try {
helper.updateHtmlContent("73","Albsample1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I figured it out. I originally had used a contact field called "City" and the values I was testing with were not valid. So I removed that field, then recreated a new field. I also found out that the ID for the field was not "0" and was instead a larger number like "2403" which I found from doing a RestAPI query to get the data.
ReplyDeleteHere is what the format ended up being.
{"fieldValues":[{"id":2403,"value":"yyyyyy","type":"FieldValue"}],"type":"FormData"}
In the event that you have any pre-dispatch, you can utilize email showcasing to tell your clients ahead of time and develop the reckoning before the real dispatch. Building reckoning before a major dispatch is an extremely helpful advertising strategy which you can do it with email showcasing. Besides, you can even offer rebates and exceptional offers to your endorsers of further improve your association with them. https://dcit.newcastle.edu.au/education/2016/09/22/the-most-popular-email-services-in-the-world/
ReplyDeletebonkers.
DeleteThe tech support team of Systechconnect have experience and knowledge of common problems that occur with Mac mail and Juno mail services. So, if it is simple problem then it gets sorted out quickly.
ReplyDeleteextract emails