// post方式发送email public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); File file = doAttachment(request); EmailAttachment attachment = new EmailAttachment(); attachment.setPath(file.getPath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setName(file.getName()); MultiPartEmail email = new MultiPartEmail(); email.setCharset("UTF-8"); email.setHostName("smtp.sina.com"); email.setAuthentication("T-GWAP", "dangdang"); try { email.addTo(parameters.get("to")); email.setFrom(parameters.get("from")); email.setSubject(parameters.get("subject")); email.setMsg(parameters.get("body")); email.attach(attachment); email.send(); request.setAttribute("sendmail.message", "邮件发送成功!"); } catch (EmailException e) { Logger logger = Logger.getLogger(SendAttachmentMailServlet.class); logger.error("邮件发送不成功", e); request.setAttribute("sendmail.message", "邮件发送不成功!"); } request.getRequestDispatcher("/sendResult.jsp").forward(request, response); }
/** Encode a query string. The input Map contains names indexing Object[]. */ public static String encodeQueryString(Map parameters) { final StringBuilder sb = new StringBuilder(100); boolean first = true; try { for (Object o : parameters.keySet()) { final String name = (String) o; final Object[] values = (Object[]) parameters.get(name); for (final Object currentValue : values) { if (currentValue instanceof String) { if (!first) sb.append('&'); sb.append(URLEncoder.encode(name, NetUtils.STANDARD_PARAMETER_ENCODING)); sb.append('='); sb.append( URLEncoder.encode((String) currentValue, NetUtils.STANDARD_PARAMETER_ENCODING)); first = false; } } } } catch (UnsupportedEncodingException e) { // Should not happen as we are using a required encoding throw new OXFException(e); } return sb.toString(); }
/** Combine a path info and a parameters map to form a path info with a query string. */ public static String pathInfoParametersToPathInfoQueryString(String pathInfo, Map parameters) throws IOException { final StringBuilder redirectURL = new StringBuilder(pathInfo); if (parameters != null) { boolean first = true; for (Object o : parameters.keySet()) { final String name = (String) o; final Object[] values = (Object[]) parameters.get(name); for (final Object currentValue : values) { if (currentValue instanceof String) { redirectURL.append(first ? "?" : "&"); redirectURL.append(URLEncoder.encode(name, NetUtils.STANDARD_PARAMETER_ENCODING)); redirectURL.append("="); redirectURL.append( URLEncoder.encode((String) currentValue, NetUtils.STANDARD_PARAMETER_ENCODING)); first = false; } } } } return redirectURL.toString(); }
public static String getContentTypeCharset(String contentType) { final Map<String, String> parameters = getContentTypeParameters(contentType); return (parameters == null) ? null : parameters.get("charset"); }
/** * Request content type. * * @return the content type of the current request */ private ContentType getRequestContentType() { final String s = req.getContentType(); if (s == null) return null; if (s.contains(Response.MULTIPART)) return ContentType.MULTIPART; return TYPE_CONTENTS.get(s); }