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, 2023 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 jakarta.mail.Message;
14: import jakarta.mail.Session;
15: import jakarta.mail.Transport;
16: import jakarta.mail.internet.InternetAddress;
17: import jakarta.mail.internet.MimeMessage;
18: import jakarta.servlet.jsp.JspException;
19: import jakarta.servlet.jsp.tagext.BodyTagSupport;
20:
21: import java.net.InetAddress;
22: import java.util.Properties;
23:
24: /**
25: * Custom tag for sending messages.
26: */
27: public class SendTag extends BodyTagSupport {
28: private String body;
29: private String cc;
30: private String host;
31: private String recipients;
32: private String sender;
33: private String subject;
34:
35: /**
36: * host attribute setter method.
37: */
38: public void setHost(String host) {
39: this.host = host;
40: }
41:
42: /**
43: * recipient attribute setter method.
44: */
45: public void setRecipients(String recipients) {
46: this.recipients = recipients;
47: }
48:
49: /**
50: * sender attribute setter method.
51: */
52: public void setSender(String sender) {
53: this.sender = sender;
54: }
55:
56: /**
57: * cc attribute setter method.
58: */
59: public void setCc(String cc) {
60: this.cc = cc;
61: }
62:
63: /**
64: * subject attribute setter method.
65: */
66: public void setSubject(String subject) {
67: this.subject = subject;
68: }
69:
70: /**
71: * Method for processing the end of the tag.
72: */
73: public int doEndTag() throws JspException {
74: Properties props = System.getProperties();
75:
76: try {
77:• if (host != null)
78: props.put("mail.smtp.host", host);
79:• else if (props.getProperty("mail.smtp.host") == null)
80: props.put("mail.smtp.host", InetAddress.getLocalHost().
81: getHostName());
82: } catch (Exception ex) {
83: throw new JspException(ex.getMessage());
84: }
85: Session session = Session.getDefaultInstance(props, null);
86:
87: Message msg = new MimeMessage(session);
88: InternetAddress[] toAddrs = null, ccAddrs = null;
89:
90: try {
91:• if (recipients != null) {
92: toAddrs = InternetAddress.parse(recipients, false);
93: msg.setRecipients(Message.RecipientType.TO, toAddrs);
94: } else
95: throw new JspException("No recipient address specified");
96:
97:• if (sender != null)
98: msg.setFrom(new InternetAddress(sender));
99: else
100: throw new JspException("No sender address specified");
101:
102:• if (cc != null) {
103: ccAddrs = InternetAddress.parse(cc, false);
104: msg.setRecipients(Message.RecipientType.CC, ccAddrs);
105: }
106:
107:• if (subject != null)
108: msg.setSubject(subject);
109:
110:• if ((body = getBodyContent().getString()) != null)
111: msg.setText(body);
112: else
113: msg.setText("");
114:
115: Transport.send(msg);
116:
117: } catch (Exception ex) {
118: throw new JspException(ex.getMessage());
119: }
120:
121: return (EVAL_PAGE);
122: }
123: }
124: