td>
TR>
TABLE>
form>
body>
html>
The JSP program for processing the email is as follows:
-------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% Request.SetCharacterencoding ("GB2312");%>
<% @ Page Import = "java.util. *, javax.mail. *"%>
<% @ Page Import = "javax.mail.internet. *"%>
<% @ page import = "javax.activation. *"%>
<% @ page import = "java.net. *"%>
Send success title>
hEAD>
<%
Try {
String TTO = Request.getParameter ("to");
String Ttitle = Request.GetParameter ("Title");
String emailType = Request.getParameter ("emailType"); // Get an Email type
String tcontent = Request.getParameter ("Content");
String Tfj1 = Request.getParameter ("fj1");
String TFJ2 = Request.getParameter ("fj2");
String TFJ3 = Request.getParameter ("fj3");
Properties PROPS = New Properties ();
Props.PUT ("Mail.smtp.host", "127.0.0.1");
Props.Put ("mail.smtp.auth", "true");
Session s = session.getInstance (PROPS);
S.SETDEBUG (TRUE);
MimeMessage Message = New MimeMessage (s);
// Set the sender / recipient / topic / send time to the message object
InternetAddress from = new internetaddress ("xxf@cafe.com");
Message.SetFrom (from);
InternetAddress to = New InternetAddress (TTO);
Message.setRecipient (Message.RecipientType.to, To);
Message.setSubject (TTITLE);
Message.setSentDate (New Date ());
Multipart mm = new mimemultipart (); // New a MIMEMULTIPART object is used to store multiple Bodypart objects
/ / Set the letter text content
Bodypart MDP = new mimebodypart (); // Newly built a Bodypart object that stores letters content
Mdp.setContent (tcontent, emailtype "; charset = GB2312"); // Set content and format / encoding method for Bodypart objects
mm.addbodypart (MDP); // Add BodyPart containing the contents of the letter to the MimeMultipart object
/ / Set an attachment 1 of the Letter (Custom Attachment: Directly add the text content to the custom file as an attachment)
MDP = new mimebodypart (); // Newly built a Bodypart
DataHandler DH = New DataHandler (TFJ1, "Text / Plain; Charset = GB2312);
// Newly build a DataHandler object and set its content and format / encoding method
MDP.SetFileName ("text.txt"); // Plus this sentence will be sent as an attachment, otherwise the text content will be used as a letter
MDP.SetDataHandler (DH); // Set content for the Bodypart object as DHMM.AddBodypart (MDP); // Add BodyPart containing attachments to the MimeMultipart object
/ / Set an accessory 2 of the letter (with the local file as an attachment)
MDP = new mimebodypart ();
FileDataSource FDS = New FileDataSource (TFJ2);
DH = New DataHandler (FDS);
INT DDD = Tfj2.lastIndexof ("//");
String fname = tfj2.substring (ddd); // extraction file name
String ffname = new string (FName.GetBytes ("GB2312"), "ISO8859-1"); // Handling the file name is Chinese
Mdp.setFileName (ffname); / / can be inconsistent with the original file name, but it is best
MDP.SetDataHandler (DH);
mm.addbodypart (MDP);
/ / Set annex 3 of the letter (using a remote file as an attachment)
MDP = new mimebodypart ();
URL URLFJ = New URL (TFJ3);
URLDataSource UR = New UrldataSource (URLFJ);
// Note: The parameters used here can only be used for URL objects, cannot be a string of the URL, incorrectly in the previous class (please understand), here to correct it.
DH = New DataHandler (UR);
INT TTT = Tfj3.lastIndexof ("/");
String urlname = tfj3.substring (ttt);
// String urlfname = new string (urlname.getbytes ("GB2312"), "ISO8859-1"); // I don't know what is going on, here I can't handle Chinese issues.
MDP.SetFileName (UrlName);
MDP.SetDataHandler (DH);
mm.addbodypart (MDP);
Message.setContent (mm); // Put the MM as the content of the message object
Message.savechanges ();
Transport Transport = S.GetTransport ("SMTP");
Transport.connect ("127.0.0.1", "XXF", "Coffee");
Transport.sendMessage (Message, Message.GetallRecipients ());
Transport.close ();
%>
Send success! font> p>
Go see my mailbox
Send a sealed p>
div>
<%
} catch (messagingException e) {
Out.println (E.TOString ());
}
%>
body>
html>
So far, we basically learn to send a variety of types of mail. But how do we get your own email, let's talk about it again :)!
(to be continued)
转载请注明原文地址:https://www.9cbs.com/read-24741.html