Search in sources :

Example 6 with EmailData

use of com.helger.smtp.data.EmailData in project ph-web by phax.

the class MailAPITest method testBasic.

@Ignore("to avoid spamming my mailbox")
@Test
public void testBasic() {
    /*
     * This file might not be present, as it contains the real-life SMTP
     * settings. It should reside in src/test/resource and is SVN ignored by
     * name
     */
    final IReadableResource aRes = new ClassPathResource("smtp-settings.xml");
    if (aRes.exists()) {
        final boolean bOldIsDebug = GlobalDebug.isDebugMode();
        GlobalDebug.setDebugModeDirect(true);
        try {
            EmailGlobalSettings.enableJavaxMailDebugging(GlobalDebug.isDebugMode());
            // Setup debug listeners
            EmailGlobalSettings.addConnectionListener(new LoggingConnectionListener());
            EmailGlobalSettings.addConnectionListener(new LoggingConnectionListener(EErrorLevel.WARN));
            EmailGlobalSettings.addEmailDataTransportListener(new LoggingTransportListener());
            EmailGlobalSettings.addEmailDataTransportListener(new LoggingTransportListener(EErrorLevel.WARN));
            final SMTPSettings aSMTPSettings = MicroTypeConverter.convertToNative(MicroReader.readMicroXML(aRes).getDocumentElement(), SMTPSettings.class);
            final IMutableEmailData aMailData = new EmailData(EEmailType.TEXT);
            aMailData.to().add(new EmailAddress("ph@helger.com"));
            aMailData.cc().add(new EmailAddress("hudri@helger.com"));
            aMailData.setFrom(new EmailAddress("auto@helger.com"));
            aMailData.setSubject("JÜnit test with späcial käräktärs");
            aMailData.setBody("Hi there\nLine 2\n4 special chars: äöüß\n123456789\nBest regards: ph-smtp");
            MailAPI.queueMail(aSMTPSettings, aMailData);
            MailAPI.stop();
            /*
         * try to queue again after MailAPI was stopped - should end up in
         * failed mail queue
         */
            assertEquals(0, MailAPI.getFailedMailQueue().size());
            MailAPI.queueMail(aSMTPSettings, aMailData);
            assertEquals(1, MailAPI.getFailedMailQueue().size());
        } finally {
            GlobalDebug.setDebugModeDirect(bOldIsDebug);
            EmailGlobalSettings.setToDefault();
        }
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) LoggingConnectionListener(com.helger.smtp.transport.listener.LoggingConnectionListener) IReadableResource(com.helger.commons.io.resource.IReadableResource) LoggingTransportListener(com.helger.smtp.transport.listener.LoggingTransportListener) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) EmailData(com.helger.smtp.data.EmailData) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) EmailAddress(com.helger.commons.email.EmailAddress) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with EmailData

use of com.helger.smtp.data.EmailData in project peppol-practical by phax.

the class PagePublicContact method fillContent.

