/ * On the Java version, someone often asks how to send mail with JavaMail? How to receive mail? How to access multiple folders and so on. The problem is scattered, and the history of history has already submerged in the ocean of the problem. I have done a Java project before, which contains a webmail feature. At the beginning of the Java implementation, I have a bit gain for JavaMail. Seeing the regular problems in the forum, therefore put some of my experience, I hope to have some help. This article only introduces the use of JavaMail to send mail features, which involve SMTP authentication, mail attachments, and HTML content mail. Other content related to multi-mail box, receive POP3 mail and IMAP, will be introduced in subsequent articles.
The following procedures are required: Javamail, Jaf package, J2EE.jar contains the above two packages, it is recommended that you install J2SDKee or directly copy J2EE.ja, add it to JBuilder's library, or system ClassPath * / package com.me. Util.mail; / *** @author Zhangkun Aistill@msn.com* @version 1.0 * / import java.util. *; import javax.mail. *; import javax.mail.internet. *; import java.util. Date; import javax.activation *;. import java.io *;. import com.me.util *;. public class sendMail {private MimeMessage mimeMsg; // MIME message object private session session; // message session object private Properties props ; // System Attribute Private Boolean NeedAuth = false; // SMTP Does Requirement Private String UserName = ""; // SMTP Authentication User Name and Password Private String Password = ""; Private Multipart MP; // Multipart Object, Mail Content, Title, attachment and other content are added to the MIMEMESSAGE object / *** * / public sendmail () {setsmtphost (getconfig.mailhost); // If you do not specify a mail server, get createmimeMessage () from the GetConfig class. } public sendMail (String smtp) {setSmtpHost (smtp); createMimeMessage ();} / *** @param hostName String * / public void setSmtpHost (String hostName) {System.out.println ( "set system properties: mail.smtp .host = " hostname); if (props == NULL) PROPS = system.getproperties S (); // Get the system properties object Props.PUT ("mail.smtp.host", hostname); // Set SMTP host} / *** @return boolean * / public boolean createmimeMessage () {Try {system. Out.println ("ready to get the mail session object! "); session = session.getDefaultInstance (PrOPS, NULL); // Get mail session object} catches (Exception E) {system.err.println (" Errors when getting a mail session object! " E); Return False; } System.out.println ("Prepare to create MIME Mail Object!"); Try {mimemsg = new mimeMessage (session); // Create MIME mail object MP = new mimeMultipart (); return true;} catch (Exception E) { System.err.Println ("Create MIME mail object fails!" E); return false;}} / *** @
Param Need Boolean * / Public Void SetNeedAuth (Boolean NEED) {System.out.println ("Sets SMTP Identity Certification: mail.smtp.Auth =" NEED); if (Props == Null) PROPS = System.getProperties () ; if (NEED) {Props.Put ("mail.smtp.auth", "true");} else {prOPs.put ("mail.smtp.auth", "false");}} / *** @ param name String * @param pass String * / public void setNamePass (String name, String pass) {username = name; password = pass;} / *** @param mailSubject String * @return boolean * / public boolean setSubject (String mailSubject ) {System.out.println ("Set Mail Topics!"); Try {mimemsg.setSubject (mailsubject); return true;} catch (exception e) {system.rr.println ("Setting the mail theme error!")) ; return false;}} / *** @param mailBody String * / public boolean setBody (String mailBody) {try {BodyPart bp = new MimeBodyPart (); bp.setContent ( " " Mailbody," Text / HTML; Charset = GB2312 "); mp.addbodypart (bp); return true;} catch (exception e) {system.err.println (" set mail body An error occurred! " E); Return False;}} / *** @Param Name String * @Param pass string * / publ ic boolean addFileAffix (String filename) {System.out.println ( "email attachments increase:" filename); try {BodyPart bp = new MimeBodyPart (); FileDataSource fileds = new FileDataSource (filename); bp.setDataHandler (new DataHandler ( Fileds)); bp.setFileName (fileds.getname ()); mp.addbodypart (bp); return true;} catch (exception e) {system.rr.println ("Add Mail Attachment:" FileName "error! " e); return false;}} / *** @Param name string * @Param pass string * / public boolean setFrom (STRING" {system.out.println ("
Set the sender! "); try {mimemsg.setfrom (new internetdress (from)); // Sets sender return true;} catch (Exception E) {return false;}} / *** @Param name string * @Param pass string * / public boolean setTo (String to) {if (to == null) return false; try {mimeMsg.setRecipients (Message.RecipientType.TO, InternetAddress.parse (to)); return true;} catch (Exception e) {return false;}} / *** @param name String * @param pass String * / public boolean setCopyTo (String copyto) {if (copyto == null) return false; try {mimeMsg.setRecipients (Message.RecipientType.CC, ( Address []) internetdress.parse (copyto); Return True;} catCH (Exception E) {Return False;}} / *** @Param Name string * @Param pass string * / public boolean sendout () {Try { MIMEMSG.SETCONTENT (MIMEMSG.SAVECHANGES (); system.out.println ("I am sending mail ...."); session mailsession = session.getInstance (props, null); transport transport = mailsession.getTransport (" SMTP "); Transport.connect ((String) Props.get (" mail.smtp.host "), username, password; transport.sendMessage (mimemsg, mimemsg.getrecipients (Message.Recipientt) Ype.to)); // transport.send (mimemsg); system.out.println ("Send mail success! "); transport.close (); return true;} catch (exception e) {system.err.println (" mail delivery failed! " E); return false;}} / *** Just do it as this * / public static void main (string [] args) {string mailbody = "" "