Java implementation of the e-mail tool class, easy to use (need to rack the package of send Email to me)

Source: Internet
Author: User

Original: Java implementation of the e-mail tool class, easy to use (need to rack the package of send Email to me)

Source code: Http://www.zuidaima.com/share/1550463394794496.htm

Package Com.zuidaima.util;import Java.util.properties;import Javax.mail.bodypart;import javax.mail.Message;import Javax.mail.multipart;import Javax.mail.session;import Javax.mail.transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.mimebodypart;import Javax.mail.internet.mimemessage;import Javax.mail.internet.mimemultipart;public class EmailHelper {private String Host = "smtp.163.com"; The SMTP server has many types of hostprivate static String user = "Zuidaima"; User name private String pwd = "XXXXXX"; Password private String from = "[email protected]"; Sender Address private String to = ""; Recipient address private String subject = "www.zuidaima.com"; Message title public void Setaddress (string from, string to, string subject) {This.from = From;this.to = To;this.subject = Subjec t;} public void Send (String txt) {Properties props = new properties ();//Set the properties of the mail server to send mail (this is the SMTP server using NetEase here) props.put (" Mail.smtp.host ", host);//need to be authorized, that is, the user name and password verification, so as to pass the verification (must have this) props.put (" Mail.smtp.auth "," true ");//With the newly set PRops object constructs a sessionsession session = session.getdefaultinstance (props);//With this sentence you can display the process information at the console while the message is being sent for debugging// (You can see the process of sending the message on the console) Session.setdebug (TRUE);//define the Message object with the session parameter MimeMessage msg = new MimeMessage ( session); try {//Load Sender address Message.setfrom (new internetaddress (from));//Load recipient address message.addrecipient ( Message.RecipientType.TO, New InternetAddress (To)),//Load Header message.setsubject (subject);//Add parts of the message to the multipart object, Includes text content and attachments multipart multipart = new Mimemultipart ();//Set the text content of the message bodypart Contentpart = new MimeBodyPart (); Contentpart.settext (TXT); Multipart.addbodypart (Contentpart);//Add attachment//BodyPart Messagebodypart = new MimeBodyPart () ;//DataSource Source = new Filedatasource (affix);//Add the contents of the attachment//Messagebodypart.setdatahandler (new DataHandler (source)) ///Add the title of the attachment//here is important, through the following BASE64 encoded conversion can guarantee that your Chinese attachment header name will not be garbled when sent//Sun.misc.BASE64Encoder enc = new Sun.misc.BASE64Encoder ();//Messagebodypart.setfilename ("=? GBK? B? " +//Enc.encode (Affixname.getbytes ()) + "? =");//Multipart.addbodyparT (messagebodypart);//Put multipart object into message message.setcontent (multipart);//Save Mail Message.savechanges ();// Send mail Transport transport = Session.gettransport ("SMTP");//Mailbox Transport.connect (host, user, PWD) connected to the server;// Send the message out transport.sendmessage (message, message.getallrecipients ()); Transport.close ();} catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {Emailhelper cn = new Emailhelper ();//Set Sender address, recipient address, and message header cn.setaddress (user, "[EMA Il protected] "," source Code "); Cn.send (" Did you get the mail I sent for the wood? " ");}}

Java implementation of the e-mail tool class, easy to use (need to rack the package of send Email to me)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.