@Override
protected void fillContent(@Nonnull final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final String sRecaptchWebKey = AppConfig.getRecaptchaWebKey();
    final String sRecaptchSecretKey = AppConfig.getRecaptchaSecretKey();
    final boolean bRecaptchaEnabled = StringHelper.hasText(sRecaptchWebKey) && StringHelper.hasText(sRecaptchSecretKey);
    aNodeList.addChild(p("If you have general questions concerning Peppol technology, you may contact me using the form below. Please be aware, that I run this page on a voluntary basis and that the answers you may receive are my personal answers and not official OpenPeppol answers."));
    boolean bShowForm = true;
    final FormErrorList aFormErrors = new FormErrorList();
    if (aWPEC.hasAction(CPageParam.ACTION_PERFORM)) {
        final String sName = StringHelper.trim(aWPEC.params().getAsString(FIELD_NAME));
        final String sEmail = StringHelper.trim(aWPEC.params().getAsString(FIELD_EMAIL));
        final String sTopic = aWPEC.params().getAsString(FIELD_TOPIC);
        final String sText = StringHelper.trim(aWPEC.params().getAsString(FIELD_TEXT));
        final String sReCaptchaResponse = StringHelper.trim(aWPEC.params().getAsString(HCReCaptchaV2.RESPONSE_PARAMETER_NAME));
        if (StringHelper.hasNoText(sName))
            aFormErrors.addFieldError(FIELD_NAME, "Your name must be provided.");
        if (StringHelper.hasNoText(sEmail))
            aFormErrors.addFieldError(FIELD_EMAIL, "Your email address must be provided.");
        else if (!EmailAddressHelper.isValid(sEmail))
            aFormErrors.addFieldError(FIELD_EMAIL, "The provided email address is invalid.");
        if (StringHelper.hasNoText(sText))
            aFormErrors.addFieldError(FIELD_TEXT, "A message text must be provided.");
        if (bRecaptchaEnabled)
            if (aFormErrors.isEmpty() || StringHelper.hasText(sReCaptchaResponse)) {
                if (!CaptchaStateSessionSingleton.getInstance().isChecked()) {
                    // Check only if no other errors occurred
                    if (ReCaptchaServerSideValidator.check(sRecaptchSecretKey, sReCaptchaResponse).isFailure())
                        aFormErrors.addFieldError(FIELD_CAPTCHA, "Please confirm you are not a robot!");
                    else
                        CaptchaStateSessionSingleton.getInstance().setChecked();
                }
            }
        if (aFormErrors.isEmpty()) {
            if (!_isSpamBody(sText)) {
                final EmailData aEmailData = new EmailData(EEmailType.TEXT);
                aEmailData.setFrom(new EmailAddress("peppol-practical@helger.com", "Peppol Practical"));
                aEmailData.to().add(new EmailAddress("philip@helger.com"));
                aEmailData.replyTo().add(new EmailAddress(sEmail, sName));
                aEmailData.setSubject("[" + AppHelper.getApplicationTitle() + "] Contact Form - " + sName);
                final StringBuilder aSB = new StringBuilder();
                aSB.append("Contact form from " + AppHelper.getApplicationTitle() + " was filled out.\n\n");
                aSB.append("Name: ").append(sName).append("\n");
                aSB.append("Email: ").append(sEmail).append("\n");
                aSB.append("Topic: ").append(sTopic).append("\n");
                aSB.append("Text:\n").append(sText).append("\n");
                aEmailData.setBody(aSB.toString());
                LOGGER.info("Sending contact form from '" + sName + "' and email '" + sEmail + "' with topic '" + sTopic + "'");
                ScopedMailAPI.getInstance().queueMail(InternalErrorSettings.getSMTPSettings(), aEmailData);
            } else
                LOGGER.info("Ignoring spam contact form from '" + sEmail + "' with topic '" + sTopic + "'");
            aNodeList.addChild(success("Thank you for your message. Please note that I run this page on a voluntary basis on my expenses - you may consider a donation."));
            bShowForm = false;
        }
    }
    if (bShowForm) {
        final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormSelf(aWPEC));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your name").setCtrl(new HCEdit(new RequestField(FIELD_NAME))).setErrorList(aFormErrors.getListOfField(FIELD_NAME)));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your email address").setCtrl(new HCEdit(new RequestField(FIELD_EMAIL))).setErrorList(aFormErrors.getListOfField(FIELD_EMAIL)));
        final HCExtSelect aSelect = new HCExtSelect(new RequestField(FIELD_TOPIC));
        aSelect.addOption("SML/SMK");
        aSelect.addOption("SMP");
        aSelect.addOption("AccessPoint (AP)/AS4");
        aSelect.addOption("Peppol Directory");
        aSelect.addOption("Validation");
        aSelect.addOption("CEF");
        aSelect.addOption("General question");
        aSelect.addOptionPleaseSelect(aDisplayLocale);
        aForm.addFormGroup(new BootstrapFormGroup().setLabel("Topic").setCtrl(aSelect).setErrorList(aFormErrors.getListOfField(FIELD_TOPIC)));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your message").setCtrl(new HCTextAreaAutosize(new RequestField(FIELD_TEXT)).setRows(5)).setErrorList(aFormErrors.getListOfField(FIELD_TEXT)));
        if (bRecaptchaEnabled)
            if (!CaptchaStateSessionSingleton.getInstance().isChecked()) {
                // Add visible Captcha
                aForm.addFormGroup(new BootstrapFormGroup().setCtrl(HCReCaptchaV2.create(sRecaptchWebKey, aDisplayLocale)).setErrorList(aFormErrors.getListOfField(FIELD_CAPTCHA)));
            }
        aForm.addChild(new HCHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM));
        aForm.addChild(new BootstrapSubmitButton().addChild("Send message").setIcon(EFontAwesome4Icon.PAPER_PLANE));
    }
}
Also used : Locale(java.util.Locale) HCExtSelect(com.helger.photon.uicore.html.select.HCExtSelect) HCNodeList(com.helger.html.hc.impl.HCNodeList) HCHiddenField(com.helger.html.hc.html.forms.HCHiddenField) HCTextAreaAutosize(com.helger.photon.uictrls.autosize.HCTextAreaAutosize) FormErrorList(com.helger.photon.core.form.FormErrorList) HCEdit(com.helger.html.hc.html.forms.HCEdit) EmailData(com.helger.smtp.data.EmailData) EmailAddress(com.helger.commons.email.EmailAddress) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) BootstrapSubmitButton(com.helger.photon.bootstrap4.button.BootstrapSubmitButton) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) RequestField(com.helger.photon.core.form.RequestField)

Aggregations

EmailData (com.helger.smtp.data.EmailData)7 EmailAddress (com.helger.commons.email.EmailAddress)5 Test (org.junit.Test)4 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 HCEdit (com.helger.html.hc.html.forms.HCEdit)2 HCHiddenField (com.helger.html.hc.html.forms.HCHiddenField)2 HCNodeList (com.helger.html.hc.impl.HCNodeList)2 BootstrapSubmitButton (com.helger.photon.bootstrap4.button.BootstrapSubmitButton)2 BootstrapForm (com.helger.photon.bootstrap4.form.BootstrapForm)2 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)2 FormErrorList (com.helger.photon.core.form.FormErrorList)2 RequestField (com.helger.photon.core.form.RequestField)2 HCExtSelect (com.helger.photon.uicore.html.select.HCExtSelect)2 HCTextAreaAutosize (com.helger.photon.uictrls.autosize.HCTextAreaAutosize)2 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)2 ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)2 SMTPSettings (com.helger.smtp.settings.SMTPSettings)2 Locale (java.util.Locale)2 Ignore (org.junit.Ignore)2