Search in sources :

Example 1 with MangoEmailContent

use of com.serotonin.m2m2.email.MangoEmailContent in project ma-core-public by infiniteautomation.

the class PasswordResetService method sendEmail.

public void sendEmail(User user, String token) throws TemplateException, IOException, AddressException {
    URI uri = null;
    try {
        uri = this.generateResetUrl(token);
    } catch (Exception e) {
    }
    Translations translations = Translations.getTranslations(user.getLocaleObject());
    Jws<Claims> parsed = this.parse(token);
    Date expiration = parsed.getBody().getExpiration();
    Map<String, Object> model = new HashMap<>();
    model.put("username", user.getUsername());
    model.put("resetUri", uri != null ? uri : "");
    model.put("token", token);
    model.put("expiration", expiration);
    TranslatableMessage subject = new TranslatableMessage("ftl.passwordReset.subject", user.getUsername());
    MangoEmailContent content = new MangoEmailContent("passwordReset", model, translations, subject.translate(translations), Common.UTF8);
    EmailWorkItem.queueEmail(user.getEmail(), content);
}
Also used : Claims(io.jsonwebtoken.Claims) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) Translations(com.serotonin.m2m2.i18n.Translations) TemplateException(freemarker.template.TemplateException) AddressException(javax.mail.internet.AddressException) NotFoundException(com.serotonin.m2m2.vo.exception.NotFoundException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Date(java.util.Date)

Example 2 with MangoEmailContent

use of com.serotonin.m2m2.email.MangoEmailContent in project ma-core-public by infiniteautomation.

the class SystemSettingsDwr method sendTestEmail.

@DwrPermission(admin = true)
public Map<String, Object> sendTestEmail(String host, int port, String from, String name, boolean auth, String username, String password, boolean tls, int contentType) {
    // Save the settings
    saveEmailSettings(host, port, from, name, auth, username, password, tls, contentType);
    // Get the web context information
    User user = Common.getHttpUser();
    Map<String, Object> result = new HashMap<>();
    try {
        Translations translations = getTranslations();
        Map<String, Object> model = new HashMap<>();
        model.put("message", new TranslatableMessage("systemSettings.testEmail"));
        MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
        EmailWorkItem.queueEmail(user.getEmail(), cnt);
        result.put("message", new TranslatableMessage("common.testEmailSent", user.getEmail()));
    } catch (Exception e) {
        result.put("exception", e.getMessage());
    }
    return result;
}
Also used : User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) InvalidArgumentException(com.serotonin.InvalidArgumentException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with MangoEmailContent

use of com.serotonin.m2m2.email.MangoEmailContent in project ma-modules-public by infiniteautomation.

the class ServerRestV2Controller method sendTestEmail.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) {
    try {
        Translations translations = Common.getTranslations();
        Map<String, Object> model = new HashMap<>();
        model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
        MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
        EmailWorkItem.queueEmail(email, cnt);
        return new ResponseEntity<String>(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()), HttpStatus.OK);
    } catch (Exception e) {
        throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) IOException(java.io.IOException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with MangoEmailContent

use of com.serotonin.m2m2.email.MangoEmailContent in project ma-modules-public by infiniteautomation.

the class ServerRestController method sendTestEmail.

@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<String> result = new RestProcessResult<String>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            try {
                Translations translations = Common.getTranslations();
                Map<String, Object> model = new HashMap<>();
                model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
                MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
                EmailWorkItem.queueEmail(email, cnt);
                return result.createResponseEntity(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()));
            } catch (Exception e) {
                result.addRestMessage(this.getInternalServerErrorMessage(e.getMessage()));
            }
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to send a test email.");
            result.addRestMessage(this.getUnauthorizedMessage());
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with MangoEmailContent

use of com.serotonin.m2m2.email.MangoEmailContent in project ma-core-public by infiniteautomation.

the class MailingListsDwr method sendTestEmail.

@DwrPermission(admin = true)
public ProcessResult sendTestEmail(int id, String name, List<RecipientListEntryBean> entryBeans) {
    ProcessResult response = new ProcessResult();
    MailingList ml = createMailingList(id, null, name, AlarmLevels.IGNORE, entryBeans);
    MailingListDao.instance.populateEntrySubclasses(ml.getEntries());
    Set<String> addresses = new HashSet<String>();
    ml.appendAddresses(addresses, null);
    String[] toAddrs = addresses.toArray(new String[0]);
    try {
        Translations translations = Common.getTranslations();
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("message", new TranslatableMessage("ftl.userTestEmail", ml.getName()));
        MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
        EmailWorkItem.queueEmail(toAddrs, cnt);
    } catch (Exception e) {
        response.addGenericMessage("mailingLists.testerror", e.getMessage());
        log.warn("", e);
    }
    return response;
}
Also used : HashMap(java.util.HashMap) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) HashSet(java.util.HashSet) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

MangoEmailContent (com.serotonin.m2m2.email.MangoEmailContent)7 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)7 Translations (com.serotonin.m2m2.i18n.Translations)7 HashMap (java.util.HashMap)7 IOException (java.io.IOException)5 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 User (com.serotonin.m2m2.vo.User)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 IntStringPair (com.serotonin.db.pair.IntStringPair)1 PostEmailRunnable (com.serotonin.m2m2.email.PostEmailRunnable)1 UsedImagesDirective (com.serotonin.m2m2.email.UsedImagesDirective)1 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)1 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1