在jsp中發送email !

來源:互聯網
上載者:User
作者/出處:Blueski編譯(包含了2篇文章,前篇用了sun.net.smtp,後一篇用了javamail.
一、我們可以通過任何支援sun規範中的sun.net.smtp包的JSP引擎(如JSWDK)發送mail。
(警告:使用內建的internal Sun規範包,這將影響到你的jsp程式的可移植性。)
以下scriptlet利用SmtpClient類在jsp檔案中發送email。
<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
String from="gseshadri@hotmail.com";
String to="govind@jguru.com, govi@bigfoot.com";
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();
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更豐富或者說更複雜,但它是可移植的。這裡重新建立了一個 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, // sender
String[] astrTo, // recipient(s)
String[] astrBCC, // bcc recipient(s), optional
String strSubject, // subject
boolean debugging)
{
ms_strFrom = strFrom; // who the message is from
ms_astrTo = astrTo; // who (plural) the message is to
ms_debugging = debugging; // who (plural) the message is to
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.