I often use mail sending classes

xiaoxiao2021-03-06  23

Package javax.util.zz; import javax.mail. *; import javax.mail.internet. *; import javax.activation. *; import java.util. *; import java.net. *; import java.io. * ; / ** *

Mail Send class *

supports normal mode and HTML mode, you can send multiple attachments, support SMTP server authentication.
Based on JavaMail development, please include JavaMail in the ClassPath system variable when used.

*


instructions: *

mail mail = new mail (); *

mail.setxxx .... *

mail.send ();
* @Author * @version 1.0 * / public class mail {private address [] to = null; private address [] cc = null; private address [] BCC = null; private String from = ""; private String title = ""; private String content = ""; private String smtpHost = ""; private int smtpPort = 25; private String content_type = MODE_TEXT; private String htmlMailDesc = ""; private String smtpUser = ""; private String smtpPassword = ""; private boolean isAuthenticationSMTP = false; private Vector vFiles = new Vector (); private Vector vURLs = new Vector (); public Mail () {} / ** * set SMTP Server, use the default port * @Param Server SMTP server ip * / public void setsmtphost (string server) {this.smtphost = server;} / ** * Settings SMTP server * @Param Server SMTP server ip * @Param port port * / Public void setsmtphost (string server, int port) {this.smtphost = server; this.smtpport = port;} / ** * Set the recipient address * @Param aemail recipient email address * / public void setto (string aemail) {string [] s = new string [1]; s [0] = aemail; this.to = getaddress (s } / ** * Set multiple recipient addresses * @PARAM Emails Recipient Email Address * / public void setto (String [] emails) {this.to = getaddress (emails);} / ** * settings Cc address * @Param AEMAIL Cc address * / public void setc (string aemail) {string [] s = new string [1]; s [0] = AEMAIL; this.cc = getAddress (s);} / * * * Set multiple credit addresses * @

PARAM Emails Cc address * / public void setcc (String [] emails) {this.cc = getaddress (emails);} / ** * Setting DC address * @Param Emails Dark address * / public void setbcc (String aemail ) {String [] s = new string [1]; s [0] = aemail; this.bcc = getaddress (s);} / ** * Set multiple dark delivery addresses * @Param emails Dark delivery address * / public Void setbcc (String [] emails) {this.bcc = getaddress (emails);} / ** * Settings sender address * @Param aemail sender address * / public void setFrom (string aemail) {// if ( IsValideMailaddress (AEMAIL) {// throw new myexception ("invalid email address"); //} this.from = aemail;} / ** * Setting mail topic * @Param mailtitle Mail Topic * / public void setsubject (String Mailtitle) {this.title = mailtitle;} / ** * Set mail text content * @Param mailcontent mail text content * / public void setBody (string mailcontent) {this.content = mailcontent;} / ** * Setting the email character type * @Param ContentType Please select * / public void setContentType (String ContentType) from Static Variable Text and HTML. = ContentType;} / ** * Settings HTML format messages displayed in general mode * @Param Desc Description text * / public void sethtmlmailsc (string desc) {this.htmlmailsc = desc;} / ** * Settings SMTP server users * @param username authentication user name * @param password password * / public void setSmtpAuthentication (String username, String password) {this.smtpUser = username; this.smtpPassword = password; this.isAuthenticationSMTP = true;} / ** * Add attachment * @Param AFILE Local file * / public void addattachment (file Afile) {vFiles.add (Afile);} / ** * Add attachment * @Param fileurl file URL * / public void addattachment (url fileurl) {vurls.add ( FileURL);

} / ** * Logging message with attachment * @return * / public boolean Hasattachment () {Return VFiles.Size () VURLS.SIZE ()> 0;} / ** * Send Mail * / Public void send () {Try {Properties Server = New Properties (); IF (String New NullpointRexception); "please set smtp host";} else {server.put ("mail.smtp.host", THIS.SMTPHOST;} Server.put ("mail.smtp.port", string.valueof (this.smtpport); if (this.isauthenticationsmtp) {server.put ("mail.smtp.auth", "true" );} Session conn = Session.getInstance (server, null); MimeMessage msg = new MimeMessage (conn); if (StringUtil.isEmpty (this.from)) {throw new NullPointerException ( "Please set FROM address");} else {Msg.setFrom (new internetdress (this.from));} if (this.to! = Null) {msg.seTrecipients (Message.RecipientType.to, this.to);} // else {// throw new nullpointerexception ("PLE ASE set to address "); //} if (this.cc! = null) {msg.seTrecipients (Message.RecipientType.cc, this.cc);} if (this.bcc! = null) {msg.setripients Message.recipientType.bcc, this.bcc);} Sun.misc.base64encoder enc = new sun.misc.base64encoder (); msg.setsubject ("=? GB2312? B?" Enc.Encode (this.title.getbytes ()) "? ="); If (! HaSattachment ()) {// If there is no accessory IF (this.ishtmlmodemail ()) {// is an HTML format mail if (! This.hashtmldesc ()) { Msg.setContent (this.content, this.content_type);

} Else {Multipart mp = new MimeMultipart (); MimeBodyPart mbp = null; mbp = new MimeBodyPart (); mbp.setContent (this.content, this.content_type); mp.addBodyPart (mbp); mbp = new MimeBodyPart (); Mbp.SetContent (this.htmlmailsc, this.mode_text); mp.addbodypart (MBP); msg.setContent (mp);}}} else {// is a text format Msg.SetText (this.content);}} else {// has an attachment multipart mp = new mimeMultipart (); mimebodypart mbp = null; // message body for (int i = 0; i

} Msg.saveChanges (); if (this.isAuthenticationSMTP) {Transport transport = conn.getTransport ( "smtp"); transport.connect (this.smtpHost, this.smtpUser, this.smtpPassword); transport.sendMessage (msg, msg .geletRecipients ()); transport.close ();} else {transport.send (msg, msg.getallRecipients ());}} catch (javax.mail.internet.addressException E) {E.PrintStackTrace ();} catch (javax.mail.MessagingException e) {e.printStackTrace ();}} public boolean isValidEmailAddress (String email) {if (StringUtil.isEmpty (email)) return false; if (email.indexOf ( "@")> 0) Return! Email.endSwith ("@"); Return False;} private address [] getaddress (string [] add) {address [] a = new address [add.length]; for (int i = 0; i

http://blog.9cbs.net/allenzz/

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

New Post(0)