Java
Chilkat2 Java
Outlook Send Email using MIME Format
See more Outlook Examples
This example sends an email using MIME format via the Microsoft Graph Outlook REST API.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// Use your previously obtained access token here:
// See the following examples for getting an access token:
// Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
// Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
// Refresh Access Token (Azure AD v2.0 Endpoint).
// Refresh Access Token (Azure AD Endpoint).
JsonObject jsonToken = new JsonObject();
try {
jsonToken.loadFile("qa_data/tokens/microsoftGraph.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
http.setAuthToken(jsonToken.stringOf("access_token"));
// To send email, we'll POST to the following endpoint:
//
// POST /users/{id | userPrincipalName}/sendMail
//
// (The special keyword "me" may be used in place of a principal name.)
//
// The body of the POST request will contain the MIME source of the email in base64 format.
// Create a new email object
Email email = new Email();
email.setSubject("Test Outlook API to Send HTML Email with Attachments");
email.setFrom("Mary <mary@somewhere.com>");
email.addTo("Joe", "joe@example.com");
// Add a plain-text alternative body, which will likely never be seen.
// (It is shown if the receiving email client is incapable of displaying HTML email.)
email.addPlainTextAlternativeBody("This is a plain-text alternative body...");
// Our HTML will include an image, so add the related image here.
String contentIdStarfish;
try {
contentIdStarfish = email.addRelatedFile("qa_data/jpg/starfish.jpg");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The src attribute for the image tag is set to the contentIdStarfish:
StringBuilder sbHtml = new StringBuilder();
sbHtml.append("<html><body><p>This is an HTML email with an embedded image.</p>");
sbHtml.append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
int numReplacements = sbHtml.replace("CONTENT_ID_STARFISH", contentIdStarfish);
email.addHtmlAlternativeBody(sbHtml.getAsString());
// Finally, add some attachments to the email.
// Add a file attachment.
try {
email.addFileAttachment2("qa_data/pdf/fishing.pdf", "application/pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Add an attachment where the content is contained in a string.
String content = "This is the content of the 2nd attached file.";
email.addStringAttachment("someText.txt", content);
// Get the email as multi-line base64..
BinData bdMime = new BinData();
email.getMimeBd(bdMime);
// Now get it as multi-line base64
StringBuilder sbBase64 = new StringBuilder();
bdMime.getEncodedSb("base64_mime", sbBase64);
// Send the HTTP POST (i.e. send the email)
HttpResponse resp = new HttpResponse();
try {
http.httpSb("POST", "https://graph.microsoft.com/v1.0/me/sendMail", sbBase64, "utf-8", "text/plain", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The send succeeded if the response status code = 202.
// In the success case, there is no response body. (We just get the response code to know that it succeeded.)
if (resp.getStatusCode() != 202) {
JsonObject json = new JsonObject();
json.load(resp.getBodyStr());
json.setEmitCompact(false);
System.out.println(json.emit());
System.out.println("Failed, response status code = " + resp.getStatusCode());
} else {
System.out.println("Outlook Mail Sent.");
}
}
}