JSP and Java Mail API

xiaoxiao2021-03-06  42

JSP and Java Mail API Author: Anonymous From: Unknown Summary: Developing Java Mail API is a public API SUN continuing efforts to provide the framework for Java developers a good example. Promote a public framework, opposing a resolution of the supplier, fully predicting an increasingly open development environment. The structure of the Java Mail API itself proves that one of its developers - the workload of software development should depend on the complexity of the application itself and the degree of control required by the developer. In other words, Java Mail APIs are as simple as possible. At first glance, the total number of classes and classes between Java Mail APIs may make people misunderstand long learning time. In fact, once it is officially started, you will find that the API does not loses a simple tool to join a robust mail / communication support in the application. Establish a JavaMail use environment, which software needs to first, you need to install the JavaMail API. There are now two commonly used JavaMail API versions: 1.2 and 1.1.3. Although version 1.2 is the latest version, version 1.1.3 contains version 1.2.1 of Java 2 Platform, Enterprise Edition, J2EE, so many people still use it. · JavaMail 1.2 installation To use JavaMail 1.2 API, download the JavaMail 1.2 implementation, unpack the javamail-1_2.zip file, and add the mail.jar file to the classpath. In addition to the core class, with version 1.2 implementation, SMTP, IMAP4, and POP3 suppliers are available. · JavaMail 1.1.3 installation To use JavaMail 1.1.3 API, download the JavaMail 1.1.3 implementation, unwield the javamail1_1_3.zip file, and add the mail.jar file to your classpath. In addition to the core class, the SMTP and IMAP4 suppliers are available with the version 1.1.3 implementation. If you access a POP server with JavaMail 1.1.3, download and install a POP3 vendor. Sun has a JavaMail implementation. After downloading and unwilling the pop31_1_1.zip file, add POP3.jar to your ClassPath. This is followed by the installation of JavaBeans Activation Framework. All versions of the JavaMail API require JavaBeans Activation Framework to support input and corresponding processing of any data block. There is not much function, but you can find this basic MIME support in many browsers and email tools. After downloading the frame, unpack the JAF1_0_1.zip file and add the activation.jar file to the ClassPath. For JavaMail 1.2 users, you should now add mail.jar and activation.jar files to classpath. For JavaMail 1.1.3 users, now you should have added mail.jar, pop3.jar, and activation.jar files to classpath. If you don't plan to use POP3, you don't have to add POP3.jar to ClassPath. If you don't want to change the ClassPath environment variable, copy the JAR file to the lib / EXT directory in the Java Runtime Environment, JRE directory.

For example, the Default directory of the J2SE 1.3 release in the Windows platform C: /jdk1.3/jre/lib/ext. What are the core classes of the Java Mail API · Javax.mail.Session: Session class define a basic mail session (SESSION), which is the highest level of the Java Mail API. All other classes are effective via this session. The session object obtains information with java.util.properties objects, such as mail servers, usernames, passwords, and other information shared throughout the application. · Javax.mail.MAESSAGE: Once you get the session object, you can continue to create the message you want to send. This is done by the Message class. Because Message is an abstract class, you must use a subclass, in most cases, Javax.mail.Internet.mimeMeMessage. MimeMessage is an email message that can understand the MIME type and head, just as defined in different RFCs. Although the non-ASCII characters in some headers are also coded, the Message header can only be limited to a US-ASCII character. · Javax.mail.address: Once you have created session and message, you can use Address to determine the letters address with address after filling the content. Like Message, Address is also an abstract class. You are using the Javax.mail.Internet.InternetAddress class. · Javax.mail. AUTHENTICATOR: Like the Java.Net class, JavaMail API can also use Authenticator to access protected resources through the username and password. For JavaMail APIs, these resources are email servers. JavaMail Authenticator is in a javax.mail package, and it is different from the class authenticator in Java.net. Both do not share the same authenticator because JavaMail API is used for Java 1.1, which does not have a Java.net category. To use Authenticator, create a subclass of an abstract class and return to the PasswordAuthentication instance from the getPasswordAuthentication () method. Once you have finished, you must register an Authenticator to Session. Then, when you need to authenticate, you will notify Authenticator. You can pop up the window, or you can read the user name and password from the configuration file (although not encrypted is unsafe), return them to the call as the PasswordAuthentication object. · Javax.mail.Transport: The last part of the message is used to use the Transport class. This type of protocol specified language sends a message (usually SMTP). It is an abstract class, and its work is similar to the session. Only call the static send () method, you can use the default version of the class: Transport.send (Message); or readers can also get a specific instance from the session for your own protocol (if not If you need to pass it), send a message, then close the connection. · Javax.mail.Store: Store class implements read, write, monitor, lookup, etc. on a particular email protocol. You can access the Javax.mail.Folder class through the Javax.mail.Store class.

