Java Send QQ Mail

Source: Internet
Author: User

The interview was asked this question, others asked me to use Java sent the mail did not, was asked a face confused force. Then the study, not difficult, according to the online method of tossing a few days to get out.

First, the use of QQ mailbox to send mail before the need to configure in the mailbox, open POP3 and SMTP services, in fact, this is two network protocols, one is to accept the mail protocol, one is to send mail protocol;

POP3

is the abbreviation for post Office Protocol 3, the 3rd version of the Post Office Protocol, which specifies how to connect a personal computer to an Internet mail server and an electronic protocol to download e-mail. It is the first offline protocol standard for Internet e-mail, POP3 allows users to store messages from the server on a local host (that is, their own computer) while deleting messages saved on the mail server, while the POP3 server is the receiving mail server that follows the POP3 protocol to receive e-mail.

Smtp

The full name of SMTP is "Simple Mail Transfer Protocol", which is simply the message Transfer Protocol. It is a set of specifications for transferring messages from the source address to the destination, which controls how messages are relayed. The SMTP protocol is a TCP/IP protocol cluster that helps each computer find its next destination when sending or relaying letters. The SMTP server is the outgoing mail server that follows the SMTP protocol.

Log in to the Web version of the mailbox, in the settings-"account can open these two services. You need to verify with your phone, and then save the generated authorization code, which is used in code or in third-party mail management software.

Then there is the need for the jar package resources, if the use of pure Java code to send mail only need two jar package, here are resources http://www.runoob.com/java/java-sending-email.html

Activation.jar

Mail.jar

You can then send the message using the code, which describes sending a simple text file and an HTML-formatted message.

 Packageqqmail.test;Importjava.util.Date;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;/*** Send QQ Mail * *@authorYaoqi * @date July 13, 2017*/ Public classTestmail { Public Static voidMain (string[] args) {String from= "34845*** @qq. com";//E -mail address of the senderString to = "16841*** @qq. com";//recipient's email addressString subject = "MailTest";//Message SubjectString BODY = "message body";//Message Content//set some properties for sending messagesProperties prop =NewProperties (); Prop.setproperty ("Mail.transport.protocol", "SMTP"); Prop.setproperty ("Mail.smtp.host", "smtp.qq.com"); Prop.setproperty ("Mail.smtp.port", "465"); Prop.setproperty ("Mail.smtp.auth", "true"); Prop.setproperty ("Mail.debug", "true"); Prop.setproperty ("Mail.smtp.ssl.enable", "true");//QQ Mailbox must set this item, SSL encryption optionSession Session= Session.getdefaultinstance (prop,NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {//here need to verify the mailbox authorization code, QQ mailbox need authorization Code                return NewPasswordauthentication (From, "rvrvrjloadxm****");                }        }); MimeMessage message=NewMimeMessage (session); Try{Transport Transport=Session.gettransport (); Message.setfrom (Newinternetaddress (from)); Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); Message.setsentdate (NewDate ());            Message.setsubject (subject); //If you are just sending text, use the SetText () methodMessage.settext (body); //If you are sending an HTML-formatted message, you will need to use the SetContent () method and set the parameter text/html, which is equivalent and will overwrite the contents of the previous method setting .Message.setcontent (" );            Message.savechanges (); Session.setdebug (true); Transport.connect (From,"rvrvrjloadxm****");            Transport.sendmessage (Message, message.getallrecipients ());        Transport.close (); } Catch(Exception e)        {E.printstacktrace (); }    }}

If you want to send a message with an attachment, you need to add multiple messages, the rest are the same, and the message is combined into one message at a later.

Try{Transport Transport=Session.gettransport (); Message.setfrom (Newinternetaddress (from)); Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); Message.setsentdate (NewDate ());            Message.setsubject (subject); //Create a message body sectionBodyPart Messagebodypart =NewMimeBodyPart (); Messagebodypart.settext ("This is the message body"); //Creating multiple MessagesMultipart Multipart =NewMimemultipart (); Multipart.addbodypart (Messagebodypart); //Adding a message body part to a multi-message//Create an attachmentMessagebodypart =NewMimeBodyPart (); File File=NewFile ("D:\\java\\workspace\\sendmaildemo\\file.txt"); DataSource Source=Newfiledatasource (file); Messagebodypart.setdatahandler (NewDataHandler (source)); Messagebodypart.setfilename ("File");            Multipart.addbodypart (Messagebodypart);            Message.setcontent (multipart);            Message.savechanges (); Session.setdebug (true); Transport.connect (From,"rvrvrjloadxm****");            Transport.sendmessage (Message, message.getallrecipients ());        Transport.close (); } Catch(Exception e) {e.printstacktrace (); }

Java Send QQ 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.