/ * 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 copy J2EE.jar directly, add it to the 2nd of JBuilder, 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.m.util . *;
Public class sendmail {
Private mimeMessage mimemsg; // mime mail object
Private session session; // message session object private property props; // System Property Private Boolean NeedAuth = FALSE; / / SMTP Need to be authenticated
Private string username = ""; // SMTP authentication username and password prince password = ""
Private Multipart MP; // Multipart object, email content, title, attachment, etc. 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 ("Setting System Properties: mail.smtp.host =" HostName); if (Props == Null) Props = SYSTEM .getproperties (); // get system properties object
Props.PUT ("mail.smtp.host", hostname); // Set SMTP host}
/ ** * @Return Boolean * / public boolean createmimeMeMessage () {try {system.out.println ("Prepare for mail session object!"); Session = session.getDefaultInstance (Props, NULL); // Get mail session object } Catch (exception e) {system.err.println ("Errors when getting a mail session object!" E); Return False;} system.out.println ("Prepare to create MIME mail objects!"); Try {mimsg = New MimeMessage (session); // Create MIME E-mail Object MP = New MimeMultipart ();
Return true;} catch (exception e) {system.err.println ("Create MIME mail object failed!" E); Return False;}}
/ ** * @Param Need Boolean * / public void setNeedAuth (Boolean NEED) {system.out.println ("Sets SMTP authentication: mail.smtp.auth =" NEED); if (Props == Null) = 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 subject!"); Try {mimeMsg.setSubject (mailSubject); return true;} catch (Exception E) {system.err.println ("Setting an error in the message topic!"); 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 (" Influence when setting the message! " e); return false;}}
/ ** * @Param name string * @Param pass string * / public boolean addfileAffix (string filename) {
System.out.println ("Add Mail Accessories:" FileName);
Try {bodypart bp = new mimebodypart (); fileDataSource fileds = new fileDataSource (filename); bp.setDataHandler (NEW DATAHANDAL (Fileds)); bp.setFileName
mp.addbodypart (bp);
Return true;} catch (exception e) {system.err.println ("Add Mail Attachment:" FileName "Errors!" E); Return False;}}
/ ** * @Param name string * @Param pass string * / public boolean setfrom (string from) {system.out.println ("Setting sender!"); Try {mimemsg.setFrom (New InternetdRESS (FROM)); // Set the 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 {mimsg.seTrecipients (message.recipienttype.to, internetdress.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 []) Internet ADRESS.PARSE (COPYTO)); Return True;} Catch (Exception E) {Return False;}} / ** * @Param Name String * @Param pass string * / public boolean sendout () {Try {mimemsg.setContent MP); 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.GETRECIPIPIENTS (Message.RecipientType.to); //Transport.send (MIMEMSG);
System.out.println ("Send Mail!"); 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 = "" "