First introduced J2EE's packet path to ClassPath, which is already installed in its installation path lib directory to find mail.jar, activation.jar, join the two packages to the system's environment variable, then do not need JBuilder to develop, you can also use JBuilder Used. Program: Import java.io. *; Import java.util. *; Import javax.mail. *; Import javax.mail.internet. *;
/ ** *
Title: Simple Mailbean P> *
Description: Provides easy Mail sending function (/ P> *
Copyright: CopyRight (c) 2003 p> * @version 1.0 * /
Class Extendstring {public extendstring () {} / ** Remove the blank character between the strings and convert the string into China's standard character GB2312 string. * / public string CS (String str) {// remove the string 2-side blank character try {if (str == null) return ""; str = str.trim (); if (str == null) return "; str = new string (str.getbytes (" 8859_1 "), "GBK");} catch (Exception E) {system.out.println (e);} Return Str;}
}
Public class sendmail {private string errmsg = ""; private extendstring exStr = new extendstring ();
Private string sender = ""; // sender address private STRING SMTPHOST = ""; // mail send server (SMTP) private string user = ""; // login username prince string password = "" "; // login password
Private string subject = ""; // mail theme
Public sendmail () {this.setpropertiesattri ();
Private void setpropertiesattri () {Try {INPUTSTREAM IS = getClass (). getResourceAsStream ("mailserver.properties"); Properties Prop = New Properties (); Prop.Load (IS);
THIS.SETSMTPHOST (Prop.Get ("SMTPHOST"). TOSTRING ()); this.Setuser ("User"). TOSTRING ()); this.SetPassword (Prop.get ("password"). Tostring ()); This.Set Shender (Prop.Get ("sender"). ToString ()); this.setSubject (exStr.cs ("subject"). Tostring ()));} catch (Exception EX) ) {System.err.Println ("EX1 in sendmail.java:" EX.TOSTRING ());}} / ** Set the sender address * / public void setsender (string sender) {this.sender = sender; }
Public string get Shesender () {return sero
/ ** Set Mail Send Server (SMTP) * / Public Void Setsmtphost (STRING SMTPHOST) {this.smtphost = SMTPHOST;} public string getsmtphost () {Return SMTPHOST;}
/ ** Set the login username * / public void setuse {this.user = user;} public string getuser () {Return User;
/ ** Set the login password * / public void setpassword (String password) {this.password = password;} public string getpassword () {return password;}
/ ** Set MAIL Topic * / Public Void Setsubject (String Subject) {this.subject = Subject;} PUBLIC STRING GETSUBJECT ()}
/ ** * E-mail using smtp main routine * @throws MessagingException mail transmission fails * / public void smtp (String receiver, String content) throws MessagingException {if (smtpHost == null) throw new MessagingException ( "smtpHost not found"); IF (user == null) Throw new MessagingException ("User Not Found"); if (Password == Null) Throw New MessagingException ("Password Not Found");
Properties Properties = New Properties (); Properties.put ("mail.smtp.host", smtphost; // Set SMTP host Properties.put ("mail.smtp.auth", "true"); // Use SMTP identity Verify session session = session.getDefaultInstance () {public passwordAutAuthentication getPasswordAuthentication () {Return New PasswordAuthentication (user, password);}});
// Get the mail session object mimeMeMessage mimessg = new mimeMessage (session); // Create a MIME mail object if (sender! = Null) // Set the sender address {mimsg.setFrom (new internetdress (sender));} Receiver! = null) // Sets the recipient address {mimemsg.seTrecipients (message.RecipientType.to, Parse (Receiver);}}} (Subject! = NULL) // Set the email topic {mimemsg.setSubject (Subject, " GBK ");} mimebodypart part = new mimebodypart (); // mail content section part.setText (content == null?": Content, "gbk");
// Set the mail format to HTML CQC Part.setContent (Content.Tostring (), "Text / HTML; Charset = GBK"); Multipart Multipart = New MimeMultipart (); Multipart.AddBodypart (Part); // Added in Multipart MIMEMSG.SETCONTENT (MULTIPART); // Add Multipart to Information MIMEMSG.SetSentDate (New Date ()); // Settings Send Date Transport.send (mimeMsg); // Send Mail}
/ ** set to resolve the address string * / private InternetAddress [] parse (String addressSet) throws AddressException {ArrayList list = new ArrayList (); StringTokenizer tokens = new StringTokenizer (addressSet, ";"); while (tokens.hasMoreTokens () ) {list.add (new InternetAddress (tokens.nextToken () trim ().));} InternetAddress [] addressArray = new InternetAddress [list.size ()]; list.toArray (addressArray); return addressArray;} / * * * External call * / public boolean sendmails (string mail, string content) {int maillen = 0; int contentlen = 0; if (mail == null || content == null) {Return False;}
Try {this.smtp (Mail, Content);} catch (exception ex) {system.err.println ("ex2 in sendmail.java:" ex.toT7tring ());}
Return True;}
Public static void main (String [] args) {sendmail mail = new sendmail (); string email = "9cbs@9cbs.net; tom@163.com"; string content = "account: 123 password: 123 Thank you for registration! www.xxxx.com.cn
xxxx [} (EX33: EX.TOSTRING ()) ("EX33: EX.TOSTRING ());}}
}
After compiling, create a text filemilserver.properties in the directory of the class file: SMTPHOST =SMTP.163.comuser=Userpassword=pwdsender=9cbs@9cbs.comSubject=hello running program to implement mail delivery!