Search in sources :

Example 16 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class EmailService method sendVerificationMail.

/**
 * Sends an email to the user with the temporary verification code.
 *
 * @param name the name of the player
 * @param mailAddress the player's email
 * @param code the verification code
 * @return true if email could be sent, false otherwise
 */
public boolean sendVerificationMail(String name, String mailAddress, String code) {
    if (!hasAllInformation()) {
        logger.warning("Cannot send verification email: not all email settings are complete");
        return false;
    }
    HtmlEmail email;
    try {
        email = sendMailSsl.initializeMail(mailAddress);
    } catch (EmailException e) {
        logger.logException("Failed to create verification email with the given settings:", e);
        return false;
    }
    String mailText = replaceTagsForVerificationEmail(settings.getVerificationEmailMessage(), name, code, settings.getProperty(SecuritySettings.VERIFICATION_CODE_EXPIRATION_MINUTES));
    return sendMailSsl.sendEmail(mailText, email);
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Example 17 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class EmailService method sendRecoveryCode.

/**
 * Sends an email to the user with a recovery code for the password recovery process.
 *
 * @param name the name of the player
 * @param email the player's email address
 * @param code the recovery code
 * @return true if email could be sent, false otherwise
 */
public boolean sendRecoveryCode(String name, String email, String code) {
    HtmlEmail htmlEmail;
    try {
        htmlEmail = sendMailSsl.initializeMail(email);
    } catch (EmailException e) {
        logger.logException("Failed to create email for recovery code:", e);
        return false;
    }
    String message = replaceTagsForRecoveryCodeMail(settings.getRecoveryCodeEmailMessage(), name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID));
    return sendMailSsl.sendEmail(message, htmlEmail);
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Example 18 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class EmailService method sendPasswordMail.

/**
 * Sends an email to the user with his new password.
 *
 * @param name the name of the player
 * @param mailAddress the player's email
 * @param newPass the new password
 * @return true if email could be sent, false otherwise
 */
public boolean sendPasswordMail(String name, String mailAddress, String newPass) {
    if (!hasAllInformation()) {
        logger.warning("Cannot perform email registration: not all email settings are complete");
        return false;
    }
    HtmlEmail email;
    try {
        email = sendMailSsl.initializeMail(mailAddress);
    } catch (EmailException e) {
        logger.logException("Failed to create email with the given settings:", e);
        return false;
    }
    String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass);
    // Generate an image?
    File file = null;
    if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
        try {
            file = generatePasswordImage(name, newPass);
            mailText = embedImageIntoEmailContent(file, email, mailText);
        } catch (IOException | EmailException e) {
            logger.logException("Unable to send new password as image for email " + mailAddress + ":", e);
        }
    }
    boolean couldSendEmail = sendMailSsl.sendEmail(mailText, email);
    FileUtils.delete(file);
    return couldSendEmail;
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException) File(java.io.File)

Example 19 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class SendMailSslTest method shouldCreateEmailObjectWithAddress.

@Test
public void shouldCreateEmailObjectWithAddress() throws EmailException {
    // given
    given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(465);
    String smtpHost = "mail.example.com";
    given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost);
    String senderAccount = "exampleAccount";
    given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderAccount);
    String senderAddress = "mail@example.com";
    given(settings.getProperty(EmailSettings.MAIL_ADDRESS)).willReturn(senderAddress);
    String senderName = "Server administration";
    given(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)).willReturn(senderName);
    // when
    HtmlEmail email = sendMailSsl.initializeMail("recipient@example.com");
    // then
    assertThat(email, not(nullValue()));
    assertThat(email.getToAddresses(), hasSize(1));
    assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com"));
    assertThat(email.getFromAddress().getAddress(), equalTo(senderAddress));
    assertThat(email.getFromAddress().getPersonal(), equalTo(senderName));
    assertThat(email.getHostName(), equalTo(smtpHost));
    assertThat(email.getSmtpPort(), equalTo("465"));
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) Test(org.junit.Test)

Example 20 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class SendMailSslTest method shouldCreateEmailObject.

@Test
public void shouldCreateEmailObject() throws EmailException {
    // given
    given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(465);
    String smtpHost = "mail.example.com";
    given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost);
    String senderAccount = "sender@example.org";
    given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderAccount);
    String senderName = "Server administration";
    given(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)).willReturn(senderName);
    given(settings.getProperty(PluginSettings.LOG_LEVEL)).willReturn(LogLevel.DEBUG);
    // when
    HtmlEmail email = sendMailSsl.initializeMail("recipient@example.com");
    // then
    assertThat(email, not(nullValue()));
    assertThat(email.getToAddresses(), hasSize(1));
    assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com"));
    assertThat(email.getFromAddress().getAddress(), equalTo(senderAccount));
    assertThat(email.getFromAddress().getPersonal(), equalTo(senderName));
    assertThat(email.getHostName(), equalTo(smtpHost));
    assertThat(email.getSmtpPort(), equalTo("465"));
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) Test(org.junit.Test)

Aggregations

HtmlEmail (org.apache.commons.mail.HtmlEmail)47 EmailException (org.apache.commons.mail.EmailException)24 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Email (org.apache.commons.mail.Email)7 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)6 IOException (java.io.IOException)5 InternetAddress (javax.mail.internet.InternetAddress)5 DefaultAuthenticator (org.apache.commons.mail.DefaultAuthenticator)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 File (java.io.File)4 List (java.util.List)4 Map (java.util.Map)4 EmailAttachment (org.apache.commons.mail.EmailAttachment)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 JobExecutionException (org.quartz.JobExecutionException)4 RenderResult (cn.bran.japid.template.RenderResult)3 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)3