`
qiuyujia
  • 浏览: 171978 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java HttpClient 抓取页面 发送邮件

阅读更多
   这两天编辑部要求做个健康网上调查,将用户填写的信息发到指定的邮箱,调查信息内容是蛮大的,所以先设置个模板页面(JSP),用于发送邮箱显示用,然后通过用户的请求request.getQueryString()来获取提交的参数及值,再将它传回到模板中,想到用HttpClient来抓取模板页面HTML源码;再发送到指定的邮箱;
  1、根据请求得到参数和值;如:name=qyj&age=28
        String query=req.getQueryString();

2、 将参数传回模板,用于发送到邮箱用的HTML,再用httpClient 来抓取
        HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod("../js/mailContent.jsp?"+query);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());

byte[] responseBody = getMethod.getResponseBody();
str=new String(responseBody,"UTF-8");//内容体编码

3、发送邮件
public  String sendEmail(String str){
if(str.indexOf("null")>0)
str=str.replaceAll("null", " ");
String returnInfo="";
String tto="wu945@126.com";//
String ttitle="健康调表";
String tcontent=str;
Properties props=new Properties();
props.put("mail.smtp.host","smtp.126.com");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);

MimeMessage message=new MimeMessage(s);

//  给消息对象设置发件人/收件人/主题/发信时间
try{
InternetAddress from=new InternetAddress("t1717g@126.com");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());


//  给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,"text/html;charset=utf-8");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("smtp.126.com","t1717g","123456.hz");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("发送成功!");
returnInfo="success";
}catch (Exception e){
e.printStackTrace();
// TODO: handle exception

return returnInfo;
}
一开始是想用什么技术直接提取调查用的界面发到邮箱,但是图片啊,样式啊,主要是用户填写的信息不能直接获得,于是罢了;
其次,碰到的最大的问题就是编码问题,又编写了sevlet请求时的编码过滤

public void doFilter(ServletRequest req, ServletResponse rep,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest)req;
request.setCharacterEncoding("UTF-8");
HttpServletResponse respones = (HttpServletResponse)rep;
respones.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(request, respones);
}
不过得在web.xml中配置下
   <filter>
<filter-name>encodeFilter</filter-name>
<filter-class>com.juhe.yibao.EncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/jsp/mailContent.jsp</url-pattern>
</filter-mapping>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics