发送带有接缝的活动邀请
發送郵件邀請時,您需要發送帶有附件的電子郵件,其中包含有關特定事件的信息。 我將創建用于發送邀請的簡單模板和發送者類。
Seam 2.x包含其他組件,這些組件負責發送郵件和創建模板。 要使用此功能,我們需要在應用程序中包括接縫郵件組件,而使用maven時,我們可以這樣做:
<dependency><groupId>org.jboss.seam</groupId><artifactId>jboss-seam-mail</artifactId> </dependency>接縫模板機制使我們可以像對標準jsp頁面一樣創建郵件模板。 它很容易學習,并且還可以使用標準的jsp標記(如果使用的話是JSF)。 在此示例中,我將不對接縫郵件模板化機制的使用做進一步的介紹。在下面,您可以找到用于發送邀請的模板的簡單示例。
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <m:message xmlns="http://www.w3.org/1999/xhtml"xmlns:m="http://jboss.com/products/seam/mail"xmlns:h="http://java.sun.com/jsf/html"><m:header name="Content-Class" value="urn:content-classes:calendarmessage"/><m:from name="Test Mail" address="no-reply-mail@invitation.example" /><m:to name="Igor Madjeric">#{eventInvitation.recipient}</m:to><m:subject><h:outputText value="Test invitation" /> </m:subject><m:body><m:attachment contentType="text/calendar;method=CANCEL" fileName="invitation.ics"> BEGIN:VCALENDAR METHOD:REQUEST PRODID:-//Direct Scouts GmbH//INA//DE VERSION:2.0 CALSCALE:GREGORIAN BEGIN:VEVENT DTSTAMP:#{eventInvitation.currentDateAsString} DTSTART:#{eventInvitation.startAsString} DTEND:#{eventInvitation.endAsString} SUMMARY;CHARSET=UTF-8:Test invitation UID:de827ded-5fc8-4ceb-af1b-b8d9cfbcbca8 ATTENDEE;ROLE=OWNER;PARTSTAT=NEEDS-ACTION;RSVP=FALSE:MAILTO:#{eventInvitation.recipient} ORGANIZER:MAILTO:xxx@gmail.com LOCATION;CHARSET=UTF-8:#{eventInvitation.location} DESCRIPTION;CHARSET=UTF-8:#{eventInvitation.description} SEQUENCE:0 PRIORITY:5 CLASS:PUBLIC STATUS:CONFIRMED TRANSP:OPAQUE BEGIN:VALARM ACTION:DISPLAY DESCRIPTION:REMINDER TRIGGER;RELATED=START:-PT00H15M00S END:VALARM END:VEVENT END:VCALENDAR</m:attachment> </m:body> </m:message>如您所見,它并不復雜,就像制作JSP頁面一樣。 創建邀請時,您需要注意UID,它是為其創建邀請的事件的唯一標識符,因此,如果以后需要更改有關該事件的某些內容,則只需要使用相同的UID。 在此示例中,我創建了EventInvitation模型類,其中包含事件所需的數據。 它們不包含大量數據,但是如果您需要更多數據,則可以對其進行擴展。
package ba.codecentric.mail.sender.model;import java.text.SimpleDateFormat; import java.util.Date; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope;@Name("eventInvitation") @Scope(ScopeType.PAGE) public class EventInvitation {SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");private String recipient;private String location;private String description;/* Start and stop dates */private Date start;private Date end;public String getRecipient() {return recipient;}public void setRecipient(String recipient) {this.recipient = recipient;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getStartAsString() {return iCalendarDateFormat.format(start);}public String getEndAsString() {return iCalendarDateFormat.format(end);}public Date getStart() {return start;}public void setStart(Date start) {this.start = start;}public Date getEnd() {return end;}public void setEnd(Date end) {this.end = end;}public String getCurrentDateAsString() {return iCalendarDateFormat.format(new Date());}@Overridepublic String toString() {return "EventInvitation [recipient=" + recipient + ", location="+ location + ", description=" + description + ", start=" + start + ", end=" + end + "]";} }它是具有頁面范圍的簡單接縫組件,與頁面停留時間相同。 從模板中可以看到,我們使用方法..AsString設置日期值。 這是因為,我們不能簡單地使用原始日期來表示邀請中的日期,而是使用下一種格式“ yyyyMMdd'T'HHmm'00'”來格式化日期。
為了填寫日期,我使用了下一個簡單表格:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:rich="http://richfaces.org/rich" xmlns:a4j="http://richfaces.org/a4j" xmlns:f="http://java.sun.com/jsf/core" template="/includes/template.xhtml"><!-- main content --> <ui:define name="MainContent"> <div class="WelcomeContent"> <a4j:form> <rich:panel header="Welcom To Seam Mail Invitation Sender" style="width: 315px"> Start:<rich:calendar value="#{eventInvitation.start}" popup="true" datePattern="dd/M/yy hh:mm a" showApplyButton="true" cellWidth="24px" cellHeight="22px" style="width:200px"/> <br /> End:<rich:calendar value="#{eventInvitation.end}" popup="true" datePattern="dd/M/yy hh:mm a" showApplyButton="true" cellWidth="24px" cellHeight="22px" style="width:200px"/> <br /> Location:<h:inputText value="#{eventInvitation.location}" id="location"/> <br /> Description:<h:inputText value="#{eventInvitation.description}" id="description"/> <br /> Recipient:<h:inputText value="#{eventInvitation.recipient}" id="recipient"/> <a4j:commandButton value="Send Invitation" action="#{mailInvitationSender.sendInvitation}" reRender="info" /> <h:panelGroup id="info"> <h:outputText value="Status: #{mailInvitationSender.status} " rendered="#{not empty mailInvitationSender.status}" /> </h:panelGroup> </rich:panel> </a4j:form> </div> </ui:define> </ui:composition>填充數據的簡單頁面沒有什么復雜的。 最后,我們將研究發送者類。
package ba.codecentric.mail.sender.controller.impl;import javax.ejb.Remove; import javax.ejb.Stateful;import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.faces.Renderer; import org.jboss.seam.log.Log;import ba.codecentric.mail.sender.controller.LocalMailInvitationSender; import ba.codecentric.mail.sender.model.EventInvitation;@Name("mailInvitationSender") @Scope(ScopeType.CONVERSATION) @Stateful public class StandardMailInvitationSender implements LocalMailInvitationSender {private static final String STATUS_SUCCESS = "SUCCESS"; private static final String STATUS_FAIL = "FAIL";private static String INVITATION_TEMPLATE = "/invitation.xhtml";@Logger private static Log LOG;// Component used for rendering template. @In(create = true) private Renderer renderer;@In private EventInvitation eventInvitation;private String status;public String getStatus() { return status; }public void setStatus(String status) { this.status = status; }@Override public void sendInvitation() { LOG.info("Send invitation method is called!"); try { LOG.debug(eventInvitation); renderer.render(INVITATION_TEMPLATE); status = STATUS_SUCCESS; } catch (Exception e) { LOG.error(e); status = STATUS_FAIL; } LOG.info("Invitation sending:" + status); }@Remove public void done() { LOG.debug("Bean removed!"); } }這是簡單的類,使用渲染器基于模板創建郵件。 因此,沒有什么特別的。 當然,您需要在components.xml中配置郵件會話,但這是簡單的配置。 您需要在components.xml中添加下一行:
<mail:mail-session session-jndi-name="java:/Mail" />就這樣。 您的應用程序已準備好發送邀請:)。 注意:components.xml中的上述行將創建郵件會話組件,seam將使用該組件來發送郵件。 例如,如果您使用JBoss 4.xx,則可以在“ mail-service.xml”文件中編輯配置。 但是如何配置郵件會話不在本文的討論范圍內,如果您需要有關此主題的更多信息,可以查看我較早的文章“ Configure Seam Mail”。
參考:在Igor Madjeric博客上,從我們的JCG合作伙伴 Igor Madjeric 發送帶有Seam的活動邀請 。
翻譯自: https://www.javacodegeeks.com/2012/10/sending-event-invitations-with-seam.html
總結
以上是生活随笔為你收集整理的发送带有接缝的活动邀请的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用MRUnit测试Hadoop程序
- 下一篇: MapReduce:处理数据密集型文本处