Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaMail API

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use JavaMail API, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

How to use JavaMail API

I will lead you to learn some cases of using Java Mail API tasks.

1. Send email

After obtaining the Session, create and fill in the mail message, and then send it to the mail server. This is the process of using Java Mail API to send mail. Before sending mail, we need to set up the SMTP server: by setting the mail.smtp.host property of Properties.

String host =...; String from =...; String to =...; / / Get system propertiesProperties props = System.getProperties (); / / Setup mail serverprops.put ("mail.smtp.host", host); / / Get sessionSession session = Session.getDefaultInstance (props, null); / / Define messageMimeMessage message = new MimeMessage (session); message.setFrom (new InternetAddress (from)); message.addRecipient (Message.RecipientType.TO, new InternetAddress (to); message.setSubject ("Hello JavaMail")) Message.setText ("Welcome to JavaMail"); / / Send messageTransport.send (message)

Since exceptions may be thrown during the process of creating and sending mail messages, we need to put the above code into the try-catch building block.

2. Receive mail

In order to read the mail, we get the session and connect to the appropriate store of the mailbox, open the appropriate Folder, and then get the mail we want, and of course don't forget to close the connection at the end.

String host =...; String username =...; String password =...; / / Create empty propertiesProperties props = new Properties (); / / Get sessionSession session = Session.getDefaultInstance (props, null); / / Get the storeStore store = session.getStore ("pop3"); store.connect (host, username, password); / / Get folderFolder folder = store.getFolder ("INBOX"); folder.open (Folder.READ_ONLY); / / Get directoryMessage message [] = folder.getMessages (); for (int item0, n=message.length) I ":" + message.getFrom () [0] + "\ t" + message.getSubject ();} / / Close connection folder.close (false); store.close ()

What the above code does is read each message from the mailbox and display the sender's address and subject of the message. Technically, there is the possibility of an exception: getFrom () [0] throws an exception when the sender's address is empty.

The following code snippet effectively explains how to read the email content. After displaying the sender and subject of each email, a user prompt will appear to confirm whether the user has read the email. If you enter YES, we can use the Message.writeTo (java.io.OutputStream os) method to output the email content to the console. For the specific use of Message.writeTo (), please see JavaMail API.

BufferedReader reader = new BufferedReader (new InputStreamReader (System.in)); / / Get directoryMessage message [] = folder.getMessages (); for (int iTunes 0, n messages. Messages; I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report