Java + Tomcat website deployment on the third day - mail delivery

zhaozj2021-02-16  105

1. Need mail.jar and activation.jar to find in J2EE's lib

C: / sun / appserver / lib

2. To use in Tomcat, you need to put these two JARs in Tomcat / Common / Lib and add to ClassPath

Import javax.mail. *; import javax.activation. *; import javax.mail.internet. *;

/ * - Set attribute - * / proties = system.getProperties (); Properties.Put ("mail.smtp.auth", stramphen); Properties.Put ("mail.smtp.host", strHost;

/ * - Password Verification - * / SMTPAUTHENTICATOR SA = New SMTPAUTHENTICATOR (STRUSR, STRPWD); session session = session.getInstance (Properties, SA); session.setdebug (false);

/ * - Constructive message - * / message msg = new mimeMeMessage (session); msg.setSentDate (new Date ()); msg.setdress (strfrom); msg.seTrecipient (message.recipientType.to, New Internet ADRESS (STRTO)); msg.setSubject (strsubject); msg.setContent (strbody, "text / place; charSet = GB2312");

/ * - Transfer email - * / transport = session.getTransport ("SMTP"); if (Strauthen.equals ("True") // Need to verify {Transport.connect (Strhost, Strusr, StrPwd); TRANSPORT .sendMessage (MSG, Msg.GetallRecipients ());} else {transport.send (msg);} Transport.close (); 3.Jsp

Sendmail Sender = New Sendmail ("Ohahu@sohu.com", // - Sender "Ohahu@163.com", // - Received "This Liu Guoqiang Title", // - Title "This Liu Guoqiang Body" , // - Content NULL); // - Annex Sender.putAuthentication ("smtp.sohu.com", // - SMTP server "ohahu", // - username ", // - password Don't forget to lose "True"); // Do you need to verify sender.startsend (); 4. If you need to improve, you have not added an attachment code, you can refer to

http://community.9cbs.net/expert/faq/faq_index.asp?id=12942

The Strattach should be the path information of the attachment.

Next task:

1. Improve accessories Add some of the content 2. Verify the user registration application and password modification function via email

2. File upload code

3. Database record is displayed in XML file, and the database generated a directory tree

Add: Address Address:

In order to add a fourth day for an attachment, it is not worth it. The main code is as follows:

/ * - Add attachment - * / Multipart multiPart = new MimeMultipart (); // contents of the letter MimeBodyPart textBody = new MimeBodyPart (); textBody.setText (strBody); multiPart.addBodyPart (textBody); // Enumeration elemFile mail attachments = vfile.elements (); // vector vfile; use vfile.addelement (String newfile); add attachment attachcount = 0; while (elemFile.hasMoreElements ()) {string strfilename = elemfile.nextelement (). Tostring (); / / accessories path file file = new file (strFileName); if // determine whether a file exists continue (file.exists ()!); iAttachCount ; MimeBodyPart fileBody = new MimeBodyPart (); FileDataSource fileData = new FileDataSource (strFileName); fileBody .SetDataHandler (NEW DataHandler (FileData)); FileBody.SetFileName (FileData.GetName ()); multipart.addbody (filebody);} vfile.removeLlelements ();

Msg.setContent (Multipart);

With this part, you don't have to use the above msg.setcontent (STRBODY, "Text / Plain; Charset = GB2312"); Therefore, there is no accessory to construct the mail with a setContent function.

转载请注明原文地址:https://www.9cbs.com/read-11362.html

New Post(0)