In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to send QQ email in SpringBoot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to send QQ email in SpringBoot".
Mail protocol
We often hear about various mail protocols, such as SMTP, POP3, IMAP, so what is the purpose of these protocols and what is the difference? Let's discuss this problem first.
SMTP is an application layer protocol based on TCP/IP, and its status is similar to that of a HTTP,SMTP server listening by default, with a port number of 25. Seeing this, friends may think that since SMTP is an application layer protocol based on TCP/IP, can I also send an email through Socket? The answer is yes.
In life, we have to go through the following steps to deliver an email:
1. Xiao Wang in Shenzhen first delivered the mail to the post office in Shenzhen.
two。 The post office in Shenzhen transports the mail to the post office in Shanghai.
3. Xiao Zhang from Shanghai came to the post office to pick up the mail.
This is a scaled-down version of the email sending process in life. These three steps can correspond to our email sending process, assuming sending email from aaa@qq.com to 111@163.com:
1.aaa@qq.com delivers the mail to Tencent's mail server first.
two。 Tencent's mail server delivers our mail to NetEase's mail server.
3.111@163.com logs in to NetEase's mail server to check email
Mail delivery is roughly this process, this process involves a number of protocols, let's take a look at them separately.
The full name of SMTP protocol is Simple Mail Transfer Protocol, which is translated as simple Mail transfer Protocol. It defines the communication rules between mail client software and SMTP server, as well as between SMTP server and SMTP server. In other words, aaa@qq.com users use SMTP protocol in the process of first delivering mail to Tencent's SMTP server, and then Tencent's SMTP server delivers mail to NetEase's SMTP server. The process still uses SMTP protocol, and SMTP server is used to receive mail. The full name of the POP3 protocol is Post Office Protocol, which is translated as the post office protocol, which defines the communication rules between the mail client and the POP3 server, so in what scenario will the protocol be used? When the mail arrives at NetEase's SMTP server, 111@163.com users need to log in to the server to check the mail, and this is when the protocol is used: the mail service provider will provide a special mail storage space for each user. After the SMTP server receives the mail, it will save the mail to the corresponding user's mail storage space. If the user wants to read the mail, It needs to be done through the POP3 mail server of the mail service provider. Finally, some friends may have heard of the IMAP protocol, which is an extension of the POP3 protocol with stronger functions and similar effects, so I won't repeat it here.
Prepare to send an QQ email
First of all, we need to log in to QQ Mail's web version and click the settings button above:
Then click the account tab:
Find the option to enable POP3/SMTP in the account tab, as follows:
Click to open, turn on the relevant functions, the opening process requires mobile phone number verification, you can follow the steps, do not repeat. After opening it successfully, you can get an authorization code, save the number and use it later.
Then we need the jar package JavaxMail.
Send a simple email
If we only send a simple text, the sending method is relatively simple, and the whole process can be divided into three steps as follows:
Step 1: construct the basic environment of the SMTP mail server
Properties properties = new Properties (); properties.setProperty ("mail.host", "smtp.qq.com"); properties.setProperty ("mail.transport.protocol", "smtp"); properties.setProperty ("mail.smtp.auth", "true"); properties.setProperty ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty ("mail.smtp.port", "465"); Session session = Session.getDefaultInstance (properties); session.setDebug (true)
Step 2: construct the message
MimeMessage mimeMessage = new MimeMessage (session); mimeMessage.addRecipients (Message.RecipientType.TO, "111@qq.com"); / / set recipient mimeMessage.addRecipients (Message.RecipientType.CC, "222@qq.com"); / / CC mimeMessage.setFrom ("1510161612@qq.com"); / / Sender mimeMessage.setSubject ("Test message subject"); / / message subject mimeMessage.setContent ("Hello, this is a test email", "text/html;charset=utf-8") / / text
Step 3: send an email
Transport transport = session.getTransport (); transport.connect ("smtp.qq.com", "333@qq.com", "authorization code just applied for"); transport.sendMessage (mimeMessage, mimeMessage.getAllRecipients ()); / / send email. The second parameter is recipient transport.close (); complex email
To send a complex email, the first step is the same as the third step, only the second step is more troublesome to construct the email, so next show your partners an email that sends one picture and text + two attachments. To send a complex email, you need to be familiar with three concepts, as follows:
1.MimeMessage: this class is an email message that understands MIME types and headers
2.MimeMultipart: this class defines methods to add, delete, and get different parts of the message.
3.MimeBodyPart: this object represents part of the content of a MimeMessage object. Each MimeBodyPart is considered to have two parts: the MIME type and the content that matches that type.
The complete message generation process is as follows (steps 1 and 3 refer to the above):
MimeMessage mimeMessage = new MimeMessage (session); mimeMessage.addRecipients (Message.RecipientType.TO, "111@qq.com"); / / set recipient mimeMessage.addRecipients (Message.RecipientType.CC, "222@qq.com"); / / CC mimeMessage.setFrom ("333@qq.com"); / / Sender mimeMessage.setSubject ("Test message subject"); / / message subject MimeMultipart mixed = new MimeMultipart ("mixed"); mimeMessage.setContent (mixed) / / set the MIME message body of the whole message to a mixed combinatorial relationship MimeBodyPart attach2 = new MimeBodyPart (); / / create attachment 1MimeBodyPart attach3 = new MimeBodyPart (); / / create attachment 2MimeBodyPart content = new MimeBodyPart (); / / create email body mixed.addBodyPart (attach2); / / add attachment 1 to MIME message body mixed.addBodyPart (attach3); / / add attachment 2 to MIME message body mixed.addBodyPart (content) / / add the body to the message body FileDataSource fds1 = new FileDataSource (new File ("C:\ Users\\ sang\\ Desktop\\ 1.png"); / / construct the data source DataHandler dh2 = new DataHandler (fds1) for attachment 1; / / data processing attach2.setDataHandler (dh2); / / set the data source attach2.setFileName for attachment 1 ("1.png") / / set the file name of Annex I / / the operation of Annex II is similar to that of Annex I, so FileDataSource fds2 = new FileDataSource ("C:\ Users\\ sang\\ Desktop\\ blog notes .xlsx") is not annotated here; DataHandler dh3 = new DataHandler (fds2); attach3.setDataHandler (dh3); attach3.setFileName (MimeUtility.encodeText ("blog notes .xlsx")) / / when setting a file name in Chinese, you can use the encodeText method in the MimeUtility class to avoid garbled MimeMultipart bodyMimeMultipart = new MimeMultipart ("related"); / / set the MIME type of the body content.setContent (bodyMimeMultipart); / / add bodyMimeMultipart to the body of the message MimeBodyPart bodyPart = new MimeBodyPart (); / / bodyPart.setContent the HTML part of the body ("Hello Hello, this is a test email
"," text/html;charset=utf-8 "); MimeBodyPart picPart = new MimeBodyPart (); / / the picture part of the body DataHandler dataHandler = new DataHandler (" C:\\ Users\\ sang\\ Desktop\\ 2.png "); picPart.setDataHandler (dataHandler); picPart.setContentID (" 2.png "); / / add the HTML and picture part of the text to bodyMimeMultipart bodyMimeMultipart.addBodyPart (bodyPart); bodyMimeMultipart.addBodyPart (picPart); mimeMessage.saveChanges ()
It's that simple for OK,Java Mail to send QQ emails, but I won't repeat them here as for others, such as 163 Sina, which are written in a similar way.
Thank you for your reading, the above is the content of "how to send QQ mail in SpringBoot". After the study of this article, I believe you have a deeper understanding of how to send QQ mail in SpringBoot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.