View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.mail;
18  
19  import java.io.Serializable;
20  
21  import javax.annotation.Resource;
22  import javax.enterprise.context.SessionScoped;
23  import javax.inject.Named;
24  import javax.mail.Address;
25  import javax.mail.Message;
26  import javax.mail.Session;
27  import javax.mail.Transport;
28  import javax.mail.internet.InternetAddress;
29  import javax.mail.internet.MimeMessage;
30  
31  /**
32   * <p>
33   * {@link Email} contains all the business logic for the application, and also serves as the controller for the JSF view.
34   * </p>
35   * <p>
36   * It contains address, subject, and content for the <code>email</code> to be sent.
37   * </p>
38   * <p>
39   * The {@link #send()} method provides the business logic to send the email
40   * </p>
41   * 
42   * @author Joel Tosi
43   * 
44   */
45  
46  @Named
47  @SessionScoped
48  public class Email implements Serializable {
49  
50      private static final long serialVersionUID = 1544680932114626710L;
51  
52      /**
53       * Resource for sending the email. The mail subsystem is defined in either standalone.xml or domain.xml in your respective
54       * configuration directory.
55       */
56      @Resource(mappedName = "java:jboss/mail/Default")
57      private Session mySession;
58  
59      private String to;
60  
61      private String from;
62  
63      private String subject;
64  
65      private String body;
66  
67      public String getTo() {
68          return to;
69      }
70  
71      public void setTo(String to) {
72          this.to = to;
73      }
74  
75      public String getFrom() {
76          return from;
77      }
78  
79      public void setFrom(String from) {
80          this.from = from;
81      }
82  
83      public String getSubject() {
84          return subject;
85      }
86  
87      public void setSubject(String subject) {
88          this.subject = subject;
89      }
90  
91      public String getBody() {
92          return body;
93      }
94  
95      public void setBody(String body) {
96          this.body = body;
97      }
98  
99      /**
100      * Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
101      * is not handled in this example.
102      * 
103      * @throws Exception
104      */
105     public void send() throws Exception {
106         Message message = new MimeMessage(mySession);
107         message.setFrom(new InternetAddress(from));
108         Address toAddress = new InternetAddress(to);
109         message.addRecipient(Message.RecipientType.TO, toAddress);
110         message.setSubject(subject);
111         message.setContent(body, "text/plain");
112         Transport.send(message);
113     }
114 }