jspEmail:sendMailServlet

 package javamailtest;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;

public class sendMailServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    String smtpServer = request.getParameter("mailserver");
    String mailTo = request.getParameter("mailto");
    String mailFrom = request.getParameter("mailfrom");
    String subject = request.getParameter("subject");
    String content = request.getParameter("content");
    System.out.println(smtpServer+mailTo+mailFrom+subject+content);
    System.out.println(request.getParameter("file1"));

    try {
      Properties props = new Properties();
      //Properties prop = System.getProperties();取得Properties的另一种方法
      //props.put("mail.transport.protocol","smtp");
      props.put("mail.smtp.host","smtp.163.com");
      //props.put("mail.smtp.port","25");
      props.put("mail.smtp.auth","true");
      Session mailSession = Session.getInstance(props);
      mailSession.setDebug(true);

      /*
       Properties props=new Properties();//也可用Properties props = System.getProperties();
       props.put("mail.smtp.host","smtp.163.com");//存储发送邮件服务器的信息
       props.put("mail.smtp.auth","true");//同时通过验证
       Session s=Session.getInstance(props);//根据属性新建一个邮件会话
       s.setDebug(true);

 

       MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象

       //设置邮件
       InternetAddress from=new InternetAddress("liboofsc@163.com");
       message.setFrom(from);//设置发件人
       InternetAddress to=new InternetAddress(tto);
       message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
       message.setSubject(ttitle);//设置主题
       message.setText(tcontent);//设置信件内容
       message.setSentDate(new Date());//设置发信时间

       //发送邮件
       message.saveChanges();//存储邮件信息
       Transport transport=s.getTransport("smtp");
       transport.connect("smtp.163.com","liboofsc","642364");//以smtp方式登录邮箱
       transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
        //已设好的收件人地址
       transport.close();


       */
      //MimeMessage的对象用来装整个邮件的对象
      MimeMessage msg = new MimeMessage(mailSession);

      //Multipart mp = new MimeMultipart();
      //BodyPart的对象用来装附件型式内容和脚本型式邮件内容
      //MimeBodyPart bp = new MimeBodyPart();
      FileDataSource fileds = new FileDataSource("c://csb.log");
      //在BodyPart对象中添加一个附件
      //bp.setDataHandler(new DataHandler(fileds));
      //bp.setFileName(fileds.getName());

      //bp.setText(content);
      //在BodyPart对象中添加一个html脚本
      /*
      bp.setContent(
    "<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
    "<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
        "<div align=center><a href=http://www.csdn.net> csdn </a></div>", "text/html;charset=GB2312");
*/
      //mp.addBodyPart(bp);
      //msg.setContent(mp);
      InternetAddress address = new InternetAddress("lyws@tfol.com");
      address.setPersonal("lyws");
      msg.setFrom(address);
      msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("lyws@tfol.com"));
      //msg.setSentDate(new Date());
      //加入邮件标题
      msg.setSubject(subject);

      /**
       * 邮件的正文内容通常可以分几个部分,包括文本正文,脚本正文及附件。
       * 每一部分通常都用一个MimeBodyPart对象来装
       * 接一下把几部分都加入Multipart对象,Multipart对象包含邮件正文的全部内容
       * 最后把Multipart对象放入MimeMessage对象中,正文内容就设定完成
          */

      //定义一个Multipart对象用来装整个邮件的正文部分
      Multipart multipart = new MimeMultipart();
      //定义一个MimeBodyPart对象,用来装邮件的文本内容然后加入Multipart对象中
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(content);
      multipart.addBodyPart(messageBodyPart);
      //定义一个MimeBodyPart对象,用来装邮件的附件内容然后加入Multipart对象中
      MimeBodyPart fileBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource("c://aa.jpg");
      fileBodyPart.setDataHandler(new DataHandler(source));
      fileBodyPart.setFileName(source.getName());
      multipart.addBodyPart(fileBodyPart);
      //定义一个MimeBodyPart对象,用来装邮件的脚本内容然后加入Multipart对象中
      MimeBodyPart htmlBodyPart = new MimeBodyPart();
      htmlBodyPart.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
                              "<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
        "<div align=center><a href=http://www.csdn.net> csdn </a></div>", "text/html;charset=GB2312");
      multipart.addBodyPart(htmlBodyPart);

      //把Multipart对象加入到MimeMessage对象中
      msg.setContent(multipart);

      //msg.setText(content);
      msg.saveChanges();
      Transport trans = mailSession.getTransport("smtp");
      //System.out.println("1111111111111111");

        trans.connect("smtp.163.com", "lyws", "800518");

      //System.out.println("1111111111111111");
      //trans.connect( (String) props.get("mail.smtp.host"), "liboofsc", "642364");
      trans.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
      //msg.writeTo(System.out);
      out.println("邮件发送成功"+mailTo);
      trans.close();
    }
    catch (MessagingException ex) {
      System.out.println(ex);
    }
    out.close();
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
  //Clean up resources
  public void destroy() {
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值