Formulář a třídy pro odesílání e-mailů. Implementováno odesílání PDF
sestav mailem.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package info.bukova.isspst.mail;
|
||||
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
|
||||
public class MailMessage extends SimpleMailMessage {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -3240551712942170018L;
|
||||
|
||||
private boolean html;
|
||||
private String attachementName;
|
||||
private byte[] attachementData;
|
||||
private String contentType;
|
||||
|
||||
public boolean isHtml() {
|
||||
return html;
|
||||
}
|
||||
|
||||
public void setHtml(boolean html) {
|
||||
this.html = html;
|
||||
}
|
||||
|
||||
public byte[] getAttachementData() {
|
||||
return attachementData;
|
||||
}
|
||||
|
||||
public void setAttachementData(byte[] attachementData) {
|
||||
this.attachementData = attachementData;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
public String getAttachementName() {
|
||||
return attachementName;
|
||||
}
|
||||
|
||||
public void setAttachementName(String attachementName) {
|
||||
this.attachementName = attachementName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package info.bukova.isspst.mail;
|
||||
|
||||
public interface Mailer {
|
||||
|
||||
public void send(MailMessage message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package info.bukova.isspst.mail;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.mail.MailParseException;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
|
||||
public class MailerWithAttachement implements Mailer {
|
||||
|
||||
private JavaMailSender sender;
|
||||
private String from;
|
||||
|
||||
public MailerWithAttachement(JavaMailSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(MailMessage message) {
|
||||
MimeMessage mimeMessage = sender.createMimeMessage();
|
||||
|
||||
try {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
|
||||
if (message.getFrom() == null || message.getFrom().isEmpty()) {
|
||||
if (from == null || from.isEmpty()) {
|
||||
message.setFrom("tomcat@is.bukova.info");
|
||||
} else {
|
||||
message.setFrom(from);
|
||||
}
|
||||
}
|
||||
|
||||
helper.setFrom(message.getFrom());
|
||||
helper.setTo(message.getTo());
|
||||
helper.setSubject(message.getSubject());
|
||||
helper.setText(message.getText(), message.isHtml());
|
||||
|
||||
if (message.getAttachementData() != null) {
|
||||
InputStreamSource source = new ByteArrayResource(message.getAttachementData());
|
||||
helper.addAttachment(message.getAttachementName(), source, message.getContentType());
|
||||
}
|
||||
} catch (MessagingException e) {
|
||||
throw new MailParseException(e);
|
||||
}
|
||||
sender.send(mimeMessage);
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package info.bukova.isspst.mail;
|
||||
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
||||
public class SimpleMailer implements Mailer {
|
||||
|
||||
private JavaMailSender sender;
|
||||
private String from;
|
||||
|
||||
public SimpleMailer(JavaMailSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(MailMessage message) {
|
||||
if (message.getFrom() == null || message.getFrom().isEmpty()) {
|
||||
if (from == null || from.isEmpty()) {
|
||||
message.setFrom("tomcat@is.bukova.info");
|
||||
} else {
|
||||
message.setFrom(from);
|
||||
}
|
||||
}
|
||||
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package info.bukova.isspst.ui.mail;
|
||||
|
||||
import info.bukova.isspst.mail.MailMessage;
|
||||
import info.bukova.isspst.mail.Mailer;
|
||||
import info.bukova.isspst.reporting.Generator;
|
||||
import info.bukova.isspst.reporting.GeneratorFactory;
|
||||
import info.bukova.isspst.reporting.ReportDefinition;
|
||||
|
||||
import org.zkoss.bind.annotation.BindingParam;
|
||||
import org.zkoss.bind.annotation.Command;
|
||||
import org.zkoss.bind.annotation.ExecutionArgParam;
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.zk.ui.select.annotation.WireVariable;
|
||||
import org.zkoss.zul.Window;
|
||||
|
||||
public class MailForm {
|
||||
|
||||
@WireVariable
|
||||
private Mailer mailer;
|
||||
@WireVariable
|
||||
private ReportDefinition reportDefinition;
|
||||
@WireVariable
|
||||
private GeneratorFactory genFactory;
|
||||
private MailMessage message;
|
||||
private String to;
|
||||
private String attachement;
|
||||
private boolean report;
|
||||
|
||||
@Init
|
||||
public void init(@ExecutionArgParam("report") Boolean report) {
|
||||
message = new MailMessage();
|
||||
message.setHtml(true);
|
||||
this.report = report;
|
||||
if (report) {
|
||||
attachement = "report.pdf";
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
public void send(@BindingParam("window") Window window) {
|
||||
message.setTo(to);
|
||||
if (report) {
|
||||
Generator generator = genFactory.createGenerator(reportDefinition);
|
||||
message.setAttachementData(generator.generate());
|
||||
message.setAttachementName("report.pdf");
|
||||
message.setContentType("application/pdf");
|
||||
}
|
||||
|
||||
mailer.send(message);
|
||||
window.detach();
|
||||
}
|
||||
|
||||
public MailMessage getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(MailMessage message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getAttachement() {
|
||||
return attachement;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
package info.bukova.isspst.ui.reporting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.zkoss.bind.annotation.Command;
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.zk.ui.Executions;
|
||||
import org.zkoss.zul.Window;
|
||||
|
||||
public class ReportVM {
|
||||
|
||||
@@ -8,5 +14,13 @@ public class ReportVM {
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
@Command
|
||||
public void send() {
|
||||
Map<String, Boolean> params = new HashMap<String, Boolean>();
|
||||
params.put("report", true);
|
||||
Window mailForm = (Window) Executions.createComponents("/app/mailForm.zul", null, params);
|
||||
mailForm.doModal();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user