Send an email with JavaMail

xiaoxiao2021-03-06  40

Source code download: http://www.freewebs.com/camel20/

/ * * File: mail.java * Date: 2005-2-28 * author: Fuweilin * project: * Ver: * Note: * * / package com.toone.eg.common;

/ ** * @Author Fuwln * @version 1.0 * /

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

/ ** * @Author Fuwl * * to change the template for this generated type comment go to * window - preferences - java - code generation - code and comfic class mail {

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 mail () {setsmtphost ("smtp.163.com"); // If you do not specify a mail server, get createmimeMessage () from the GetConfig class.

Public Mail (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 mail object MP = new mimeMultipart (); return true;} catch (Exception E) {system .err.println ("Create a 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 ("Errors when you set the message body!" 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 DataHandler (fileds)); bp.setFileName (fileds.getName ()); 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 () {mimemsg.setContent (mp); mimemsg.savechanges (); system.out.println ("is 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.rr.Println ("Mail Send Failed!" E); Return False;} } / ** * @Param destmail destination mail address * @Param Content body * / public void send (string destmail, string content) {string mailbody = " " "

content ""; if (setsubject ("Topic - Test") == false) Return; IF (setBody (mailbody) == false) Return ;

// Set the recipient address IF ("Test@test.com") == false) Return; if (setFrom (destmail == false) Return; // if (addFileAffix ("D: // Test .txt ") == false) Return; setNamePass (" UserName "," password "); if (sendout () == false Return;}

/ ** * Just do it as this * / public static void main (String [] args) {

Mail mail = new mail (); mail.send ("fuweilin@hotmail.com", "test, test
ceshi");}}

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

New Post(0)