Skip to content

Package: SendTag

SendTag

nameinstructionbranchcomplexitylinemethod
SendTag()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
doEndTag()
M: 123 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 32 C: 0
0%
M: 1 C: 0
0%
setCc(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setHost(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setRecipients(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSender(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSubject(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2001, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package demo;
12:
13: import java.util.*;
14: import java.net.*;
15: import jakarta.mail.*;
16: import jakarta.mail.internet.*;
17: import jakarta.servlet.jsp.*;
18: import jakarta.servlet.jsp.tagext.*;
19:
20: /**
21: * Custom tag for sending messages.
22: */
23: public class SendTag extends BodyTagSupport {
24: private String body;
25: private String cc;
26: private String host;
27: private String recipients;
28: private String sender;
29: private String subject;
30:
31: /**
32: * host attribute setter method.
33: */
34: public void setHost(String host) {
35:         this.host = host;
36: }
37:
38: /**
39: * recipient attribute setter method.
40: */
41: public void setRecipients(String recipients) {
42:         this.recipients = recipients;
43: }
44:
45: /**
46: * sender attribute setter method.
47: */
48: public void setSender(String sender) {
49:         this.sender = sender;
50: }
51:
52: /**
53: * cc attribute setter method.
54: */
55: public void setCc(String cc) {
56:         this.cc = cc;
57: }
58:
59: /**
60: * subject attribute setter method.
61: */
62: public void setSubject(String subject) {
63:         this.subject = subject;
64: }
65:
66: /**
67: * Method for processing the end of the tag.
68: */
69: public int doEndTag() throws JspException {
70:         Properties props = System.getProperties();
71:         
72:         try {
73:•         if (host != null)
74:                 props.put("mail.smtp.host", host);
75:•         else if (props.getProperty("mail.smtp.host") == null)
76:                 props.put("mail.smtp.host", InetAddress.getLocalHost().
77:                  getHostName());
78:         } catch (Exception ex) {
79:          throw new JspException(ex.getMessage());
80:         }
81:         Session session = Session.getDefaultInstance(props, null);
82:                 
83:         Message msg = new MimeMessage(session);
84:         InternetAddress[] toAddrs = null, ccAddrs = null;
85:
86:         try {
87:•         if (recipients != null) {
88:                 toAddrs = InternetAddress.parse(recipients, false);
89:                 msg.setRecipients(Message.RecipientType.TO, toAddrs);
90:          } else
91:                 throw new JspException("No recipient address specified");
92:
93:•         if (sender != null)
94:                 msg.setFrom(new InternetAddress(sender));
95:          else
96:                 throw new JspException("No sender address specified");
97:
98:•         if (cc != null) {
99:                 ccAddrs = InternetAddress.parse(cc, false);
100:                 msg.setRecipients(Message.RecipientType.CC, ccAddrs);
101:          }
102:
103:•         if (subject != null)
104:                 msg.setSubject(subject);
105:
106:•         if ((body = getBodyContent().getString()) != null)
107:                 msg.setText(body);
108:          else
109:                 msg.setText("");
110:
111:          Transport.send(msg);
112:         
113:         } catch (Exception ex) {
114:          throw new JspException(ex.getMessage());
115:         }
116:
117:         return(EVAL_PAGE);
118: }
119: }
120: