Formulář a třídy pro odesílání e-mailů. Implementováno odesílání PDF
sestav mailem.multitenant
							parent
							
								
									e26f56fcb9
								
							
						
					
					
						commit
						1283fe627d
					
				@ -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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					mail.from=kosef.rokos@gmail.com
 | 
				
			||||||
 | 
					mail.host=smtp.gmail.com
 | 
				
			||||||
 | 
					mail.port=587
 | 
				
			||||||
 | 
					mail.useauth=true
 | 
				
			||||||
 | 
					mail.usessl=true
 | 
				
			||||||
 | 
					mail.username=josef.rokos@gmail.com
 | 
				
			||||||
 | 
					mail.password=XXXXXX
 | 
				
			||||||
@ -0,0 +1,44 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
 | 
					<beans xmlns="http://www.springframework.org/schema/beans"
 | 
				
			||||||
 | 
						xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
				
			||||||
 | 
						xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
 | 
				
			||||||
 | 
							<property name="host" value="${mail.host}"/>
 | 
				
			||||||
 | 
							<property name="port" value="${mail.port}"/>
 | 
				
			||||||
 | 
							<property name="username" value="${mail.username}"/>
 | 
				
			||||||
 | 
							<property name="password" value="${mail.password}"/>
 | 
				
			||||||
 | 
							<property name="defaultEncoding" value="UTF-8"/>
 | 
				
			||||||
 | 
							<property name="javaMailProperties">
 | 
				
			||||||
 | 
								<props>
 | 
				
			||||||
 | 
									<prop key="mail.smtp.auth">${mail.useauth}</prop>
 | 
				
			||||||
 | 
									<prop key="mail.smtp.starttls.enable">${mail.usessl}</prop>
 | 
				
			||||||
 | 
								</props>
 | 
				
			||||||
 | 
							</property>
 | 
				
			||||||
 | 
						</bean>
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						<bean id="mailer" class="info.bukova.isspst.mail.MailerWithAttachement">
 | 
				
			||||||
 | 
							<constructor-arg ref="mailSender"/>
 | 
				
			||||||
 | 
							<property name="from" value="${mail.from}"/>
 | 
				
			||||||
 | 
						</bean>
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						<bean id="simpleMailer" class="info.bukova.isspst.mail.SimpleMailer">
 | 
				
			||||||
 | 
							<constructor-arg ref="mailSender"/>
 | 
				
			||||||
 | 
							<property name="from" value="${mail.from}"/>
 | 
				
			||||||
 | 
						</bean>
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					<!-- 	<bean id="mailer" class="info.bukova.rsfaktura.services.mail.ThreadMailer"> -->
 | 
				
			||||||
 | 
					<!-- 		<constructor-arg ref="attachementMailer"/> -->
 | 
				
			||||||
 | 
					<!-- 	</bean> -->
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					<!-- 	<bean id="attachementMailer" class="info.bukova.rsfaktura.services.mail.AttachementMailer"> -->
 | 
				
			||||||
 | 
					<!-- 		<constructor-arg ref="tmpStorage"/> -->
 | 
				
			||||||
 | 
					<!-- 		<property name="from" value="josef.rokos@gmail.com"/> -->
 | 
				
			||||||
 | 
					<!-- 		<property name="sender" ref="mailSender"/> -->
 | 
				
			||||||
 | 
					<!-- 	</bean> -->
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					<!-- 	<bean id="messageBuilder" class="info.bukova.rsfaktura.services.mail.MailMessageBuilder"> -->
 | 
				
			||||||
 | 
					<!-- 		<constructor-arg ref="tmpStorage"/> -->
 | 
				
			||||||
 | 
					<!-- 	</bean> -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</beans>
 | 
				
			||||||
@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					<?page title="Mail" contentType="text/html;charset=UTF-8"?>
 | 
				
			||||||
 | 
					<zk>
 | 
				
			||||||
 | 
					<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 | 
				
			||||||
 | 
					<window id="mailWin" border="normal" apply="org.zkoss.bind.BindComposer"
 | 
				
			||||||
 | 
					viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="500px" closable="true">
 | 
				
			||||||
 | 
						<caption zclass="form-caption" label="${labels.MailForm}" />
 | 
				
			||||||
 | 
						<grid>
 | 
				
			||||||
 | 
							<columns>
 | 
				
			||||||
 | 
								<column hflex="min"/>
 | 
				
			||||||
 | 
								<column/>
 | 
				
			||||||
 | 
							</columns>
 | 
				
			||||||
 | 
							<rows>
 | 
				
			||||||
 | 
								<row>
 | 
				
			||||||
 | 
									<label value="${labels.MailFor}"/> <textbox value="@bind(vm.to)" width="100%"/>
 | 
				
			||||||
 | 
								</row>
 | 
				
			||||||
 | 
								<row>
 | 
				
			||||||
 | 
									<label value="${labels.MailSubject }"/> <textbox value="@bind(vm.message.subject)" width="100%"/>
 | 
				
			||||||
 | 
								</row>
 | 
				
			||||||
 | 
								<row>
 | 
				
			||||||
 | 
									<label value="${labels.MailAttachement}"/> <textbox value="@load(vm.attachement)" readonly="true" width="100%"/>
 | 
				
			||||||
 | 
								</row>
 | 
				
			||||||
 | 
							</rows>
 | 
				
			||||||
 | 
						</grid>
 | 
				
			||||||
 | 
						<vbox>
 | 
				
			||||||
 | 
							<ckeditor width="470px" height="200px" value="@bind(vm.message.text)" toolbar="Basic"/>
 | 
				
			||||||
 | 
							<hbox>
 | 
				
			||||||
 | 
								<button label="${labels.MailSend}" onClick="@command('send', window=mailWin)"/> <button label="${labels.ButtonStorno}" onClick="mailWin.detach()"/>
 | 
				
			||||||
 | 
							</hbox>
 | 
				
			||||||
 | 
						</vbox>
 | 
				
			||||||
 | 
					</window>
 | 
				
			||||||
 | 
					</zk>
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue