熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

如何使用JSP來發送郵件

2013-11-15 12:08:16  來源: JSP教程 

  MESSAGEJSP

  <TABLE width= border= align="center" cellPadding= cellSpacing=>
<FORM action=message_dojsp method=post>
<TBODY>
<TR>
<TD width= height=>信件標題<FONT color=#ff>*</FONT></TD>
<TD><INPUT class=input id=topic name=topic>
</TD>
</TR>
<TR>
<TD width= height= valign="top">反饋信息<FONT color=#ff>*</FONT></TD>
<TD><TEXTAREA class=input id=body name=body rows= cols=""></TEXTAREA>
</TD>
</TR>
<TR>
<TD width= height=>如何稱呼<FONT color=#ff>*</FONT></TD>
<TD><INPUT class=input id=name name=name>
</TD>
</TR>
<TR>
<TD width= height=>你的郵件<FONT color=#ff>* </FONT></TD>
<TD height=><INPUT class=input id=email name=email>
</TD>
</TR>
<TR>
<TD width= height=>你的電話</TD>
<TD><INPUT name=phone class=input id="phone">
</TD>
</TR>
<TR>
<TD width= height=>公司地址<BR>
(或網址)</TD>
<TD><INPUT class=input id=http size= name=http>
</TD>
</TR>
<TR>
<TD align=right width=>&nbsp;</TD>
<TD height=><INPUT class=bt type=submit value=提交 name=tt onclick="return check()">
</TD>
</TR>
</FORM>
</TABLE>

  處理頁面:

  <%@ page contentType="text/html; charset=gb" language="java" import="javasql*javautil*javaxmail*cncmpycompany*javaxmailinternet*javaxactivation*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN" "
<html>
<head>
<jsp:useBean id="company" scope="page" class="cncmpycompanycompany"></jsp:useBean>
<jsp:useBean id="comCtl" scope="page" class="cncmpycompanycomCtl"></jsp:useBean>
<meta httpequiv="ContentType" content="text/html; charset=gb">
<title>留言本發送</title>
</head>
<body>

  <%
//int id=;
company=comCtlselectCompany();
String email="";
email=companygetEmail();
if(email==null) email="nbybc@sinacom";

  String username=requestgetParameter("name");
String topic=requestgetParameter("topic");
String body=requestgetParameter("body");
String email=requestgetParameter("email");
String phone=requestgetParameter("phone");
String ("http");
//String email=new String(requestgetParameter("email")getBytes("ISO_"));
//outprint(username+"<br>");
//outprint(email+"<br>");
try{

Properties props = new Properties();
propsput("mailsmtphost" "smtpsohucom");
propsput("mailtransportprotocol" "smtp");
propsput("mailsmtpauth" "true");
javaxmailSession sessmail = javaxmailSessiongetInstance(props);
Message msg = new MimeMessage(sessmail);
String From="sonic@sohucom";
msgsetFrom(new InternetAddress(From));
String To=email;
InternetAddress[] address=null;
address = InternetAddressparse(Tofalse);
// newMessagesetRecipient(MessageRecipientTypeTO new InternetAddress(requestgetParameter("to")));
msgsetRecipients(MessageRecipientTypeTO address);
//設定收信人
msgsetSubject("網上留言信息:"); //設定EMail的主旨
msgsetSentDate(new javautilDate()); //設定送信的時間
msgsetText("標題:"+topic+"內容"+body+"聯系人Email"+email+"聯系電話"+phone+"公司網址/地址"+http); //設定傳送信的MIME Type
Transport transport = sessmailgetTransport("smtp");
transportconnect("smtpsohucom""sonic""");
transportsendMessage(msg msggetAllRecipients());
transportclose();

outprint("<script language=javascript>alert(已經成功留言!!);windowlocation;</script>");

}catch(Exception e){
outprintln(etoString());
}

  %>
</body>
</html>


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20375.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.