01: package org.claros.mini.actions;
02:
03: import java.util.ArrayList;
04: import java.util.Date;
05: import java.util.HashMap;
06:
07: import javax.mail.Address;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10: import org.apache.struts.action.ActionForm;
11: import org.apache.struts.action.ActionForward;
12: import org.apache.struts.action.ActionMapping;
13:
14: import org.claros.commons.configuration.PropertyFile;
15: import org.claros.commons.mail.models.Email;
16: import org.claros.commons.mail.models.EmailHeader;
17: import org.claros.commons.mail.models.EmailPart;
18: import org.claros.commons.mail.protocols.Smtp;
19: import org.claros.commons.mail.utility.Utility;
20: import org.claros.mini.common.BaseLoggedAction;
21: import org.claros.mini.models.ComposeFormBean;
22:
23: /**
24: * @version 1.0
25: * @author Umut Gökbayrak
26: */
27: public class SendMailAction extends BaseLoggedAction {
28:
29: public ActionForward myExecute(ActionMapping mapping,
30: ActionForm form, HttpServletRequest request,
31: HttpServletResponse response) throws Exception {
32: Smtp smtp = new Smtp(getConnectionProfile(request),
33: getAuthProfile(request));
34: ComposeFormBean bean = (ComposeFormBean) request.getSession()
35: .getAttribute("ComposeFormBean");
36:
37: // learn the global charset setting.
38: String charset = PropertyFile.getConfiguration(
39: "/config/config.xml")
40: .getString("common-params.charset");
41:
42: // now create a new email object.
43: Email email = new Email();
44:
45: EmailHeader header = new EmailHeader();
46: header.setFrom(Utility.stringToAddressArray(bean.getFrom()));
47:
48: header.setTo(Utility.stringToAddressArray(bean.getTo()));
49: header.setCc(Utility.stringToAddressArray(bean.getCc()));
50: header.setBcc(Utility.stringToAddressArray(bean.getBcc()));
51: header.setSubject(bean.getSubject());
52: header.setDate(new Date());
53: email.setBaseHeader(header);
54:
55: String messageBody = bean.getMessageBody();
56: ArrayList parts = new ArrayList();
57: EmailPart bodyPart = new EmailPart();
58: bodyPart.setContentType("text/plain; charset=" + charset);
59: bodyPart.setContent(messageBody);
60: parts.add(0, bodyPart);
61:
62: // attach some files...
63: ArrayList attachments = (ArrayList) request.getSession()
64: .getAttribute("attachments");
65: if (attachments != null) {
66: parts.addAll(attachments);
67: }
68: email.setParts(parts);
69:
70: // it is time to send the email object message
71: HashMap sendRes = smtp.send(email);
72: // MimeMessage msg = (MimeMessage)sendRes.get("msg");
73:
74: // if we fail to send the message to any of the recepients
75: // we should make a report about it to the user.
76: Address[] sent = (Address[]) sendRes.get("sent");
77: Address[] invalid = (Address[]) sendRes.get("invalid");
78: Address[] fail = (Address[]) sendRes.get("fail");
79: if (sent != null) {
80: request.setAttribute("sent", sent);
81: }
82: if (invalid != null) {
83: request.setAttribute("invalid", invalid);
84: }
85: if (fail != null) {
86: request.setAttribute("fail", fail);
87: }
88:
89: // time to do cleaning
90: request.getSession().setAttribute("ComposeFormBean", null);
91:
92: return mapping.findForward("success");
93: }
94: }
|