1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46 @Named
47 @SessionScoped
48 public class Email implements Serializable {
49
50 private static final long serialVersionUID = 1544680932114626710L;
51
52
53
54
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
101
102
103
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 }