Use Java Mail to send and receive emails (reproduced)
Using Java Mail to send and receive mail This article is compiled by some chapters of the "J2Se Advanced" book written by JR hosting, "J2SE advance" is writing, perfect stage. After you read, there is any suggestion, criticism, please contact me, or leave a message here. "J2SE advance" Writing project group Thank you for reading this article. With the continuous promotion of network applications, more and more emails are used by everyone. Although we often connect email with Foxmail, Outlook email clients, but often we also need to program the implementation of sending and receive mail, such as receipt of mail after a website registered user, or when shopping Send a confirmation email within a few minutes after completing the order. For such demand, we cannot write mail or handlers yourself through existing mail clients. Here you explain to you how to use JavaMail to implement mail delivery. Note: This article only intends to discuss some of the techniques of JavaMail tape accessories mail, so just give some code, more more exciting content, please look forward to the "J2Se Advanced" book. 1. Send mail with attachment We usually decompose into 2 most, one is a message header, such as senders, receives, the subject, and another part of the message, which includes an accessory of the message. When we send a normal mail, the type of content setting is "text / html". After the attachment, we need to set the content type to Multipart, and the content includes an attachment and "text / html" body. The following this tells you how to place the attachment into the email.
private Multipart getMultipart () throws MessagingException, UnsupportedEncodingException {MimeMultipart mp = new MimeMultipart (); try {// set content in the content MimeBodyPart contentMbp = new MimeBodyPart (); // specify the character set, otherwise it will be garbled contentMbp.setContent ( _mailContent.getContent (), "text / html; charset = GB2312"); mp.addbodypart (contentMBP); // Add attachment for (int i = 0; i <_mailattachment.getattachpath (). size (); i ) { MimeBodyPart mbp = new MimeBodyPart (); FileDataSource fds = new FileDataSource ((String) _mailAttachment.getAttachPath () get (i).); mbp.setDataHandler (new DataHandler (fds)); mbp.setFileName (MimeUtility.encodeWord (fds. GetName (), "GB2312", NULL); mp.addbodypart (MBP);}} catch (messagingException IE {system.out.println ("set content message error ..." i E.getMessage ()); throw IE;} catch (unsupporteencodingexception {system.out.println ("Encode the filename error ..." IE.getMessage ()); throw ie;} return mp;} Place an attachment The precautions are as follows: I need to pay attention to the problem of the character set when Mail. Not only the content is set in Content, but also the file name also needs to be set. If we remove MBP.SetFileName (fds.getname (), "GB2312", "GB2312", "GB2312", this sentence, then the attachment you choose will still be taken to the email, but you can't see it in the attachment. We can know by viewing email. We use this feature to implement the HTML language written in the seminated content, and include the message of the image information. 2. Sending the message containing the HTML page, everyone knows that the HTML language can bring a picture link ( img>), then we need to send mail The pictures of these links are specially processed. Otherwise, the picture will not be seen when the other party receives the message.
Our special treatment is to send them as an attachment, but not in the attachment. To do this, you must first analyze the input content and find the path to the picture. The code is then generated in IMG> this code into IMG>. We use MBP1.SetHeader ("Content-ID", "IMG") when sending attachments. How do I specifically parse the operation of Content, I haven't described it. I now give an example of how to send a modified Content. // For the Content of sending an HTML type. Inside the picture. For (int i = 0; i <_mailcontent.getimghash (). size (); i ) {mimebodypart mbp1 = new mimebodypart (); // Get picture data fileDataSource fds = new fileDataSource (String) _mailContent.getiMGhash () .get ("IMG" i)); // Set to MIMEBODYPART Mbp1.SetDataHandler (NEW DataHandler (FDS)); // Setting the corresponding relationship of the picture attachment and HTML Mbp1.setHeader ("Content-ID", "IMG " i); mp.addbodypart (mbp1); The status of the message is defined in class flags.flag. Including the following: flags.flag.answered flags.flag.deleted Flag.Flag.Draft flag.flag.flagged flags.flag.recent flags.flag.seen flags.flag.user we can set according to different needs, but It should be noted that not all servers support these states. We can use the getPermanentFlags method before doing operations to get the status in the message.
Refer to the following code MSSAGE M = Folder.getMessage (1); // set the deleted flag m.setflag (Flags.Flag.deleted, true); // check if deleted flag is set of this message ing (m.isset) .Flag.deleted)) System.out.println ("deleted message"); // Examine All System Flags for this message flags flags = m.GetFlags (); Flags.Flag [] sf = flags.getsystemflags (); for (INT i = 0; i For (int i = 0; i