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;
}
}