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