Vylepšený dialog pro odeslání e-mailu. Adresa "Od" se nastavuje podle

přihlášeného uživatele. Přidáno logování chyb při odesílání e-mailu.
multitenant
Josef Rokos 10 years ago
parent 78cf41d2a0
commit 0a98db893a

@ -3,9 +3,13 @@ package info.bukova.isspst.mail;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource; import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailParseException; import org.springframework.mail.MailParseException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@ -14,6 +18,8 @@ public class MailerWithAttachement implements Mailer {
private JavaMailSender sender; private JavaMailSender sender;
private String from; private String from;
private boolean overrideFrom = false;
private static final Logger logger = LoggerFactory.getLogger(MailerWithAttachement.class);
public MailerWithAttachement(JavaMailSender sender) { public MailerWithAttachement(JavaMailSender sender) {
this.sender = sender; this.sender = sender;
@ -27,7 +33,9 @@ public class MailerWithAttachement implements Mailer {
try { try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
if (message.getFrom() == null || message.getFrom().isEmpty()) { message.setReplyTo(message.getFrom());
if (message.getFrom() == null || message.getFrom().isEmpty() || overrideFrom) {
if (from == null || from.isEmpty()) { if (from == null || from.isEmpty()) {
message.setFrom("tomcat@is.bukova.info"); message.setFrom("tomcat@is.bukova.info");
} else { } else {
@ -36,9 +44,15 @@ public class MailerWithAttachement implements Mailer {
} }
helper.setFrom(message.getFrom()); helper.setFrom(message.getFrom());
helper.setReplyTo(message.getReplyTo());
helper.setTo(message.getTo()); helper.setTo(message.getTo());
helper.setSubject(message.getSubject()); helper.setSubject(message.getSubject());
if (message.getText() != null) {
helper.setText(message.getText(), message.isHtml()); helper.setText(message.getText(), message.isHtml());
} else {
helper.setText("", message.isHtml());
}
if (message.getAttachementData() != null) { if (message.getAttachementData() != null) {
InputStreamSource source = new ByteArrayResource(message.getAttachementData()); InputStreamSource source = new ByteArrayResource(message.getAttachementData());
@ -47,11 +61,22 @@ public class MailerWithAttachement implements Mailer {
} catch (MessagingException e) { } catch (MessagingException e) {
throw new MailParseException(e); throw new MailParseException(e);
} }
try {
sender.send(mimeMessage); sender.send(mimeMessage);
} catch (MailAuthenticationException e) {
logger.error("Authentication error");
} catch (MailSendException e) {
logger.error("Mail send error: " + e.getMessage());
}
} }
public void setFrom(String from) { public void setFrom(String from) {
this.from = from; this.from = from;
} }
public void setOverrideFrom(boolean override) {
this.overrideFrom = override;
}
} }

@ -232,6 +232,7 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
} }
if (message != null) { if (message != null) {
message.setFrom(getLoggedInUser().getEmail());
message.setTo(e.getOwnedBy().getEmail()); message.setTo(e.getOwnedBy().getEmail());
mailer.send(message); mailer.send(message);
} }

@ -1,15 +1,29 @@
package info.bukova.isspst.ui.mail; package info.bukova.isspst.ui.mail;
import static ch.lambdaj.Lambda.filter;
import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static org.hamcrest.Matchers.startsWith;
import info.bukova.isspst.data.Address;
import info.bukova.isspst.data.Member;
import info.bukova.isspst.data.User;
import info.bukova.isspst.mail.MailMessage; import info.bukova.isspst.mail.MailMessage;
import info.bukova.isspst.mail.Mailer; import info.bukova.isspst.mail.Mailer;
import info.bukova.isspst.reporting.Generator; import info.bukova.isspst.reporting.Generator;
import info.bukova.isspst.reporting.GeneratorFactory; import info.bukova.isspst.reporting.GeneratorFactory;
import info.bukova.isspst.reporting.ReportDefinition; import info.bukova.isspst.reporting.ReportDefinition;
import info.bukova.isspst.services.addressbook.AdbService;
import info.bukova.isspst.services.users.UserService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.security.access.AccessDeniedException;
import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ExecutionArgParam; import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window; import org.zkoss.zul.Window;
@ -21,10 +35,22 @@ public class MailForm {
private ReportDefinition reportDefinition; private ReportDefinition reportDefinition;
@WireVariable @WireVariable
private GeneratorFactory genFactory; private GeneratorFactory genFactory;
@WireVariable
private UserService userService;
@WireVariable
private AdbService adbService;
private MailMessage message; private MailMessage message;
private String to; private String to;
private String cc;
private String bcc;
private String attachement; private String attachement;
private boolean report; private boolean report;
private List<Address> addressbook;
private List<User> users;
private List<Address> selectedAddresses;
private List<User> selectedUsers;
private int adbType;
private String findAddress;
@Init @Init
public void init(@ExecutionArgParam("report") Boolean report) { public void init(@ExecutionArgParam("report") Boolean report) {
@ -34,11 +60,37 @@ public class MailForm {
if (report) { if (report) {
attachement = "report.pdf"; attachement = "report.pdf";
} }
try {
addressbook = adbService.getAll();
} catch (AccessDeniedException e) {
addressbook = new ArrayList<Address>();
}
users = userService.getAll();
selectedAddresses = new ArrayList<Address>();
selectedUsers = new ArrayList<User>();
to = "";
cc = "";
bcc = "";
} }
@Command @Command
public void send(@BindingParam("window") Window window) { public void send(@BindingParam("window") Window window) {
message.setTo(to); message.setTo(to.split(";"));
if (cc != null && !cc.isEmpty()) {
message.setCc(cc.split(";"));
}
if (bcc != null && !bcc.isEmpty()) {
message.setCc(bcc.split(";"));
}
message.setFrom(userService.getCurrent().getEmail());
if (report) { if (report) {
Generator generator = genFactory.createGenerator(reportDefinition); Generator generator = genFactory.createGenerator(reportDefinition);
message.setAttachementData(generator.generate()); message.setAttachementData(generator.generate());
@ -50,6 +102,76 @@ public class MailForm {
window.detach(); window.detach();
} }
@Command
@NotifyChange("to")
public void addTo() {
to = appendAddress(to, selAddrs());
}
@Command
@NotifyChange("cc")
public void addCc() {
cc = appendAddress(cc, selAddrs());
}
@Command
@NotifyChange("bcc")
public void addBcc() {
bcc = appendAddress(bcc, selAddrs());
}
@Command
@NotifyChange({"selectedAddresses", "selectedUsers"})
public void find() {
if (adbType == 0) {
List<User> found = filter(having(on(Member.class).getFullName(), startsWith(findAddress)), users);
if (!found.isEmpty()) {
selectedUsers.clear();
if (!findAddress.isEmpty() && found.get(0).getEmail() != null && !found.get(0).getEmail().isEmpty()) {
selectedUsers.add(found.get(0));
}
}
} else {
List<Address> found = filter(having(on(Address.class).getCompany(), startsWith(findAddress)), addressbook);
if (!found.isEmpty()) {
selectedAddresses.clear();
if (!findAddress.isEmpty() && found.get(0).getEmail() != null && !found.get(0).getEmail().isEmpty()) {
selectedAddresses.add(found.get(0));
}
}
}
}
private String appendAddress(String field, List<String> addrs) {
for (String addr : addrs) {
if (field.isEmpty()) {
field = addr;
} else {
field += "; " + addr;
}
}
return field;
}
private List<String> selAddrs() {
List<String> ret = new ArrayList<String>();
if (adbType == 1) {
for (Address addr : selectedAddresses) {
ret.add(addr.getCompany() + " <" + addr.getEmail() + ">");
}
} else {
for (User u : selectedUsers) {
ret.add(u.getFullName() + " <" + u.getEmail() + ">");
}
}
return ret;
}
public MailMessage getMessage() { public MailMessage getMessage() {
return message; return message;
} }
@ -70,4 +192,60 @@ public class MailForm {
return attachement; return attachement;
} }
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getBcc() {
return bcc;
}
public void setBcc(String bcc) {
this.bcc = bcc;
}
public List<Address> getAddressbook() {
return addressbook;
}
public List<User> getUsers() {
return users;
}
public int getAdbType() {
return adbType;
}
public void setAdbType(int adbType) {
this.adbType = adbType;
}
public List<Address> getSelectedAddresses() {
return selectedAddresses;
}
public void setSelectedAddresses(List<Address> selectedAddresses) {
this.selectedAddresses = selectedAddresses;
}
public List<User> getSelectedUsers() {
return selectedUsers;
}
public void setSelectedUsers(List<User> selectedUsers) {
this.selectedUsers = selectedUsers;
}
public String getFindAddress() {
return findAddress;
}
public void setFindAddress(String findAddress) {
this.findAddress = findAddress;
}
} }

@ -237,9 +237,20 @@ ReportNoOptions=Žádná nestavení
MailForm=Odeslání e-mailu MailForm=Odeslání e-mailu
MailFor=Komu: MailFor=Komu:
MailCc=Kopie:
MailBcc=Skrytá:
MailSubject=Předmět: MailSubject=Předmět:
MailSend=Odeslat MailSend=Odeslat
MailAttachement=Příloha: MailAttachement=Příloha:
MailAddressBook=Adresář:
MailUsers=Uživatelé
MailSuppliers=Dodavatelé
MailName=Jméno
MailCompany=Firma
MailToButton=Komu ->
MailCcButton=Kopie ->
MailBccButton=Skrytá ->
Error=Chyba Error=Chyba
ErrorRights=K vykobání této operace nemáte dostatečná oprávnění ErrorRights=K vykobání této operace nemáte dostatečná oprávnění
@ -258,7 +269,6 @@ false=Ne
Information=Informace Information=Informace
Requirements=Požadavky
Orders=Objednávky Orders=Objednávky
MaterialRequirement=Požadavek na materiál MaterialRequirement=Požadavek na materiál
@ -270,7 +280,6 @@ ApprovedRequirementItems=Schválené položky požadavků
CurrentRequirements=Aktuální požadavky CurrentRequirements=Aktuální požadavky
ApprovedOrders=Schválené objednávky ApprovedOrders=Schválené objednávky
BussinessTrips=Služební cesty BussinessTrips=Služební cesty
TravelOrders=Cestovní příkazy
Lists=Seznamy Lists=Seznamy
Settings=Nastavení Settings=Nastavení
Administration=Administrace Administration=Administrace

@ -1,7 +1,8 @@
mail.from=kosef.rokos@gmail.com mail.from=josef.rokos@gmail.com
mail.host=smtp.gmail.com mail.host=smtp.gmail.com
mail.port=587 mail.port=587
mail.useauth=true mail.useauth=true
mail.usessl=true mail.usessl=true
mail.username=josef.rokos@gmail.com mail.username=josef.rokos@gmail.com
mail.password=XXXXXX mail.password=XXXXXXXX
mail.overrideFrom=false

@ -20,6 +20,7 @@
<bean id="mailer" class="info.bukova.isspst.mail.MailerWithAttachement"> <bean id="mailer" class="info.bukova.isspst.mail.MailerWithAttachement">
<constructor-arg ref="mailSender"/> <constructor-arg ref="mailSender"/>
<property name="from" value="${mail.from}"/> <property name="from" value="${mail.from}"/>
<property name="overrideFrom" value="${mail.overrideFrom}"/>
</bean> </bean>
<bean id="simpleMailer" class="info.bukova.isspst.mail.SimpleMailer"> <bean id="simpleMailer" class="info.bukova.isspst.mail.SimpleMailer">

@ -2,8 +2,51 @@
<zk> <zk>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window id="mailWin" border="normal" apply="org.zkoss.bind.BindComposer" <window id="mailWin" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="500px" closable="true"> viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="800px" closable="true">
<caption zclass="form-caption" label="${labels.MailForm}" /> <caption zclass="form-caption" label="${labels.MailForm}" />
<hbox>
<vbox hflex="2">
<hbox>
<label value="${labels.MailAddressBook}"/>
<combobox selectedIndex="@bind(vm.adbType)" readonly="true">
<comboitem label="${labels.MailUsers}"/>
<comboitem label="${labels.MailSuppliers}"/>
</combobox>
</hbox>
<textbox value="@bind(vm.findAddress)" instant="true" onChange="@command('find')" width="100%"/>
<listbox model="@load(vm.users)"
visible="@load(vm.adbType eq 0)"
multiple="true"
selectedItems="@bind(vm.selectedUsers)"
height="350px">
<listhead>
<listheader label="${labels.MailName}"/>
</listhead>
<template name="model">
<listitem disabled="@load(empty each.email)" draggable="true">
<listcell label="@load(each.fullName)"/>
</listitem>
</template>
</listbox>
<listbox model="@load(vm.addressbook)"
visible="@load(vm.adbType eq 1)"
multiple="true"
selectedItems="@bind(vm.selectedAddresses)"
height="350px">
<listhead>
<listheader label="${labels.MailCompany}"/>
</listhead>
<template name="model">
<listitem disabled="@load(empty each.email)" draggable="true">
<listcell label="@load(each.company)"/>
</listitem>
</template>
</listbox>
<button label="${labels.MailToButton}" width="90%" sclass="nicebutton" onClick="@command('addTo')"/>
<button label="${labels.MailCcButton}" width="90%" sclass="nicebutton" onClick="@command('addCc')"/>
<button label="${labels.MailBccButton}" width="90%" sclass="nicebutton" onClick="@command('addBcc')"/>
</vbox>
<vbox hflex="5">
<grid> <grid>
<columns> <columns>
<column hflex="min"/> <column hflex="min"/>
@ -11,7 +54,13 @@ viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="500px"
</columns> </columns>
<rows> <rows>
<row> <row>
<label value="${labels.MailFor}"/> <textbox value="@bind(vm.to)" width="100%"/> <label value="${labels.MailFor}"/> <textbox value="@bind(vm.to)" width="100%" droppable="true" onDrop="@command('addTo')"/>
</row>
<row>
<label value="${labels.MailCc}"/> <textbox value="@bind(vm.cc)" width="100%" droppable="true" onDrop="@command('addCc')"/>
</row>
<row>
<label value="${labels.MailBcc}"/> <textbox value="@bind(vm.bcc)" width="100%" droppable="true" onDrop="@command('addBcc')"/>
</row> </row>
<row> <row>
<label value="${labels.MailSubject }"/> <textbox value="@bind(vm.message.subject)" width="100%"/> <label value="${labels.MailSubject }"/> <textbox value="@bind(vm.message.subject)" width="100%"/>
@ -22,10 +71,12 @@ viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="500px"
</rows> </rows>
</grid> </grid>
<vbox> <vbox>
<ckeditor width="470px" height="200px" value="@bind(vm.message.text)" toolbar="Basic"/> <ckeditor width="540px" height="250px" value="@bind(vm.message.text)" toolbar="Basic"/>
<hbox> <hbox>
<button label="${labels.MailSend}" onClick="@command('send', window=mailWin)"/> <button label="${labels.ButtonStorno}" onClick="mailWin.detach()"/> <button label="${labels.MailSend}" onClick="@command('send', window=mailWin)" disabled="@load(empty vm.to)" sclass="nicebutton"/> <button label="${labels.ButtonStorno}" onClick="mailWin.detach()" sclass="nicebutton"/>
</hbox> </hbox>
</vbox> </vbox>
</vbox>
</hbox>
</window> </window>
</zk> </zk>
Loading…
Cancel
Save