How to send the email through Java API in Eloqua
This post explains how to send the email through Eloqua API.Create the required model classes:
public class EmailTestDeployment{
public String contactId ;
public String sendFromUserId ;
public Email email;
public String name;
public String type;
}
public class Email
{
public int emailGroupId;
public RawHtmlContent htmlContent;
public int 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;
}
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");
System.out.println(authToken);
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 send the email:
Change the Eloqua credential accordingly.
import com.google.gson.Gson;
import models.Email;
import models.EmailGroup;
import models.EmailTestDeployment;
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 sendEmail(String contactId, String emailID,String emailGroupId) throws Exception{
try
{
Gson gson = new Gson();
EmailTestDeployment deployment = new EmailTestDeployment();
Email email = new Email();
email.name = "sampleemail1";
email.id = Integer.parseInt(emailID);
email.emailGroupId = Integer.parseInt(emailGroupId);
deployment.contactId = contactId;
deployment.email=email;
deployment.name="Email Deployment";
deployment.type = "EmailTestDeployment";
String requestBody = gson.toJson(deployment);
String sendEmailResponse = client.execute("/assets/email/deployment","POST",requestBody);
if(sendEmailResponse==null)
{
throw new Exception();
}
}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/1.0");
try {
helper.sendEmail("109", "62","1");;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Email will be send based on the subject line and email content configured in the Eloqua Email.
We can change the subject line and email content from the java code.
//Email subject
email.subject="Sample Email";
//Email content - plain text
email.sendPlainTextOnly=true;
email.plainText="Sample content";
//Email content - html
email.sendPlainTextOnly=false;
RawHtmlContent content=new RawHtmlContent();
content.html="<html><head></head><body>test</body></html>";
content.type="RawHtmlContent";
email.htmlContent=content;
No comments:
Post a Comment