JSP and JavaMail (6) --- Write Flexible Mail Send Programs

zhaozj2021-02-16  50

7. Writing a flexible sender

This section did not add something fresh, but in the previous content, you can make you flexibly send the email you want to send. After reading this section, you will feel very useful.

The changes after the changes are as follows:

-------------------------------------------------- -----------------------------------------

Write an email </ title></p> <p></ hEAD></p> <p><body></p> <p><form action = "testall.jsp" Method = "post" name = "form1"></p> <p><table width = "75" border = "0" align = "center" cellspacing = "1" bgcolor = "# 006600" class = "black"></p> <p><tr bgcolor = "# ffffff"></p> <p><TD Width = "24%"> Recident Address: </ TD></p> <p><td width = "76%"> <input name = "to" type = "text" id = "to"> </ td></p> <p></ TR></p> <p><tr bgcolor = "# ffffff"></p> <p><TD> Topic: </ TD></p> <p><TD> <input name = "title" type = "text" id = "title"> </ td></p> <p></ TR></p> <p><tr></p> <p><TD Height = "18" colSpan = "2" BGColor = "# ffffff"> letter type</p> <p><select name = "emailType" id = "emailType"></p> <p><option value = "text / plain" SELECTED> Text </ option></p> <p><option value = "text / html"> html </ option></p> <p></ select> </ td></p> <p></ TR></p> <p><tr></p> <p><TD Height = "53" colspan = "2" BGColor = "# fff"> <textarea name = "content" cols = "50" rows = "5" id = "content"> </ textarea> </ td></p> <p></ TR></p> <p><tril = "center"></p> <p><TD colspan = "2" BGCOLOR = "# ffffff"> Accessories 1 (custom): <input name = "fj1" type = "text" id = "fj1"></p> <p>(Enter text information) </ td></p> <p></ TR></p> <p><tril = "center" valign = "bottom"></p> <p><TD COLSPAN = "2" BGColor = "# fff"> Accessories 2 (local):</p> <p><input name = "fj2" type = "file" id = "fj2" size = "10"> </ td></p> <p></ TR></p> <p><tril = "center"></p> <p><TD colspan = "2" bgcolor = "# fff"> Accessories 3 (remote):</p> <p><input name = "fj3" type = "text" id = "fj3" value = "http: //"></p> <p>(Input URL) </ td></p> <p></ TR></p> <p><tril = "center"></p> <p><td color = "2" bgcolor = "# ffffff"> <input type = "submit" name = "subs" value = "send"></p> <p><Input Type = "reset" name = "submit2" value = "reset"> </ td></p> <p></ TR></p> <p></ TABLE></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>The JSP program for processing the email is as follows:</p> <p>-------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><% Request.SetCharacterencoding ("GB2312");%></p> <p><% @ Page Import = "java.util. *, javax.mail. *"%></p> <p><% @ Page Import = "javax.mail.internet. *"%></p> <p><% @ page import = "javax.activation. *"%> <! - To send attachments must be introduced into the library-></p> <p><% @ page import = "java.net. *"%> <! - Use URL class -></p> <p><html></p> <p><HEAD></p> <p><meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> <title> Send success </ title></p> <p></ hEAD></p> <p><body></p> <p><%</p> <p>Try {</p> <p>String TTO = Request.getParameter ("to");</p> <p>String Ttitle = Request.GetParameter ("Title");</p> <p>String emailType = Request.getParameter ("emailType"); // Get an Email type</p> <p>String tcontent = Request.getParameter ("Content");</p> <p>String Tfj1 = Request.getParameter ("fj1");</p> <p>String TFJ2 = Request.getParameter ("fj2");</p> <p>String TFJ3 = Request.getParameter ("fj3");</p> <p>Properties PROPS = New Properties ();</p> <p>Props.PUT ("Mail.smtp.host", "127.0.0.1");</p> <p>Props.Put ("mail.smtp.auth", "true");</p> <p>Session s = session.getInstance (PROPS);</p> <p>S.SETDEBUG (TRUE);</p> <p>MimeMessage Message = New MimeMessage (s);</p> <p>// Set the sender / recipient / topic / send time to the message object</p> <p>InternetAddress from = new internetaddress ("xxf@cafe.com");</p> <p>Message.SetFrom (from);</p> <p>InternetAddress to = New InternetAddress (TTO);</p> <p>Message.setRecipient (Message.RecipientType.to, To);</p> <p>Message.setSubject (TTITLE);</p> <p>Message.setSentDate (New Date ());</p> <p>Multipart mm = new mimemultipart (); // New a MIMEMULTIPART object is used to store multiple Bodypart objects</p> <p>/ / Set the letter text content</p> <p>Bodypart MDP = new mimebodypart (); // Newly built a Bodypart object that stores letters content</p> <p>Mdp.setContent (tcontent, emailtype "; charset = GB2312"); // Set content and format / encoding method for Bodypart objects</p> <p>mm.addbodypart (MDP); // Add BodyPart containing the contents of the letter to the MimeMultipart object</p> <p>/ / Set an attachment 1 of the Letter (Custom Attachment: Directly add the text content to the custom file as an attachment)</p> <p>MDP = new mimebodypart (); // Newly built a Bodypart</p> <p>DataHandler DH = New DataHandler (TFJ1, "Text / Plain; Charset = GB2312);</p> <p>// Newly build a DataHandler object and set its content and format / encoding method</p> <p>MDP.SetFileName ("text.txt"); // Plus this sentence will be sent as an attachment, otherwise the text content will be used as a letter</p> <p>MDP.SetDataHandler (DH); // Set content for the Bodypart object as DHMM.AddBodypart (MDP); // Add BodyPart containing attachments to the MimeMultipart object</p> <p>/ / Set an accessory 2 of the letter (with the local file as an attachment)</p> <p>MDP = new mimebodypart ();</p> <p>FileDataSource FDS = New FileDataSource (TFJ2);</p> <p>DH = New DataHandler (FDS);</p> <p>INT DDD = Tfj2.lastIndexof ("//");</p> <p>String fname = tfj2.substring (ddd); // extraction file name</p> <p>String ffname = new string (FName.GetBytes ("GB2312"), "ISO8859-1"); // Handling the file name is Chinese</p> <p>Mdp.setFileName (ffname); / / can be inconsistent with the original file name, but it is best</p> <p>MDP.SetDataHandler (DH);</p> <p>mm.addbodypart (MDP);</p> <p>/ / Set annex 3 of the letter (using a remote file as an attachment)</p> <p>MDP = new mimebodypart ();</p> <p>URL URLFJ = New URL (TFJ3);</p> <p>URLDataSource UR = New UrldataSource (URLFJ);</p> <p>// 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.</p> <p>DH = New DataHandler (UR);</p> <p>INT TTT = Tfj3.lastIndexof ("/");</p> <p>String urlname = tfj3.substring (ttt);</p> <p>// String urlfname = new string (urlname.getbytes ("GB2312"), "ISO8859-1"); // I don't know what is going on, here I can't handle Chinese issues.</p> <p>MDP.SetFileName (UrlName);</p> <p>MDP.SetDataHandler (DH);</p> <p>mm.addbodypart (MDP);</p> <p>Message.setContent (mm); // Put the MM as the content of the message object</p> <p>Message.savechanges ();</p> <p>Transport Transport = S.GetTransport ("SMTP");</p> <p>Transport.connect ("127.0.0.1", "XXF", "Coffee");</p> <p>Transport.sendMessage (Message, Message.GetallRecipients ());</p> <p>Transport.close ();</p> <p>%></p> <p><div align = "center"></p> <p><p> <font color = "# ff6600"> Send success! </ font> </ p></p> <p><p> <a href="ystemail.jsp"> Go see my mailbox </a <br></p> <p><br></p> <p><a href="index.htm"> Send a sealed </a> </ p></p> <p></ div></p> <p><%</p> <p>} catch (messagingException e) {</p> <p>Out.println (E.TOString ());</p> <p>}</p> <p>%></p> <p></ body></p> <p></ html></p> <p>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 :)!</p> <p>(to be continued)</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24741.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="24741" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.074</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'sgbeSZMWgt6n7dJaYrJLuQm6xNXiZdDiY3OP7n14l0GAMl_2FFvcCqKleWQYMlMU_2BVdNpF6HnCMkdVArEORCT1Jg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>