use of org.apache.commons.mail.EmailException in project c4sg-services by Code4SocialGood.
the class AsyncEmailServiceImpl method send.
/**
* Sends an email message asynchronously.
*
* @param from email address from which the message will be sent.
* @param recipient array of strings containing the recipients of the message.
* @param subject subject header field.
* @param text content of the message.
*/
@Async
public void send(String from, String recipient, String subject, String text) {
try {
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/mail.properties"));
String host = properties.getProperty("mail.smtp.host");
String port = properties.getProperty("mail.smtp.port");
String ssl = properties.getProperty("mail.smtp.ssl.enable");
String username = properties.getProperty("mail.smtp.username");
String password = properties.getProperty("mail.smtp.password");
HtmlEmail email = new HtmlEmail();
email.setHostName(host);
email.setSmtpPort(Integer.parseInt(port));
email.setAuthentication(username, password);
email.setSSLOnConnect(Boolean.parseBoolean(ssl));
Objects.requireNonNull(from, "Sender's email is incorrect");
Objects.requireNonNull(recipient, "Recipient's email is incorrect");
email.setFrom(from);
email.addTo(recipient);
email.setSubject(subject);
email.setHtmlMsg(text);
email.send();
System.out.println("Email sent to " + recipient + " successfully");
} catch (EmailException e) {
e.printStackTrace();
System.out.println("Fail: Could not send email to: " + recipient + " !!! ");
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.apache.commons.mail.EmailException in project Activiti by Activiti.
the class MailActivityBehavior method execute.
@Override
public void execute(ActivityExecution execution) {
boolean doIgnoreException = Boolean.parseBoolean(getStringFromField(ignoreException, execution));
String exceptionVariable = getStringFromField(exceptionVariableName, execution);
Email email = null;
try {
String toStr = getStringFromField(to, execution);
String fromStr = getStringFromField(from, execution);
String ccStr = getStringFromField(cc, execution);
String bccStr = getStringFromField(bcc, execution);
String subjectStr = getStringFromField(subject, execution);
String textStr = textVar == null ? getStringFromField(text, execution) : getStringFromField(getExpression(execution, textVar), execution);
String htmlStr = htmlVar == null ? getStringFromField(html, execution) : getStringFromField(getExpression(execution, htmlVar), execution);
String charSetStr = getStringFromField(charset, execution);
List<File> files = new LinkedList<File>();
List<DataSource> dataSources = new LinkedList<DataSource>();
getFilesFromFields(attachments, execution, files, dataSources);
email = createEmail(textStr, htmlStr, attachmentsExist(files, dataSources));
addTo(email, toStr);
setFrom(email, fromStr, execution.getTenantId());
addCc(email, ccStr);
addBcc(email, bccStr);
setSubject(email, subjectStr);
setMailServerProperties(email, execution.getTenantId());
setCharset(email, charSetStr);
attach(email, files, dataSources);
email.send();
} catch (ActivitiException e) {
handleException(execution, e.getMessage(), e, doIgnoreException, exceptionVariable);
} catch (EmailException e) {
handleException(execution, "Could not send e-mail in execution " + execution.getId(), e, doIgnoreException, exceptionVariable);
}
leave(execution);
}
use of org.apache.commons.mail.EmailException in project Activiti by Activiti.
the class MailActivityBehavior method setFrom.
protected void setFrom(Email email, String from, String tenantId) {
String fromAddress = null;
if (from != null) {
fromAddress = from;
} else {
// use default configured from address in process engine config
if (tenantId != null && tenantId.length() > 0) {
Map<String, MailServerInfo> mailServers = Context.getProcessEngineConfiguration().getMailServers();
if (mailServers != null && mailServers.containsKey(tenantId)) {
MailServerInfo mailServerInfo = mailServers.get(tenantId);
fromAddress = mailServerInfo.getMailServerDefaultFrom();
}
}
if (fromAddress == null) {
fromAddress = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
}
}
try {
email.setFrom(fromAddress);
} catch (EmailException e) {
throw new ActivitiException("Could not set " + from + " as from address in email", e);
}
}
use of org.apache.commons.mail.EmailException in project SpringStepByStep by JavaProgrammerLB.
the class JavaMailDemo method test.
@Test
public void test() {
try {
// load your HTML email template
String htmlEmailTemplate = "....hello how are you <img src=\"http://www.apache.org/images/feather.gif\"> ....";
// define you base URL to resolve relative resource locations
URL url = new URL("http://www.apache.org");
// create the email message
ImageHtmlEmail email = new ImageHtmlEmail();
email.setDataSourceResolver(new DataSourceUrlResolver(url));
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setSSLOnConnect(true);
email.setFrom("username", "LIUBEI");
email.addTo("admin@yitianyigexiangfa.com", "ADMIN");
email.setAuthentication("username", "password");
email.setSubject("Test email with inline image");
// set the html message
email.setHtmlMsg(htmlEmailTemplate);
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (EmailException e) {
e.printStackTrace();
}
}
use of org.apache.commons.mail.EmailException in project SpringStepByStep by JavaProgrammerLB.
the class JavaMailDemo method sendEmailWithAttachment.
// @Test
public void sendEmailWithAttachment() {
try {
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
System.out.println(System.getProperty("user.dir"));
attachment.setPath("src/main/resources/john.jpg");
// 通过url添加附件 attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John.jpg");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setSSLOnConnect(true);
email.setFrom("username", "LIUBEI");
email.addTo("admin@yitianyigexiangfa.com", "ADMIN");
email.setAuthentication("username", "password");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
System.out.println("finished!!!");
} catch (EmailException e) {
e.printStackTrace();
}
}
Aggregations