Sending mail to one reciepient:
import org.sape.carbon.services.email.util.classify.MailContentTypeEnum;
import org.sape.carbon.services.email.MailFailureException;
import org.sape.carbon.services.email.MailDataObject;
...
// Obtain a reference of the email component from the Lookup service
MailService mail = (MailService) Lookup.getInstance().fetchComponent("/email/test");
try {
MailDataObject mailDataObject = new MailDataObject();
mailDataObject.addTo("Someone@somewhere.com", "Someone");
mailDataObject.setFrom("joe@xyz.com", "Joe");
mailDataObject.setSubject("subject");
mailDataObject.setBody("A simple plain text email", MailContentTypeEnum.PLAIN_TEXT);
mail.sendMail(mailDataObject);
} catch(MailFailureException me) {
// handle Failure
}
Sending email to multiple recipients:
import org.sape.carbon.services.email.MailFailureException;
import org.sape.carbon.services.email.util.MailAttachment;
...
// Obtain a reference of the email component from the Lookup service
MailService mail = (MailService) Lookup.getInstance().fetchComponent("/email/test");
// Prepare "To" List, it is mandatory
HashMap toMap = new HashMap();
toMap.put("Someone@somewhere.com", "Someone");
Prepare CC list, it can be null
HashMap ccMap = new HashMap();
ccMap.put("ccSomeone@somewhere.com", "ccSomeone");
// Prepare BCC list, it can be null
HashMap bccMap = null;
try {
MailDataObject mailDataObject = new MailDataObject();
mailDataObject.addTo(toMap);
mailDataObject.addCC(ccMap);
mailDataObject.addBCC(bccMap);
mailDataObject.setFrom("joe@xyz.com", "Joe");
mailDataObject.setSubject("subject");
mailDataObject.addHeader("X-MailService", "XYZ Corporation");
mailDataObject.setBody("A simple plain text email", MailContentTypeEnum.PLAIN_TEXT);
mail.sendMail(mailDataObject);
} catch(MailFailureException me) {
// handle failure
}
Sending email with attachments:
import org.sape.carbon.services.email.MailFailureException;
import org.sape.carbon.services.email.util.MailAttachment;
...
// Obtain a reference of the email component from the Lookup service
MailService mail = (MailService) Lookup.getInstance().fetchComponent("/email/test");
// Prepare "To" List, this is mandatory
HashMap toMap = new HashMap();
toMap.put("Someone@somewhere.com", "Someone");
// Prepare CC list, can be null
HashMap ccMap = new HashMap();
ccMap.put("ccSomeone@somewhere.com", "ccSomeone");
// Prepare BCC list, can be null
HashMap bccMap = null;
// Any extra headers, can be null
HashMap headers = new HashMap();
headers.put("X-MailService", "XYZ Corporation");
// Charset for encoding e.g ISO-8859-1, UTF-8.
// if null is passed default encoding is used
String charset = null;
try {
// Adding attachments
MailAttachment[] attachments = new MailAttachment[2];
attachments[1] = new MailAttachment("Name", "description", "c:\\attachment.txt");
// Add import statement java.net.URL
URL url = new URL("http", "localhost", "attachment.txt");
attachments[2] = new MailAttachment("Name", null, url);
MailDataObject mailDataObject = new MailDataObject();
mailDataObject.addTo(toMap);
mailDataObject.addCC(ccMap);
mailDataObject.addBCC(bccMap);
mailDataObject.setFrom("joe@xyz.com", "Joe");
mailDataObject.setSubject("subject");
mailDataObject.setBody(
"<html><body><b>This message should be in bold</html></body></b>",
MailContentTypeEnum.HTML);
mailDataObject.setAttachments(attachments);
mail.sendMail(mailDataObject);
} catch(MalformedURLException mue) {
// handle failure
} catch(MailFailureException me) {
// handle failure
}
Sending internationalized emails:
import org.sape.carbon.services.email.MailFailureException;
import org.sape.carbon.services.email.util.MailAttachment;
...
// Obtain a reference of the email component from the Lookup service
MailService mail = (MailService) Lookup.getInstance().fetchComponent("/email/test");
// Prepare "To" List, it is a mandatory field
HashMap toMap = new HashMap();
toMap.put("Someone@somewhere.com", "Someone");
// Charset for encoding
String charset = "UTF-8";
String nonASCIICharacters = "ôôôôô";
String localizedFromPersonal = nonASCIICharacters;
String fromEmail = "joe@xyz.com";
String localizedSubject = nonASCIICharacters;
String localizedBody = nonASCIICharacters;
String body = "<html><body><b>" + localizedBody + "</html></body></b>";
// Note headers cannot be internationalized
Map headers = null;
try {
MailDataObject mailDataObject = new MailDataObject();
mailDataObject.addTo(toMap);
mailDataObject.setFrom("joe@xyz.com", localizedFromPersonal);
mailDataObject.setSubject(localizedSubject);
mailDataObject.setBody(body, MailContentTypeEnum.HTML);
mailDataObject.setCharset(charset);
mail.sendMail(mailDataObject);
}
catch(MailFailureException me) {
// handle failure
}