Use the smtpclient class to send simple text emails, but cannot send emails containing attachments.
To a receiver, it is a built-in JDK package, so it is more universal.
The javamail package provides excellent functions to write any powerful email.
System.
Source code.
<% --
Function: JSP routine. use JSP to send an email.
%>
<% @ Page contenttype = "text/html; GBK" %>
<% @ Page import = "sun.net. SMTP. smtpclient" %>
<HTML>
<Head>
<Title>
Send email
</Title>
</Head>
<Body>
<Form method = "Post" Action = "sendmail. jsp">
Recipient address: <input type = "text" name = "to">
Sender address: <input type = "text" name = "from">
Subject: <input type = "text" name = "server" value = "smtp.163.com">
Letter content: <textarea name = "content" Cols = "73" rows = "5"> </textarea>
<Input type = "Submit" value = "send">
</Form>
Enter required options
<Br>
<%
// Obtain parameters
String to = request. getparameter ("");
String from = request. getparameter ("from ");
String subject = request. getparameter ("subject ");
String Server = request. getparameter ("server ");
String content = request. getparameter ("content ");
// If the parameters are obtained and the conditions are met, enter the mail program segment
If (! To. Equals ("")&&! From. Equals ("")&&! Subject. Equals ("")&&! Server. Equals (""))
{
Tyr {
Smtpclient mail = new smtpclient (server );
Mail. From (from );
Mail. To ();
Java. Io. printstream mailout =
Mail. startmessage ();
Mailout. println ("from:" + from );
Mailout. println ("to:" + );
Mailout. println ("Subject:" + subject );
Mailout. println (content );
Mailout. Print ("/R/N ");
Mailout. Flush ();
Mail. closeserver ();
Out. println ("sent successfully, you can send the next letter ");
}
Catch (exception e ){
Out. println ("error:" + E. tostring ());
}
}
%>
</Body>
</Html>