当前位置: 首页 > 图灵资讯 > 技术篇> 在jsp中发送email

在jsp中发送email

来源:图灵教育
时间:2024-02-19 11:27:45
一、我们可以通过任何支持sun规范中的sunn.net.JSP引擎(如JSWDK)的smtp包发送mail。 (警告:使用内置internal Sun标准包,这将影响您的jsp程序的可移植性。) 以下scriptlet利用smtpclient在jsp文件中发送email。 <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %><%String from="[email protected]";String to="[email protected], [email protected]";try{SmtpClient client = new SmtpClient("mail.xxxxx.xxx");client.from(from);client.to(to);PrintStream message = client.startMessage();message.println("To: " + to);message.println("Subject: Sending email from JSP!");message.println("This was sent from a JSP page!");message.println("This was sent from a JSP page!");message.println();message.println("Cool beans! :-)");message.println();message.println("Govind Seshadri");message.println("jGuru.com");message.println();client.closeServer();}catch (IOException e){ System.out.println("ERROR SENDING EMAIL:"+e);}%>二、 Javamail是官方的 Java mail API,可参考 http://java.sun.com/products/javamail/。尽管API比较 sun.net.smtp.Smtpclient更丰富或更复杂,但它是可移植的。尽管API比较 sun.net.smtp.Smtpclient更丰富或更复杂,但它是可移植的。在这里重建了一个 MailSender类,它包含 JavaMail API。如下所示: // ms_ prefix is for MailSender class variables// str prefix is for String// astr prefix is for array of Strings// strbuf prefix is for StringBuffers, etc.public MailSender(String strFrom, // senderString[] astrTo, // recipient(s)String[] astrBCC, // bcc recipient(s), optionalString strSubject, // subjectboolean debugging){ms_strFrom = strFrom; // who the message is fromms_astrTo = astrTo; // who (plural) the message is toms_debugging = debugging; // who (plural) the message is to// set the hostProperties props = new Properties();props.put("mail.smtp.host", ms_strSMTPHost);// create some properties and get the default SessionSession session = Session.getDefaultInstance(props, null);session.setDebug(ms_debugging);try {// create a messagems_msg = new MimeMessage(session);// set the fromInternetAddress from = new InternetAddress(strFrom);ms_msg.setFrom(from);// set the toInternetAddress[] address = new InternetAddress[astrTo.length];for (int i = 0; i astrTo.length; ++i){address[i] = new InternetAddress(astrTo[i]);}ms_msg.setRecipients(Message.RecipientType.TO, address);// set the bcc recipientsif (astrBCC != null){address = new InternetAddress[astrBCC.length];for (int i = 0; i astrBCC.length; ++i){eh.dbg("astrBCC[" + i + "] is: '" + astrBCC[i] + "'");address[i] = new InternetAddress(astrBCC[i]);}ms_msg.setRecipients(Message.RecipientType.BCC, address);}// set the subjectms_msg.setSubject(strSubject);// set up the string buffer which will hold the messagems_strbufMsg = new StringBuffer();} catch (MessagingException mex) {mex.printStackTrace(System.err);} catch (Exception ex) {ex.printStackTrace(System.err);}}public void ms_add(String strText){ms_strbufMsg.append(strText);}public void ms_send(){try {// set the content as plain textms_msg.setContent(new String(ms_strbufMsg), "text/plain");// and awayTransport.send(ms_msg);} catch (Exception ex) {System.out.println("Caught exception in MailSender.ms_send: " + ex);}}