Java Send mail

Source: Internet
Author: User

//Download Mail.jar from Oracle First
Packagetest;ImportJavax.mail.BodyPart;ImportJavax.mail.Message;ImportJavax.mail.Multipart;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.*;ImportJava.util.*;Importjavax.activation.*; Public classMailutils {PrivateString host = "";//SMTP Server PrivateString from = "";//Sender Address PrivateString to = "";//Recipient Address PrivateString affix = "";//Attachment Address PrivateString affixname = "";//Attachment name PrivateString user = "";//User name PrivateString pwd = "";//Password PrivateString subject = "";//message Header Public voidsetaddress (string from, string to, string subject) { This. from =From ; This. to =to ; This. Subject =subject; } Public voidSetaffix (String affix, string affixname) { This. Affix =affix; This. Affixname =Affixname; } Public voidSend (string host, String user, string pwd) { This. Host =host; This. user =user; This. PWD =pwd; Properties Props=NewProperties (); //set the properties of the mail server to send mail (use NetEase's SMTP server here)Props.put ("Mail.smtp.host", host); //need to be authorized, that is, the user name and password verification, so as to pass verification (must have this)Props.put ("Mail.smtp.auth", "true"); //build a session with the props object you just set upSession session =session.getdefaultinstance (props); //with this sentence you can display the process information at the console during the sending of the message for debugging//(You can see the process of sending the message in the console)Session.setdebug (true); //defining a Message object with session parametersMimeMessage message =NewMimeMessage (session); Try { //Load Sender AddressMessage.setfrom (Newinternetaddress (from)); //Load recipient addressMessage.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Load TitleMessage.setsubject (subject); //add various parts of the message to the multipart object, including text content and attachmentsMultipart Multipart =NewMimemultipart (); //set the text content of a messageBodyPart Contentpart =NewMimeBodyPart (); Contentpart.settext ("The specific content of the message is here"); Multipart.addbodypart (Contentpart); //Add an attachmentBodyPart Messagebodypart =NewMimeBodyPart (); DataSource Source=NewFiledatasource (affix); //Add the contents of an attachmentMessagebodypart.setdatahandler (NewDataHandler (source)); //Add a caption for an attachment//It is important to use the following BASE64 encoded conversion to ensure that your Chinese attachment header name is not garbled when it is sentSun.misc.BASE64Encoder enc =NewSun.misc.BASE64Encoder (); Messagebodypart.setfilename ("=? GBK? B? " + Enc.encode (affixname.getbytes ()) + "? ="); Multipart.addbodypart (Messagebodypart); //Place the multipart object in the messagemessage.setcontent (multipart); //Save Messagemessage.savechanges (); //Send mailTransport Transport = Session.gettransport ("SMTP"); //connect to the server's mailboxTransport.connect (host, user, PWD); //Send the Mail outtransport.sendmessage (Message, message.getallrecipients ()); Transport.close (); } Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {mailutils cn=Newmailutils (); //set sender address, recipient address, and message headerCn.setaddress ("[Email protected]", "* * * @qq. com", "one JavaMail Mail with attachments"); //set the location and title of the attachment to sendCn.setaffix ("E:/1.txt", "1.txt"); /*** Set the SMTP server and mailbox account number and password * QQ mailbox as the occurrence of bad (unexplained) * 163 mailbox can be, but must open POP3/SMTP service and IMAP/SMTP services * Because the program is a third-party login, the login password must use 163 of the authorization Code*/ //NOTE: [The authorization code is not the same as your usual login password]Cn.send ("smtp.163.com", "[email protected]", "Password"); }}

Java Send mail

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.