Search in sources :

Example 1 with Message

use of org.activityinfo.server.mail.Message in project activityinfo by bedatadriven.

the class UpdateUserPermissionsHandler method createNewUser.

private User createNewUser(User executingUser, UserPermissionDTO dto) {
    if (executingUser.getId() == 0) {
        throw new AssertionError("executingUser.id == 0!");
    }
    if (executingUser.getName() == null) {
        throw new AssertionError("executingUser.name == null!");
    }
    User user = UserDAOImpl.createNewUser(dto.getEmail(), dto.getName(), executingUser.getLocale());
    user.setInvitedBy(executingUser);
    userDAO.persist(user);
    try {
        Message message = mailSender.createMessage(new InvitationMessage(user, executingUser));
        message.replyTo(executingUser.getEmail(), executingUser.getName());
        mailSender.send(message);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Could not send invitation mail", e);
        throw new CommandException("Failed to send invitation email");
    }
    return user;
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) InvitationMessage(org.activityinfo.server.mail.InvitationMessage) Message(org.activityinfo.server.mail.Message) InvitationMessage(org.activityinfo.server.mail.InvitationMessage) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) CommandException(org.activityinfo.legacy.shared.exception.CommandException) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) UserExistsException(org.activityinfo.legacy.shared.command.result.UserExistsException) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 2 with Message

use of org.activityinfo.server.mail.Message in project activityinfo by bedatadriven.

the class ReportMailer method mailReport.

private void mailReport(ReportSubscription sub, Report report, Date today, byte[] content) throws IOException, SAXException, MessagingException {
    LOGGER.log(Level.INFO, "Sending email to " + sub.getUser().getEmail());
    DateFormat reportDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, sub.getUser().getLocaleObject());
    Message email = new Message();
    email.to(sub.getUser().getEmail(), sub.getUser().getName());
    email.subject("ActivityInfo: " + report.getTitle());
    email.body(ReportMailerHelper.composeTextEmail(sub, report));
    email.addAttachment().withContent(content, "text/enriched").withFileName(report.getContent().getFileName() + " " + reportDateFormat.format(today) + ".rtf");
    mailer.send(email);
}
Also used : Message(org.activityinfo.server.mail.Message) DateFormat(java.text.DateFormat)

Example 3 with Message

use of org.activityinfo.server.mail.Message in project activityinfo by bedatadriven.

the class DigestMessageBuilder method build.

public Message build() throws IOException, MessagingException {
    Preconditions.checkNotNull(userDigest);
    Preconditions.checkNotNull(userDigest.getUser());
    // set the locale of the messages
    ThreadLocalLocaleProvider.pushLocale(userDigest.getUser().getLocaleObject());
    try {
        DigestModel model = digestModelBuilder.createModel(userDigest);
        if (!model.hasData()) {
            return null;
        }
        // create message, set recipient & bcc
        Message message = new Message();
        message.to(userDigest.getUser().getEmail(), userDigest.getUser().getName());
        String subject = I18N.MESSAGES.digestSubject(userDigest.getDate());
        message.subject(subject);
        // create the html body
        HtmlWriter htmlWriter = new HtmlWriter();
        htmlWriter.startDocument();
        htmlWriter.startDocumentHeader();
        htmlWriter.documentTitle(subject);
        htmlWriter.open(new HtmlTag("style")).at("type", "text/css").text("body { font-family:Helvetica; } a {color: black; text-decoration:none;} ").close();
        htmlWriter.endDocumentHeader();
        htmlWriter.startDocumentBody();
        htmlWriter.paragraph(I18N.MESSAGES.digestGreeting(userDigest.getUser().getName()));
        // the digest content
        htmlWriter.paragraph(digestRenderer.renderHtml(model));
        String signature = I18N.MESSAGES.digestSignature();
        htmlWriter.paragraph(signature);
        htmlWriter.endDocumentBody();
        htmlWriter.endDocument();
        LOGGER.finest("digest:\n" + htmlWriter.toString());
        message.htmlBody(htmlWriter.toString());
        return message;
    } finally {
        ThreadLocalLocaleProvider.popLocale();
    }
}
Also used : Message(org.activityinfo.server.mail.Message) HtmlWriter(org.activityinfo.server.util.html.HtmlWriter) HtmlTag(org.activityinfo.server.util.html.HtmlTag)

Example 4 with Message

use of org.activityinfo.server.mail.Message in project activityinfo by bedatadriven.

the class UserDigestResource method createUserDigest.

@GET
@Produces(MediaType.TEXT_HTML)
public String createUserDigest(@QueryParam(PARAM_USER) int userId, @QueryParam(PARAM_NOW) Long now, @QueryParam(PARAM_DAYS) int days, @QueryParam(PARAM_SENDEMAIL) @DefaultValue(PARAM_SENDEMAIL_DEF) boolean sendEmail) throws IOException, MessagingException {
    if (userId <= 0) {
        return "no user specified";
    }
    Date date = now == null ? new Date() : new Date(now);
    if (days <= 0) {
        days = getDefaultDays();
    }
    User user = entityManager.get().find(User.class, userId);
    if (!user.isEmailNotification()) {
        return "user's email notification flag is set to false.";
    }
    UserToken userToken = UserTokenManager.create(userId, UserTokenScope.SUBSCRIBE, UserTokenScope.UNSUBSCRIBE);
    authProvider.set(user);
    LOGGER.info("creating digest for " + user.getEmail() + " on " + DateFormatter.formatDateTime(date) + " for activity period: " + days + " day(s)." + " (sending email: " + sendEmail + ")");
    UserDigest userDigest = new UserDigest(user, date, days, userToken.getSecureToken());
    DigestMessageBuilder digest = new DigestMessageBuilder(digestModelBuilder, digestRenderer);
    digest.setUserDigest(userDigest);
    Message message = digest.build();
    if (message != null && sendEmail) {
        mailSender.get().send(message);
    }
    return message == null ? "no updates found" : message.getHtmlBody();
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) Message(org.activityinfo.server.mail.Message) Date(java.util.Date) UserToken(org.activityinfo.server.authentication.UserToken) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Message (org.activityinfo.server.mail.Message)4 User (org.activityinfo.server.database.hibernate.entity.User)2 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 UserExistsException (org.activityinfo.legacy.shared.command.result.UserExistsException)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)1 UserToken (org.activityinfo.server.authentication.UserToken)1 InvitationMessage (org.activityinfo.server.mail.InvitationMessage)1 HtmlTag (org.activityinfo.server.util.html.HtmlTag)1 HtmlWriter (org.activityinfo.server.util.html.HtmlWriter)1