网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.返回对象.
.Properties类不直接支持多语言属.
.JFreeChart在Webwork中的应用.
.关于窗口的操作详谈.
.按位“与”运算符 (&).
.J2ME程序开发全方位基础讲解汇总.
.在清单里设置主类.
.Java Socket编程(三)服务器Soc.
.Path 属性.
.用Java程序生成文本的捷径.
.Java修饰符之static.
.SCJP 1.4 认证的初级教程.
.Linux 是Java、XML和CORBA的最佳.
.Eclipse快速上手指南 (1).
.升级到Hibernate3.0的理由.
.了解Hibernate的FlushMode.NEVER.
.JavaSocket网络编程初级入门.
.JBuilder2005实现重构之重命名.
.XMLHTTP---介绍.
.学习javabean.

使用servlet和javamail发送邮件

发表日期:2008-1-5


import Java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.rmi.transport.Transport;

import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;

public class EmailServlet extends HttpServlet {

  //default value for mail server address, in case the user
  //doesn't provide one
  private final static String DEFAULT_SERVER = "mail.attbi.com";

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    String smtpServ = DEFAULT_SERVER;

    String from = "java2s@yourserver.com";

    String to = "yourname@yourserver.com";

    String subject = "subject line";

    String emailContent = "emailContent";

    response.setContentType("text/Html");
    java.io.PrintWriter out = response.getWriter();
    out
        .println("<html><head><title>Email message sender</title></head><body>");

    try {

      sendMessage(smtpServ, to, from, subject, emailContent);

    } catch (Exception e) {

      throw new ServletException(e.getMessage());

    }

    out.println("<h2>The message was sent sUCcessfully</h2>");

    out.println("</body></html>");

  } //doPost

  private void sendMessage(String smtpServer, String to, String from,
      String subject, String emailContent) throws Exception {

    Properties properties = System.getProperties();

    //populate the 'Properties' object with the mail
    //server address, so that the default 'Session'
    //instance can use it.
    properties.put("mail.smtp.host", smtpServer);

    Session session = Session.getDefaultInstance(properties);

    Message mailMsg = new MimeMessage(session);//a new email message

    InternetAddress[] addresses = null;

    try {

      if (to != null) {

        //throws 'AddressException' if the 'to' email address
        //violates RFC822 syntax
        addresses = InternetAddress.parse(to, false);

        mailMsg.setRecipients(Message.RecipientType.TO, addresses);

      } else {

        throw new MessagingException(
            "The mail message requires a 'To' address.");

      }

      if (from != null) {

        mailMsg.setFrom(new InternetAddress(from));

      } else {

        throw new MessagingException(
            "The mail message requires a valid 'From' address.");

      }

      if (subject != null)
        mailMsg.setSubject(subject);

      if (emailContent != null)
        mailMsg.setText(emailContent);

      //Finally, send the meail message; throws a 'SendFailedException'
      //if any of the message's recipients have an invalid adress
      Transport.send(mailMsg);

    } catch (Exception exc) {

      throw exc;

    }

  }//sendMessage

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    doPost(request, response);

  }

}//EmailServlet
上一篇:使用DOM解析器解析XML 人气:1185
下一篇:使用JavaMail发送邮件 人气:1340
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