Use Javamail in JSP (below)

zhaozj2021-02-16  54

Part II: About JavaMail

Documentation

The documentation with the downloaded JavaMail API is useful. You can find it on /Docs/javadocs/index.html under JavaMail. The second part mainly analyzes the components of the mail program. You can get more information from reading documents.

Components Send Mail Need to use Javamail, which makes it easy to use the operation of the message.

Attribute object

JavaMail needs to create a file that is "mail.smtp.host" used to send information.

Properties PROPS = New Properties (); Props.Put ("mail.smtp.host", "smtp.jspinsider.com");

Dialogue

All JavaMail-based programs require at least one or all of the dialogue targets.

Session SendmailSession; SendmailSession = session.getInstance (Props, NULL);

transmission

The transmission of the mail is only sent or subject to two states. JavaMail describes both different states as transmission and storage. Transfer will send a message and save the message.

Transport Transport; Transport = SendmailSession.getTransport ("SMTP");

Use JavaMail to save us a lot of time. JavaMail can replace all SMTP work.

Note: JavaMail does not fully support all email to send charges. It currently only supports IMAP, SMTP, and POP3, in addition to this, you only wait for new JavaMail versions or its own development protocols.

Information object

The information object will reflect the email you sent true.

Message newMessage = New MimeMailSession;

This is all the four objects we need. The next step will be how to add objects to JSP.

Part III: Javamail and JSP combination

Create JSP

Below we will begin to combine them together. The most important point is to confirm that the classification is based on the page. Also remember to mark java.util.date on the email.

<% @ page import = "javax.mail. *, javax.mail.internet. *, javax.activation. *, java.util. *"%>

Second, create a confirmation information sent by the message. Confirm that the information can be arbitrary, generally commonly used "Your mail has been sent."

How information is created and sent

We have discussed the creation of information objects in the second part. We will follow the information below. This is as simple as the properties of the setting information object. You can implement this operation by the following program.

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"));

The information will now be sent. It is very simple to achieve through JavaMail.

Transport.send (NewMessage);

Combine all components together

All components are now complete. Now put them in JSP. Pay attention to each error message and feed back it to the user. The code is as follows, you can use them directly: sample jsp email utility using javamail <% @ page import = "javax.mail. *, Javax.mail.internet. *, Javax.activation. *, Java.util. *" %> JSP Meets JavaMail, What a sweet combo. </ title> </ head> <body> <% try {profmentness pROPS = New Properties (); SESSION SendmailSession; store store transport; sendMailSession = Session.getInstance (props, null); props.put ( "mail.smtp.host", "smtp.jspinsider.com"); 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) {outputln (m.toString ());}%> </ body> </ h TML></p> <p>You will quickly experience the convenience of JavaMail, JSP and JavaMail will be the future hope.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-18363.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="18363" 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.037</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 = 'lnmwQNJi_2BX2jqtbpZQGqRLtZ2sIj2NDLCys_2BuYyjX_2BIdzvpplHyJIqAqEY6uobeVw8ewWjb4kP4YXIvQnNgnaQ_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>