国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用servlet和javamail發送郵件

2019-11-18 14:49:08
字體:
來源:轉載
供稿:網友
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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德保县| 富川| 新津县| 哈巴河县| 西盟| 任丘市| 巴楚县| 新龙县| 大宁县| 许昌县| 乌什县| 寻甸| 澎湖县| 中山市| 若羌县| 红安县| 铅山县| 长岛县| 潞城市| 巧家县| 水城县| 来凤县| 黄梅县| 临高县| 华容县| 临沭县| 固原市| 衡阳市| 鹿泉市| 怀集县| 抚松县| 沙河市| 东平县| 闽侯县| 南华县| 昂仁县| 安陆市| 宁都县| 阿拉善盟| 盐津县| 元朗区|