The step of sending and receiving mail JavaMail

First, e-mail

1) obtaining Session

i) the implementation of a subclass of class Authentication, which implement the public PasswordAuthentication getPasswordAuthentication () method

ii) an upper class New example, user name and password

iii) New a Properties object properties set mail.smtp.host and mail.smtp.auth

iv) with too static method of Session, get a Session instance

2) generating a Message

i) there is no e-mail attachments

   Step: new instance of a MimeMessage (The Session)

   Step Two: Set subject, text attribute to the Message instance

ii) the e-mail attachments

   The first step: According to one example MimeMessage Session new (Message)

   Step Two: Set Message subject property

   The third step: new instance and a MimeBodyPart Mulipart (MimeMulipart) Example

   The fourth step: to set an example MimeBodyPart text message

   Step 5: MimeBodyPart instance, add the instance to Mulipart

   Step Six: according to the number of attachments cycle:

           New MimeBodyPart examples

           Get FileDatasource

           The FileDatasource set to MimeBodyPart

           Set MimeBodyPart file name

           Adding to Mulipart MimeBodyPart

   Step Seven: The Mulipart set of content MimeMessage

3) Send a message

i) Set the Message fromAddress, toAddress, ccAddress, bccAddress

ii) Transport Send Mail    

 


Second, the incoming mail

1) new Properties instance, the set value mail.pop3.host

2) Get Session instance

3) The Session, examples obtaining Store

4) connected to store

5) acquiring Index Folder

6) Open the folder

7) get inside the folder all Message

8) optimization Message Find with FetchProfile

    FetchProfile profile = new FetchProfile();

    profile.add(FetchProfile.Item.ENVELOPE);

    profile.add(FetchProfile.Item.FLAGS);

    profile.add("X-Mailer");

    inbox.fetch(msg, profile);

9) If the judge Message of MimeType type is text / * type, you can directly get mail from the address, title and content, otherwise perform the following steps from the Message

10) acquires Multipart Message from the

11) in the ergodic Multipart BodyPart

12 if) is determined bodyPart Disposition of Part.ATTACHMENT

13) If not, direct access to the inside of the content bodyPart

14) or obtain the file name and file stream bodyPart of (inputstream), writes the stream local files, to achieve download attachments

Reproduced in: https: //my.oschina.net/usenrong/blog/197834

Guess you like

Origin blog.csdn.net/weixin_34291004/article/details/92028881