JavaMail Send mail Demo (text and attachments)

Source: Internet
Author: User

 PackageCom.get.one;ImportJavax.mail.BodyPart;ImportJavax.mail.Message;ImportJavax.mail.Multipart;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.*;ImportJava.util.*;Importjavax.activation.*; Public classTest7 {//Sender's mailbox and password (replaced by their mailbox and password)     Public StaticString Myemailaccount = "* * * @qq. com";  Public StaticString Myemailpassword = "*******";  Public Static 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        PrivateString msg = "";//Message Content                 PublicString getmsg () {returnmsg; }         Public voidsetmsg (String msg) { This. msg =msg; }         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");//Request Authentication RequiredProps.put ("Mail.smtp.port", "465"); Props.put ("Mail.smtp.socketFactory.port", "465"); Props.put ("Mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); Props.put ("Mail.smtp.socketFactory.fallback", "false"); //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 (msg);                Multipart.addbodypart (Contentpart); if(!affix.equals ("")) {                    //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 ("* * * @qq. com", "* * @qq. com", "JavaMail Mail with Attachments"); //set the text content to sendCn.setmsg ("This is the content ah"); //set the location and title of the attachment to send//cn.setaffix ("C:/kms10.log", "Kms10.log");                        /*** Set the SMTP server and mailbox account and password * The mailbox must have the POP3/SMTP service and the IMAP/SMTP service enabled * Because the program is a third-party login, the login password must be With authorization Code*/            //NOTE: [The authorization code is not the same as your usual login password]Cn.send ("smtp.qq.com", Myemailaccount, Myemailpassword); }    }}

JavaMail Send mail Demo (text and attachments)

Related Article

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.