001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.net.URLDecoder;
006: import java.util.ArrayList;
007: import java.util.Date;
008: import java.util.HashMap;
009:
010: import javax.mail.Address;
011: import javax.servlet.ServletException;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.claros.commons.mail.models.Email;
018: import org.claros.commons.mail.models.EmailHeader;
019: import org.claros.commons.mail.models.EmailPart;
020: import org.claros.commons.mail.protocols.Smtp;
021: import org.claros.commons.mail.utility.Utility;
022: import org.claros.intouch.common.services.BaseService;
023:
024: public class SendReadReceiptMailService extends BaseService {
025:
026: /**
027: *
028: */
029: private static final long serialVersionUID = -7451365138227115574L;
030: private static Log log = LogFactory
031: .getLog(SendReadReceiptMailService.class);
032:
033: /**
034: * The doPost method of the servlet. <br>
035: *
036: * This method is called when a form has its tag value method equals to post.
037: *
038: * @param request the request send by the client to the server
039: * @param response the response send by the server to the client
040: * @throws ServletException if an error occurred
041: * @throws IOException if an error occurred
042: */
043: public void doPost(HttpServletRequest request,
044: HttpServletResponse response) throws ServletException,
045: IOException {
046:
047: response.setHeader("Expires", "-1");
048: response.setHeader("Pragma", "no-cache");
049: response.setHeader("Cache-control", "no-cache");
050: response.setHeader("Content-Type", "text/plain; charset=utf-8");
051:
052: PrintWriter out = response.getWriter();
053:
054: try {
055: String from = URLDecoder.decode(request
056: .getParameter("from"), "UTF-8");
057: String to = URLDecoder.decode(request.getParameter("to"),
058: "UTF-8");
059: String subject = URLDecoder.decode(request
060: .getParameter("subject"), "UTF-8");
061: String date = URLDecoder.decode(request
062: .getParameter("date"), "UTF-8");
063:
064: Email email = new Email();
065: EmailHeader header = new EmailHeader();
066:
067: Address adrs[] = Utility.stringToAddressArray(from);
068: header.setFrom(adrs);
069:
070: Address tos[] = Utility.stringToAddressArray(to);
071: header.setTo(tos);
072:
073: header
074: .setSubject(getText(request, "read") + ": "
075: + subject);
076: header.setDate(new Date());
077:
078: email.setBaseHeader(header);
079:
080: ArrayList parts = new ArrayList();
081: EmailPart bodyPart = new EmailPart();
082: bodyPart.setContentType("text/html; charset=UTF-8");
083:
084: String body = getText(request, "read.receipt.email.message");
085: body = org.claros.commons.utility.Utility
086: .replaceAllOccurances(
087: body,
088: "#FROM#",
089: org.claros.intouch.common.utility.Utility
090: .htmlCheck(org.claros.commons.utility.Utility
091: .updateTRChars(from)));
092: body = org.claros.commons.utility.Utility
093: .replaceAllOccurances(body, "#DATE#", date);
094: bodyPart.setContent(body);
095: parts.add(0, bodyPart);
096: email.setParts(parts);
097:
098: Smtp smtp = new Smtp(getConnectionProfile(request),
099: getAuthProfile(request));
100: HashMap sendRes = smtp.send(email, false);
101: Address[] sent = (Address[]) sendRes.get("sent");
102: if (sent == null || sent.length == 0) {
103: out.print("fail");
104: } else {
105: out.print("ok");
106: }
107: } catch (Exception e) {
108: log
109: .error("Error occured while sending read receipt mail : "
110: + e);
111: out.print("fail");
112: }
113: }
114: }
|