use of org.apache.commons.mail.DefaultAuthenticator in project openhab1-addons by openhab.
the class Mail method sendMail.
/**
* Sends an email with attachment(s) via SMTP
*
* @param to the email address of the recipient
* @param subject the subject of the email
* @param message the body of the email
* @param attachmentUrlList a list of URL strings of the contents to send as attachments
*
* @return <code>true</code>, if sending the email has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends an email with attachment via SMTP")
public static boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject, @ParamDoc(name = "message") String message, @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
boolean success = false;
if (MailActionService.isProperlyConfigured) {
Email email = new SimpleEmail();
if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
email = new MultiPartEmail();
for (String attachmentUrl : attachmentUrlList) {
// Create the attachment
try {
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL(attachmentUrl));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
attachment.setName(isNotBlank(fileName) ? fileName : "Attachment");
((MultiPartEmail) email).attach(attachment);
} catch (MalformedURLException e) {
logger.error("Invalid attachment url.", e);
} catch (EmailException e) {
logger.error("Error adding attachment to email.", e);
}
}
}
email.setHostName(hostname);
email.setSmtpPort(port);
email.setStartTLSEnabled(startTLSEnabled);
email.setSSLOnConnect(sslOnConnect);
if (isNotBlank(username)) {
if (popBeforeSmtp) {
email.setPopBeforeSmtp(true, hostname, username, password);
} else {
email.setAuthenticator(new DefaultAuthenticator(username, password));
}
}
try {
if (isNotBlank(charset)) {
email.setCharset(charset);
}
email.setFrom(from);
String[] toList = to.split(";");
for (String toAddress : toList) {
email.addTo(toAddress);
}
if (!isEmpty(subject)) {
email.setSubject(subject);
}
if (!isEmpty(message)) {
email.setMsg(message);
}
email.send();
logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
success = true;
} catch (EmailException e) {
logger.error("Could not send e-mail to '" + to + "'.", e);
}
} else {
logger.error("Cannot send e-mail because of missing configuration settings. The current settings are: " + "Host: '{}', port '{}', from '{}', startTLSEnabled: {}, sslOnConnect: {}, username: '{}', password '{}'", new Object[] { hostname, String.valueOf(port), from, String.valueOf(startTLSEnabled), String.valueOf(sslOnConnect), username, password });
}
return success;
}
use of org.apache.commons.mail.DefaultAuthenticator in project Java-Tutorial by gpcodervn.
the class SendMailTLS method main.
public static void main(String[] args) throws EmailException {
// Tạo đối tượng Email
Email email = new SimpleEmail();
// Cấu hình thông tin Email Server
email.setHostName(MailConfig.HOST_NAME);
email.setSmtpPort(MailConfig.TSL_PORT);
email.setAuthenticator(new DefaultAuthenticator(MailConfig.APP_EMAIL, MailConfig.APP_PASSWORD));
email.setTLS(true);
// Người gửi
email.setFrom(MailConfig.APP_EMAIL);
// Người nhận
email.addTo(MailConfig.RECEIVE_EMAIL);
// Tiêu đề
email.setSubject("Testing Subject");
// Nội dung email
email.setMsg("Welcome to gpcoder.com");
// send message
email.send();
System.out.println("Message sent successfully");
}
use of org.apache.commons.mail.DefaultAuthenticator in project dubidubi by lzzzz4.
the class MailUtils method sendPicMail.
public static boolean sendPicMail(MailDTO dto) {
boolean isSucess = false;
Email email = new HtmlEmail();
email.setHostName("smtp.qq.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("1622472966@qq.com", "lyvihlbjmafcbdab"));
email.setSSLOnConnect(true);
try {
// 发件人
email.setFrom("1622472966@qq.com");
// 邮箱头
email.setSubject(dto.getTitle());
// 邮箱身体
email.setContent(dto.getContent(), "text/html;charset=utf-8");
email.addTo(dto.getMail());
email.send();
isSucess = true;
} catch (EmailException e) {
e.printStackTrace();
}
return isSucess;
}
use of org.apache.commons.mail.DefaultAuthenticator in project shepher by XiaoMi.
the class GeneralMailSender method send.
protected void send(String mailAddress, String title, String content) {
if (StringUtils.isBlank(mailAddress)) {
return;
}
try {
Email email = new HtmlEmail();
email.setHostName(hostname);
email.setAuthenticator(new DefaultAuthenticator(username, password));
email.setSmtpPort(port);
email.setFrom(from, fromname);
email.setSubject(title);
email.setMsg(content);
email.addTo(mailAddress.split(mailAddressEndSeparator));
email.send();
} catch (Exception e) {
logger.error("Send Mail Error", e);
}
}
use of org.apache.commons.mail.DefaultAuthenticator in project Jartop by TheRedSpy15.
the class EmailClient method send.
@FXML
private void send() throws EmailException {
// Credentials
final String addressSender = Core.getUserData().getEmailAddress();
final String password = Core.getUserData().getEmailPassword();
final String host = Core.getUserData().getHostName();
final short port = Core.getUserData().getSmtpPort();
final boolean useSSL = Core.getUserData().isSslConnection();
// Email details
final String addressReceiver = addressField.getText();
final String subject = subjectField.getText();
final String message = messageArea.getText();
// Sending
mail.setHostName(host);
mail.setSmtpPort(port);
mail.setAuthenticator(new DefaultAuthenticator(addressSender, password));
mail.setSSLOnConnect(useSSL);
mail.setFrom(addressSender);
mail.setSubject(subject);
mail.setMsg(message);
mail.addTo(addressReceiver);
// bcc & cc
if (!bccField.getText().trim().equals(""))
mail.addBcc(bccField.getText());
if (!ccField.getText().trim().equals(""))
mail.addCc(ccField.getText());
if (attachments != null) {
for (EmailAttachment i : attachments) {
mail.attach(i);
}
}
mail.send();
Desktop.getAppWindow().close();
}
Aggregations