有些地方发送个邮件到我163邮箱,邮件一打开,邮件正文就是页面,是怎么做到的?纯贴HTML上去不能显示啊.谢谢大家了

解决方案 »

  1.   


    写个获取页面源文件的方法:
    public String getSource(String url) { GetMethod method = new GetMethod(url); try {
    client.executeMethod(method);

    } catch (HttpException e) {

    //e.printStackTrace(); } catch (IOException e) { //e.printStackTrace(); }
    InputStream in = null;
    try {
    in = method.getResponseBodyAsStream();
    } catch (IOException e1) {

    //e1.printStackTrace();
    }
    in = new BufferedInputStream(in);
    Reader r = new InputStreamReader(in);
    int c;
    StringBuffer buffer = new StringBuffer(); try {
    while ((c = r.read()) != -1)
    buffer.append((char)c);
    } catch (IOException e) {

    //e.printStackTrace();
    }
    try {
    in.close();
    } catch (IOException e) {

    //e.printStackTrace();
    }
    method.releaseConnection(); return buffer.toString();
    }
    然后同javamail发送普通邮件一样,只不过在发送内容上这样写:String content=new Test().getSource("http://www.163.com")  //上边的那个方法。 
      

  2.   

    如果你想发送排版的页面,那么需要邮箱支持编辑源代码。另外,邮箱的服务商不一样,同一个页面显示效果也会有差异的。上边的那些代码是用javamail向指定邮箱发送特定的HTML页面。
      

  3.   

    4楼的意思是编写个服务器发送吗?谢谢你了假设我是163的邮箱,应该把www.sina.com.cn的页面当成邮件的正文,发到任意邮箱里?谢谢了,最好能给出个比较详细可行的步骤...谢谢!
      

  4.   

    实例,经测试可用:首先,一个java类,主要有javamail发送邮件的普通方法和获取源文件地址的方法构成;package test;import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.util.Date;import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.GetMethod;public class MailTest {
    public HttpClient client = new HttpClient();

    public void SendHTML(String From,String pwd,String to,String Subject,String url){
    InternetAddress[] address = null;
            String mailserver = "221.130.190.222";//发出邮箱的服务器
            String type = "text/html";//发送邮件格式为html
            String messageText = new Sendmail().getSource(url);//写入你要发送的页面连接,将此页面读为String
            boolean sessionDebug = false;
            try {
                  // 设定所要用的Mail 服务器和所使用的传输协议 
                  java.util.Properties props = System.getProperties();
                  props.put("mail.host", mailserver);
                  props.put("mail.transport.protocol", "smtp");
                  props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证               // 产生新的Session 服务 
                  javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, null);
                  mailSession.setDebug(sessionDebug);
                  Message msg = new MimeMessage(mailSession);              // 设定发邮件的人 
                  msg.setFrom(new InternetAddress(From));              // 设定收信人的信箱 
                  address = InternetAddress.parse(to, false);
                  msg.setRecipients(Message.RecipientType.TO, address);              // 设定信中的主题 
                  msg.setSubject(Subject);              // 设定送信的时间 
                  msg.setSentDate(new Date());              Multipart mp = new MimeMultipart();
                  MimeBodyPart mbp = new MimeBodyPart();              // 设定邮件内容的类型为 text/plain 或 text/html 
                  mbp.setContent(messageText, type + ";charset=gb2312");
                  mp.addBodyPart(mbp);
                  msg.setContent(mp);              Transport transport = mailSession.getTransport("smtp");
                  transport.connect(mailserver, From, pwd);//设发出邮箱的用户名、密码
                  transport.sendMessage(msg, msg.getAllRecipients());
                  transport.close();
            } catch (MessagingException mex) {
                  mex.printStackTrace();
            }
           
    }

    public String getSource(String url) { GetMethod method = new GetMethod(url); try {
    client.executeMethod(method);

    } catch (HttpException e) {

    //e.printStackTrace(); } catch (IOException e) { //e.printStackTrace(); }
    InputStream in = null;
    try {
    in = method.getResponseBodyAsStream();
    } catch (IOException e1) {

    //e1.printStackTrace();
    }
    in = new BufferedInputStream(in);
    Reader r = new InputStreamReader(in);
    int c;
    StringBuffer buffer = new StringBuffer(); try {
    while ((c = r.read()) != -1)
    buffer.append((char)c);
    } catch (IOException e) {

    //e.printStackTrace();
    }
    try {
    in.close();
    } catch (IOException e) {
    //e.printStackTrace();
    }
    method.releaseConnection(); return buffer.toString();
    }}然后,一个JSP测试页面。。(注意:在java类中写main方法是不可以的)
    <%@ page language="java" import="test.*" pageEncoding="gb2312"%>
    <%
    new MailTest().SendHTML("[email protected]", "000000", "[email protected]", "网页格式", "http://www.baidu.com");
    out.print("邮件已经成功发送!");
    //SendHTML方法中参数依次为:发送邮箱用户名、密码、接收邮箱用户名、邮件主题、邮件内容地址。
    //以上测试成功,但不保证邮箱服务器一直正常运行,若因密码错误无法发送,请更换SendHTML中smtp地址以及以上发送方地址和密码。
    %>
      

  5.   

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.GetMethod;
    请问下这三个包从哪来啊?
      

  6.   

    包找到了...Sendmail这个方法从哪来啊?String messageText = new Sendmail().getSource(url);//写入你要发送的页面连接,将此页面读为String
      

  7.   


    不好意思,应该是。String messageText = new MailTest().getSource(url);
    页面邮件中,所有图片应该用绝对路径。
      

  8.   

            GetMethod method = new GetMethod(url);严重: Servlet.service() for servlet jsp threw exception
    java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
    at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:220)
    at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
    at jcore.Test.getSource(Test.java:80)
    at jcore.Test.SendHTML(Test.java:33)
    at org.apache.jsp.test_jsp._jspService(org.apache.jsp.test_jsp:46)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:743)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.DecoderException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1332)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 25 more这行报错~
      

  9.   

    导包了么??这个必须要在JSP页面中测试发送,在main方法中不可以,否则获取不到httpclient中对象。
      

  10.   

    说是缺少commons-codec.jar这个包....这个包,在那个下载包压缩包里没有哇........
      

  11.   

    搞定,那个包在http://commons.apache.org/downloads/download_codec.cgi有下载.....好了,谢谢这位大大了~~~~~分给你了~~~再有问题再发帖吧~~~~~~~
      

  12.   

    这样用不行啊  jsp页面要报错   初学者  请多指教