java发送网页邮件

| 阅读数:--次| 作者:小豆豆
摘要:java发送网页邮件
java代码
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class mailHtml {

 /** *//**
  * @param args
 * @throws IOException 
 * @author Administrator
  */
 public static void main(String[] args) throws IOException{
   String tto="girl_lihuiyue@126.com";
  String ttitle="发送HTML格式邮件";
  String tcontent="<html xmlns='http://www.w3.org/1999/xhtml'>"
      +"<head>"
      +"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
      +"<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7'/>"
      +"<style type='text/css'>"
      +"*{margin:0;padding:0;font:normal 12px '宋体';color:#333;text-decoration:none;list-style:none;}"
      +"h1{font:bold 14px '宋体'; color:#464c66;}"
      +"input, img{border:none;}"
      +".clear{clear:both;}"
      +"a:hover{color:#094eb6;}"
      +".header{height:120px; background:url(http://192.168.0.3/xw/news/images/header.jpg) no-repeat center;}"
      +".logo{margin:20px 0 0;}"
      +".wrapper{background:url(http://192.168.0.3/xw/news/images/sprite_x.gif) repeat-x 0 -116px;border-top:2px solid #0a8dba;}"
      +".wrapper .key{color:#e60;}"
      +".header .center, .main{width:900px;overflow:hidden;margin:0 auto;}"
      +".nav{height:38px;padding:8px 0 0;}"
      +".nav ul li{float:left; background:url(http://192.168.0.3/xw/news/images/sprite_a.gif) no-repeat 0 -10px; padding:5px 0;}"
      +".nav ul li a{ color:#ffsdad; font-weight:bold; font-size:14px; padding:0 20px;}"
      +".main_content{background:url(http://192.168.0.3/xw/news/images/b_main_content.gif) repeat-x; overflow:hidden;}"
      +".main_two{ background:#FFF;}"
      +".footer{ text-align:center; padding:30px 0;}"
      +"#xiyibu{ width:124px; height:26px; background:url(http://192.168.0.3/xw/news/images/lantiao-1.gif); color:#FFF; font-weight:bold;}"
      +".three{ width:900px; overflow:hidden; margin:0 auto;}"
      +"</style>"
      +"<title>寻网账号激活</title>"
      +"</head>"
      +"<body> "
      +"<div class='header'>"
      +"  <div class='center'>"
      +"    <div class='logo'><img src='http://192.168.0.3/xw/news/images/logo.gif' /></div>"
      +"  </div>"
      +"  "
      +"</div>"
      +"<div class='wrapper'>"
      +"  <div class='main'>"
      +"  <div class='nav'>"
      +"    <ul>"
      +"     <li><a href='http://192.168.0.3/xw/index.html'>首页</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/feep.html'>付费寻人</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/feeitem.html'>付费寻物</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/freep.html'>免费寻人</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/freeitem.html'>免费寻物</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/receive.html'>招领</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/declare.html'>申明</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/thank.html'>感谢信</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/honor.html'>光荣榜</a></li>"
      +"     <li><a href='http://192.168.0.3/xw/bbs/index.html?id=1'>论坛</a></li>"
      +"    </ul>"
      +"   </div>"
      +"  <!--         nav end         -->"
      +"      <div class='main_two'>"
      +"      <table width='830' height='251' border='0' cellpadding='0' cellspacing='0'>"
      +"        <tr>"
      +"          <td width='776' height='45' align='left' valign='middle'><img src='http://192.168.0.3/xw/news/images/jh.jpg' width='191' height='57' /></td>"
      +"        </tr>"
      +"        <tr>"
      +"          <td height='194' align='center' valign='top'><table width='400' border='0' cellspacing='0' cellpadding='0' background='http://192.168.0.3/xw/news/images/lanse.jpg'>"
      +"            <tr>"
      +"              <td height='186' align='center' valign='middle'><table width='375' height='167' border='0' cellpadding='0' cellspacing='0'>"
      +"                <tr>"
      +"                  <td width='375' height='42' align='left' valign='top'><img src='http://192.168.0.3/xw/news/images/tu-1.gif' width='20' height='20' /> 请输入您注册时填写的电子邮件地址,我们会将您的电子邮件名发到您的邮箱</td>"
      +"                </tr>"
      +"                <tr>"
      +"                  <td height='37' align='left' valign='middle'>&nbsp;</td>"
      +"                </tr>"
      +"                <tr>"
      +"                  <td height='40' align='center' valign='middle'>"
      +"                  <a href='http://192.168.0.3/xw/login/jihuozhanghao.html?username=girl_lihuiyue@126.com'>"
      +"                 <input type='button' name='button' id='xiyibu' value='激活账号'/></a></td>"
      +"                </tr>"
      +"                <tr>"
      +"                  <td height='48' align='left' valign='middle'>账号被激活后才可以正常使用,您可以<a href='http://192.168.0.3/xw/login/zhuce.html'>点击这里</a>重新进行注册</td>"
      +"                </tr>"
      +"              </table></td>"
      +"            </tr>"
      +"          </table></td>"
      +"        </tr>"
      +"      </table>"
      +"    </div>"
      +"  </div>"
      +"</div>"
      +"<div class='footer'>"
      +" <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>寻网简介</a> ┊ <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>广告服务</a> ┊<a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>联系我们</a> ┊<a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>诚聘英才</a> ┊ <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>网站律师</a> ┊ <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>会员注册</a> ┊ <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>产品答疑</a>"
      +" <br/><br/>"
      +" 版权所有 京ICP证000000  <a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>互联网新闻信息服务许可</a>"
      +"</div>"
      +"</body>"
      +"</html>";
  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("girl_lihuiyue@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=gb2312");//给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","girl_lihuiyue@126.com","******");
  transport.sendMessage(message,message.getAllRecipients());
  transport.close();
  System.out.println("发送成功!");
  }catch (Exception e){
   e.printStackTrace();
  }
 }
 public static String readFile(String file) throws IOException{
        StringBuffer sf=new StringBuffer();
        File f = new File(file);
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        String s;
        while ((s = dis.readLine()) != null) {
            System.out.println(s);
         sf.append(s);
        }
        return sf.toString();
}
 }
返回顶部
学到老代码浏览 关闭浏览