<%@ page
import= " javax.mail.*, javax.mail.internet.*,
=>javax.activation.*, java.util.*"
%>
下一步要建立一個特定的資訊,表示郵件已經被發送。你可以自己設定該資訊,通常是:“Your mail has been sent。”
如何建立和發送訊息
建立Message對象的代碼在第二部分已經出現了。現在我們可以處理該訊息。很簡單,只要在對象中使用get和 set properties方法即可。使用post過來的資訊作為參數,例如request.getParameter()就是個很有用的方法,可以擷取post發送來的參數。參見Listing B。
Listing B:
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new
InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>