Example of sending mail using Java:
1. Send QQ Mail
1 Importjava.util.Properties;2 ImportJavax.mail.Message;3 Importjavax.mail.MessagingException;4 Importjavax.mail.Session;5 ImportJavax.mail.Transport;6 Importjavax.mail.internet.AddressException;7 Importjavax.mail.internet.InternetAddress;8 ImportJavax.mail.internet.MimeMessage;9 Ten Public classSendqqmailutil { One A Public Static voidMain (string[] args)throwsaddressexception,messagingexception { -Properties Properties =NewProperties (); -Properties.put ("Mail.transport.protocol", "SMTP");//Connection Agreement theProperties.put ("Mail.smtp.host", "smtp.qq.com");//Host name -Properties.put ("Mail.smtp.port", 465);//Port number -Properties.put ("Mail.smtp.auth", "true"); -Properties.put ("Mail.smtp.ssl.enable", "true");//set whether to use SSL secure connection---generally use +Properties.put ("Mail.debug", "true");//set whether the debug message is displayed true displays relevant information in the console - //get A Reply object +Session session =Session.getinstance (properties); A //Get Mail Object atMessage message =NewMimeMessage (session); - //Set up sender e-mail address -Message.setfrom (NewInternetAddress ("[Email protected]")); - //Set Recipient email address -Message.setrecipients (Message.RecipientType.TO,Newinternetaddress[]{NewInternetAddress ("[email protected]"),NewInternetAddress ("[email protected]"),NewInternetAddress ("[Email protected]")}); - //message.setrecipient (Message.RecipientType.TO, New internetaddress ("[email protected]");//A recipient in //set the message header -Message.setsubject ("Xmqtest"); to //set up message content +Message.settext ("e-mail content mail content xmqtest"); - //get the Mailman object theTransport Transport =Session.gettransport (); * //Connect to your own email account $Transport.connect ("[Email protected]", "xxxxxxxxxxxxx");//password for QQ mailbox to open the STMP service received after the client authorization codePanax Notoginseng //Send mail - transport.sendmessage (Message, message.getallrecipients ()); the transport.close (); + } A}
2. Send 163 e-mail
1 Importjava.io.IOException;2 ImportJava.util.*;3 4 ImportJavax.mail.*;5 Importjavax.mail.internet.*;6 Importjavax.activation.*;7 8 Public classSendmailutil {9 Ten StaticString HOST = "";//SMTP Server One StaticString from = "";//Sender Address A StaticString to = "";//Recipient Address - StaticString affix = "";//Attachment Address - StaticString affixname = "";//Attachment name the StaticString USER = "";//User name - StaticString PWD = "";//163 of the Authorization Code - StaticString SUBJECT = "";//message Header - Staticstring[] TOS =NULL; + - Static { + Try { AProperties props =NewProperties (); atProps.load (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Config.properties")); //Get related parameters from a custom configuration file -Host=props.getproperty ("Host"); -From=props.getproperty ("from"); -To=props.getproperty ("to"); -Tos=to.split (","); -Affix=props.getproperty ("Affix"); inAffixname=props.getproperty ("Affixname"); -User=props.getproperty ("User"); toPwd=props.getproperty ("pwd"); +Subject=props.getproperty ("SUBJECT"); -}Catch(IOException e) { the e.printstacktrace (); * } $ }Panax Notoginseng - /** the * Send mail + * @paramHost A * @paramUser the * @parampwd + */ - Public Static voidSend (String context) { $Properties props =NewProperties (); $Props.put ("Mail.smtp.host", host);//set the properties of the mail server to send mail (use NetEase's SMTP server here) -Props.put ("Mail.smtp.auth", "true");//need to be authorized, that is, the user name and password verification, so as to pass verification (must have this) -Session session = Session.getdefaultinstance (props);//building a session with a props object theSession.setdebug (true); -MimeMessage message =NewMimeMessage (session);//defining a Message object with session parametersWuyi Try { theMessage.setfrom (NewInternetAddress (from));//Load Sender Address -internetaddress[] SendTo =NewInternetaddress[tos.length];//Load recipient address Wu for(inti = 0; i < tos.length; i++) { -Sendto[i] =Newinternetaddress (Tos[i]); About } $ message.addrecipients (message.recipienttype.to,sendto); -Message.addrecipients (MimeMessage.RecipientType.CC, Internetaddress.parse (from));//set up a copy of your (sender's) copy before sending it to the recipient, or you will be treated as a spam message with 554 errors -Message.setsubject (SUBJECT);//Load Title -Multipart Multipart =NewMimemultipart ();//add various parts of the message to the multipart object, including text content and attachments ABodyPart Contentpart =NewMimeBodyPart ();//set the text content of a message + Contentpart.settext (context); the Multipart.addbodypart (contentpart); - if(! Affix.isempty ()) {//Add an attachment $BodyPart Messagebodypart =NewMimeBodyPart (); theDataSource Source =NewFiledatasource (affix); theMessagebodypart.setdatahandler (NewDataHandler (source));//Add the contents of an attachment theSun.misc.BASE64Encoder enc =NewSun.misc.BASE64Encoder ();//Add a caption for an attachment theMessagebodypart.setfilename ("=? GBK? B? " + Enc.encode (affixname.getbytes ()) + "? ="); - Multipart.addbodypart (messagebodypart); in } theMessage.setcontent (multipart);//Place the multipart object in the message theMessage.savechanges ();//Save Message AboutTransport Transport = Session.gettransport ("SMTP");//Send mail theTransport.connect (HOST, USER, PWD);//connect to the server's mailbox theTransport.sendmessage (Message, message.getallrecipients ());//Send the Mail out theTransport.close ();//Close Connection +}Catch(Exception e) { - e.printstacktrace (); the }Bayi } the the - //Public static void Main (string[] args) { - //Send ("content"); the // } the the}
3, regarding the Mailbox service authorization configuration Self-reference official document.
such as 163 mailbox settings:
Java Send mail sample