· Javax.mail.Folder: Folder class is used to hierarchical messages and provide the ability to access Email according to Javax.mail.Message format. How to send Email using JSP? Below we pass a simple example, how the email in JSP is sent. This example consists of two files. One is an HTML file to establish a form of mail information (including sender, recipient, theme, etc.) and send the form content to the JSP file; the other is the JSP page, responsible for the sending of the message. HTML file Send Mail </ title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> </ head> <body> <form action = "sendmail.jsp" method = "post" name = "form1"> <table align = "center"> <tr> <TD width = "50%> to: <br> <input name =" to "Size = "25"> </ td> <td width = "50%"> from: <br> <input name = "from" size = "25"> </ td> </ tr> <tr> <TD COLSPAN = "2"> Subject: <br> <input name = "SUBJECT" size = "50"> <td> </ tr> <tr> <td colspan = "2"> <p> message: <br> <Textarea name = "text" rows = 25 cols = 85> </ textarea> </ p> </ td> </ tr> </ table> <input type = "number =" cb_submit "value =" Send "> <Input type =" reset "name =" cb_reset "value =" clear "> </ form> </ body> </ html> JSP page's role is to get the data submitted by the form, and assign the data to The corresponding object in the Java Mail API, finally completing the email.</p> <p>Sendmail.jsp file <% @ page import = "javax.mail. *, javax.mail.internet. *, javax.activation. *, java.util. *"%> <html> <head> <title> JSP Meets JavaMail, what a sweet combo </ TITLE> </ head> <body> <% try {Properties props = new Properties ();. Session sendMailSession; Store store; Transport 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) ) {OUT.PRINTLN (M.TOString ());}%> </ body> </ html> How to send an HTML type message in the above example, we implemented how to send text formats, then HTML format How to send mail? Then look at the example below. This example consists of four files: • form.htm: Form.jsp: Send.jsp: Used to get information submitted, and call mymail.mail.html.send () method Send mail · StringDataSource. Java: User-defined JavaBean, is used to convert the body of the message to HTML format. HTML.java: user-defined JavaBean, used to send mail in HTML format. The MyMail.mail.html.send () method mentioned in the Send.jsp file is defined in the JavaBean.</p> <p>Form.htm <html> <head> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> <title> javamail - Send HTML Mail </ title> </ head> <body > <form method = "post" action = "send.jsp"> <table border = "0" cellspacing = "0" cellpadding = "0"> <tr> <td> SMTP host: </ td> <td> <Input Type = "text" name = "smtp" size = "80"> <td> <type: </ td> <td> <input type = "text" Name = "from" size = "80"> </ td> </ tr> <tr> <td> recipient: </ td> <TD> <input type = "name =" to "size =" 80 "> </ td> </ tr> <tr> <td> Cc: </ td> <TD> <input type =" text "name =" cc "size =" 80 "> </ td> < / TR> <Tr> <TD> Deliver: </ td> <TD> <input type = "text" name = "bcc" size = "80"> </ td> </ tr> <tr> < TD> Topic: </ TD> <TD> <input type = "text" name = "subject" size = "80"> <td> </ TD> <TD value = "TOP"> content: </ td> <TD> <textarea name = "body" rows = "5" cols = "80"> </ textarea> </ td> </ tr> <tr> <td colspan = "2" align = " Center "> <input type =</p> <p>"Submit" value = "send"> </ td> </ tr> </ table> </ form> </ body> </ html> send.jsp <% // variable declaration java.lang.string smtp, from , To, CC, BCC, Subject, Body; // obtain user input data SMTP = Request.GetParameter ("smtp"); from = request.getParameter ("from"); to = request.getParameter ("to"); CC = Request.getParameter ("cc"); BCC = Request.getParameter ("BCC"); Subject = Request.GetParameter ("Subject"); if (Subject! = null) {Subject = New java.lang.String Subject.getbytes ("ISO-8859-1"));} body = request.getParameter ("body"); // Send mail mymail.mail.html.send (SMTP, FROM, TO, CC, BCC, SUBJECT, body);%> mymail.jaf.StringDataSource.Javapackage mymail.jaf; public class StringDataSource implements javax.activation.DataSource {private Java.lang.String data; private Java.lang.String type; public StringDataSource (Java.lang.String data, Java.lang.String type) {this.data = data; this.type = type;} public java.io.InputStream getInputStream () throws java.io.IOException {return new java.io.StringBufferInputStream (data); } Public java.io.outputs tream getOutputStream () throws Java.io.IOException {throw new Java.io.IOException ( "it does not support this method now!");} public Java.lang.String getContentType () {return type;} public Java.lang . String getName () {return "mymail";}} mymail.mail.html.javaPackage mymail.mail;</p> <p>Public final class html {public static void send (java.lang.string smtp, / * smtp host address * / java.lang.string from, / * sender * / java.lang.String to, / * recipient * / Java.lang.String CC, / * Cc. * / Java.lang.String BCC, / * Dark delivery person * / java.lang.String Subject, / * Topic * / java.lang.string body / * content * /) Throws java.lang.exception {// Variable declaration java.util.properties version; // System Property Javax.mail.Session mailsession; // message session object javax.mail.internet.mimeMeX.mail.internet.mimeMeMEMSG; // mime mail object / / Set system properties PrOPS = java.lang.system.getProperties (); // Get system properties object Props.PUT ("mail.smtp.host", SMTP); // Set SMTP host // Get mail session object mailsession = Javax.mail.session.getDefaultInstance (PrOPS, NULL); // Create MIME E-mail object mimemsg = new javax.mail.internet.mimeMessage (mailsession); // Set the sender mimeMsg.SetFrom (New Javax.mail.Internet. InternetAddress (from)); // Sets the recipient if (to! = Null) {mimsg.seTrecipients (javax.mail.Message.RecipientType.to, javax.mail. Internet.internetDress.Parse (to));} // Set Cc. CC! = null) {mimemsg.seTrecipients (javax.mail.Message.RecipientType.cc, javax.mail. Internet.internetAddress.Parse (CC));} // Set Deliver IF (BCC! = null) {mimsg .setRecipients (Javax.mail.Message.RecipientType.BCC, javax.mail internet.InternetAddress.parse (bcc).);} set the message subject //mimeMsg.setSubject(subject); mimeMsg.setSubject (subject, "gb2312") ; // Set the mail content, convert the Body section into HTML format mimsg.setDataHandler (New Mymail.jaf. StringDataSource (Body, "Text / HTML"))); // Send Mail Javax. Mail.Transport.send (mimemsg);}} How to delete the message and the logo? The removal of the message involves the Flags (flag) related to the message. Different Flags represent different states, some flags are defined by the system, while others are defined by the user.</p> <p>The following list is listed below: • Flags.Flag.answered · flags.flag.deleted · flags.flag.draft · flags.flag.flagged · flags.flag.recent · flags.flag. Seen · Flags.Flag.user The above of the above flag is just a standard definition, does not mean that all mail servers or vendors support all of these logos. For example, in addition to deleting a message flag, the POP protocol no longer supports any other sign. Check if there is a new email, this is not a POP task, but the task built by the mail client. To find out which logo can be supported, you can use getPermanentFlags () to request a request to Folder. To delete a message, you can set the DLAG: Message.Flag.Deleted, true, please open Folder: Folder.Read_Write in Read_Write mode; then, when all messages are processed After completing, close the folder and pass a TRUE value, so that erase the message with the Delete flag. Folder.close (TRUE); a Folder's ExpUnge () method can be used to delete messages. But Sun's POP3 supplier does not support. Other suppliers may be able to achieve this, while others can't. IMAP suppliers are extremely likely to implement this feature. Because POP only supports access to mailboxes, you must close the folder to delete the message. To cancel the logo, just pass false to the setflag () method. Want to know if it is set, you can check it with isset (). How to achieve authentication? If you want to read, you can use an Authenticator to prompt users to enter usernames and passwords, rather than transfer usernames and passwords as a string. Here you will clearly understand how to fully use the Java Mail API certification mechanism. Do not connect to the Store without host, username, and password, but set Properties to have the host, then tell the sessions customization Authenticator instance, as shown below: // setup profroperties (); Props.Put ("Mail" .pop3.host ", host); // Setup authentication, get sessionAuthenticator auth = new PopupAuthenticator (); Session session = Session.getDefaultInstance (props, auth); // Get the storeStore store = session.getStore (" pop3 ") Store.Connect (); then, create an Authenticator subclass and return PasswordAuthentication objects from the getPasswordAuthentication () method. Below is the implementation, where the username and password take up only one domain. (This is not a swing engineering tutorial; as long as the two parts are input to the same domain, they will be separated by commas.</p> <p>) Import Javax.mail *;. Import Javax.swing *;. Import Java.util *;. Public class PopupAuthenticator extends Authenticator {public PasswordAuthentication getPasswordAuthentication () {String username, password; String result = JOptionPane.showInputDialog ( "Enter 'username , password ' "); StringTokenizer st = new StringTokenizer (result,", "); username = st.nextToken (); password = st.nextToken (); return new PasswordAuthentication (username, password);}} because involves PopupAuthenticator Swing, it will start the AWT event handling thread. This basically requires you to add a call to System.exit () to terminate the program. How to implement the forwarding of messages? Forwarding the message, it seems to be a bit more than other functions. There is no separate method to be used, and the reader must organize the message to be forwarded by processing the part of each part of the message. An email message can be composed of multiple parts. When processing the MIME message, each part is Bodypart, and it is special, which is MIMEBODYPART. Different Body Parts are combined into a container, named Multipart, and special, is MIMEMULTIPART. To forward a message, you create a component for your message body, and the message to be forwarded as another component. Further, two components are bonded into a Multipart. Then you add this Multipart to a message written properly and send it. This is essentially this. To copy a message content to another, just copy the DataHandler (the classes in JavaBeans Activation Framework).</p> <p>// Create the message to forwardMessage forward = new MimeMessage (session); // Fill in headerforward.setSubject ( "Fwd:" message.getSubject ()); forward.setFrom (new InternetAddress (from)); forward.addRecipient ( Message.RecipientType.TO, new InternetAddress (to)); // Create your new message partBodyPart messageBodyPart = new MimeBodyPart (); messageBodyPart.setText ( "Here you go with the original message: / n / n"); // Create a multipart to combine the partsMultipart multipart = new MimeMultipart (); multipart.addBodyPart (messageBodyPart); // Create and fill part for the forwarded contentmessageBodyPart = new MimeBodyPart (); messageBodyPart.setDataHandler (message.getDataHandler ()); / / Add part to multi partmultipart.addbodypart (messagebodypart); // associate multi-part with messageforward.setcontent (multipart); // send message; how to process attachments? Attachments are resources related to mail messages, as usually not included in the text file, spreadsheet, or images. Common mail programs, such as EUDORA and PINE, you can use the JavaMail API to get resource ATTACH to your message, you can get the message. The delivery of attachments: Send attachments very like forwarding messages. You build your part to make up your full message. After completing the first part, you add other parts, where you add other parts, all of which represent an attachment, not the shared handler in the event of the message. If you read attachments from the file, the data source of the attachment is FileDataSource. And if you read from the URL, the data source of the attachment is URLDataSource. Once there is DataSource, just pass it to the DataHandler constructor first, and finally use setDataHandler () to Bodypart. Assume that you want to keep the original file name of the attachment, and ultimately to do it is the file name related to the attachment with the attachment's setFileName () method.</p> <p>As follows: // Define message Message message = new MimeMessage (session); message.setFrom (new InternetAddress (from)); message.addRecipient (Message.RecipientType.TO, new InternetAddress (to)); message.setSubject ( " hello JavaMail Attachment "); // Create the message part BodyPart messageBodyPart = new MimeBodyPart (); // Fill the message messageBodyPart.setText (" Pardon Ideas "); multipart multipart = new MimeMultipart (); multipart.addBodyPart (messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart (); DataSource source = new FileDataSource (filename); messageBodyPart.setDataHandler (new DataHandler (source)); messageBodyPart.setFileName (filename); multipart.addBodyPart (messageBodyPart); // Put Parts in message message.setContent (MULTIPAR); // send the message transport.send (Message); When the message is introduced into the attachment, if the program is a servlet (small service program), in addition to the information sent, it is necessary to upload. annex. Multipart / Form-Data Form Coding Types (FORM Encoding Type) can be used for processing of each upload file. <Form encType = "multipart / form-data" method = post action = "/ myservlet"> <input type = "file" name = "thefile"> <input type = "submit" value = "Upload"> </ form > Note: The message size is limited by the SMTP server instead of the JavaMail API. If you encounter a problem, you can consider increasing the Java heap size using the method of setting MS and MX parameters. Accommodation: Acquisition of attachments from the message, more than sending them, because MIME does not have a simple concept about attachments. When the message contains an attachment, the content of the message is a Multipart object. Next, you need to handle each Part to get the main contents and accessories. Parts (Part) marked with Part.attachment configuration from Part.getDisPosition () is undoubtedly an attachment. However, there is no configuration (and a non-text mime type) and components with part.inline configuration may also be an attachment. When the configuration is part.attachment, the content of this message component can be saved when Part 1.inline.</p> <p>Just use getFileName () and getInputStream () to get the original file name and input stream. Multipart MP = (Multipart) Message.getContent (); for (int i = 0, n = multipart.getcount (); i <n; i ) {part part = multipart.getBodypart (i)); string disposition = Part. GetDisPosition (); if (Disposition! = null) && ((disposition.equals (part.attachment) || (part.inline))) {savefile (Part.getFileName (), Part.getInputStream () );}} Savefile () method only creates a file based on the file name, which reads the byte from the input stream, and then writes into the file. Invented that the file already exists, add a number after the file name New file name, if this file name still exists, continue to add until you find this file name. // from savefile () file file = new file (filename); for (int i = 0; file.exists (); i ) {file = new file (filename i);} The above code covers the simplest situation - the appropriate markings in the message. To cover all the situation, you must handle it in the configuration And the MIME type of the component is acquired to perform the corresponding processing. If (Disposition == Null) {// check if plain mimebodypart MBP = (MIMEBODYPART) Part; if (Mbp.ismType ("text / plain") {// handle Plain} else {// special non-attachment case, ...} ...} How to send multiple mails at a time? Advance the necessary Transport objects and call sendMessage () to send each email Note setting or change the recipient between calls. Message message = ...; transport t = session.gettra nsport ( "smtp"); t.connect (); message.setRecipient (Message.RecipientType.TO, recipient1); t.sendMessage (message, recipient); message.setRecipient (Message.RecipientType.TO, recipient2); t. SendMessage (Message, Recipient); Message.SetRecipient (message.recipienttype.to, recipient3); t.sendMessage (message, recipient); t.close (); how to save mail? The Writto () method of the MIMEMESSAGE class can be implemented. Objects with a Message class cannot be implemented.</p> <p>FileOutputStream Fos = New FileOutputStream ("Test.mail"); MimeMessage.Writeto (FOS); How to send an image-free HTML format mail? Some images are still on the server, so that the user's mail tool is handled. The reader can send an image as an accessory or HTML body. If all attachments are saved in the same directory, you must use a different image file name to ensure that the mail tool does not display additional pictures. In addition, the image URL should be used with an absolute path, and the relative path cannot be used. How to set up / get the priority of the email? Set the priority of the message, just add "X-Priority" attributes in the message header: MimeMessage Msg; Msg.Addheader ("X-Priority", "1"); the same truth, you have to get the priority of the message Just get " The value of the X-priority ": string priority = msg.getHeader (" x-priority "); Appendix: Java Mail API Basic Concept What is Java Mail API JavaMail API is an easy to read, write, and send electronic Email optional package (standard extension). Similar to Eudora, Pine, and Microsoft Outlook, this package is used to create a Mail User Agent (MUA) type program. The main use of the API is not transmitted, sent, and forwards messages; this function ranges to some applications, such as Sendmail and other mail transport agents (MTA) types. MUA type program allows users to read and write messages, but it relies on MTA processing actual sends. What is SMTP SMTP (Simple Mail Transfer Protocol), the simple mail transfer protocol, which defines the mechanism for sending an email. In a JavaMail API environment, JavaMail-based programs will communicate with your company or Internet service provider (ISP's) SMTP server. The SMTP server can turn the message to the recipient SMTP server to eventually get the user to be obtained via POP or IMAP. This is not required to be an open relay, although the SMTP server supports authentication, but also ensures its configuration correctly. The implementation of the JavaMail API is not supported like a configuration server to relay messages or add a delete mail account. What is POP POP (Post Office Protocol), the post office protocol. It is currently available to version 3, so people usually call it POP3, RFC 1939 defines this protocol. POP and SMTP are also a mechanism, most people get emails on the Internet. The agreement specifies that each user can only have a mailbox support. This is what it can do, and this has also caused a lot of confusion. When using POP, many of the user familiar with the user is not supported by POP protocol, such as seeing several new mail messages. These properties are built in programs such as Eudora or Microsoft Outlook, they can remember something, such as the last received email, how much is new.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-51954.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="51954" 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.038</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 = 'R3DGyvrDaPnp9mPVzN8mpWA9QF9a1RSLnxmWVCBD_2FnHKmNI9I8IKyRnxUrgUDctjIO4tw3qaoQviKD29n5AstA_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>