I. Introduction SMTP is short for "Simple Mail Transfer Protocol", which is a Simple Mail transmission Protocol used for Mail sending. SMTP authentication requires you to log on to the SMTP server only after the account name and password are provided. POP3 (Post Office Protocol 3) Protocol allows the email client to download emails from the server. However, operations on the client (such as moving emails and marking read messages) are not reported to the server, for example, if the client receives three emails from the mailbox and moves them to other folders, the emails on the mailbox server are not moved at the same time. IMAP (Internet Mail Access Protocol) provides two-way communication between webmail and the email client. client operations are fed back to the server for Mail operations, emails on the server will also perform the corresponding action. At the same time, IMAP provides convenient email download services like POP3, allowing users to read offline. The abstract browsing function provided by IMAP allows you to determine whether to download after reading the arrival time, topic, sender, size, and other information of all emails. In addition, IMAP allows you to access new emails from multiple devices at any time. In short, IMAP provides users with a more convenient and reliable experience. POP3 is easier to lose or download the same email multiple times, but IMAP can avoid these problems through the bidirectional synchronization between the mail client and webmail. NOTE: If "Save to sent" is set in the web mailbox, when sending a mail using the client POP service, emails are automatically synchronized to the "sent" folder on the web page. 2. The mail package com is sent using the SMTP protocol. like. email; import java. io. file; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. properties; import javax. activation. dataHandler; import javax. activation. dataSource; import javax. activation. fileDataSource; import javax. mail. authenticator; import javax. mail. bodyPart; impo Rt javax. mail. message; import javax. mail. messagingException; import javax. mail. multipart; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. addressException; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeBodyPart; import javax. mail. internet. mimeMessage; import javax. mail. internet. mimeMultipart;/*** @ author like * @ E-mail kelee1987 @ hot Mail.com * @ CreateTime 2:19:11 */public class SMTPSendTest {private static final int receept = 1; private static final String attachmentDir = ""; public static void sendEmail (Email emailInfo) throws UnsupportedEncodingException, MessagingException {Properties props = new Properties (); props. put ("mail. smtp. host ", emailInfo. getSmtpServer (); props. put ("mail. smtp. port ", emailInfo. getS MtpPort (); props. put ("mail. smtp. auth "," true "); Authenticator auth = new SMTPAuthenticator (emailInfo. getUsername (), emailInfo. getPassword (); Session session = Session. getInstance (props, auth); session. setDebug (false); Message msg = new MimeMessage (session); msg. setFrom (new InternetAddress (emailInfo. getFrom (), emailInfo. getFromName (); msg. setRecipients (Message. recipientType. TO, getEmailRecipien T (emailInfo. getTO (); msg. setRecipients (Message. recipientType. CC, getEmailRecipient (emailInfo. getCC (); msg. setRecipients (Message. recipientType. BCC, getEmailRecipient (emailInfo. getBCC (); if (emailInfo. getreceip() = receip) {msg. setHeader ("Disposition-Notification-To", emailInfo. getFrom ();} msg. setSubject (emailInfo. getSubject (); // sets the message content (including the HTML content of the attachment) msg. setContent (getMultipart (emailI Nfo. getContent (), attachmentDir, emailInfo. getAttachment (); msg. saveChanges (); Transport. send (msg);}/*** encapsulate email address ** @ param address * @ return * @ throws AddressException */private static InternetAddress [] getEmailRecipient (ArrayList <String> address) throws AddressException {int toLen = 0; if (address! = Null) {toLen = address. size ();} InternetAddress [] addressTo = new InternetAddress [toLen]; if (toLen! = 0) {String m_st_email = ""; for (int I = 0; I <toLen; I ++) {m_st_email = (String) address. get (I); if (m_st_email! = Null) addressTo [I] = new InternetAddress (m_st_email.trim () ;}return addressTo;} private static Multipart getMultipart (String text, String attachParentDir, ArrayList <String> attachment) throws MessagingException {// mixed message content Multipart multi = new MimeMultipart ("mixed"); // mixed MIME message // Add text content multi. addBodyPart (createContent (text); // Add the attachment content for (int I = 0; I <attachment. size (); I ++) {String AttachmentI = (String) attachment. get (I); // The actual storage name of the attachment String fileRealName = attachmentI. substring (attachmentI. indexOf ("<") + 1 ). trim (); // The display name of the attachment in the email String fileShowName = attachmentI. substring (0, attachmentI. indexOf (">"); multi. addBodyPart (createAttachment (fileShowName, new File (attachParentDir + fileRealName); // embedded attachment} return multi ;} /*** process the email attachment ** @ param fileName * display name of the attachment * @ param file * File * @ return javax. mail. bodyPart (javaMail mail content) * @ throws MessagingException */private static BodyPart createAttachment (String fileName, File) throws MessagingException {BodyPart attach = new MimeBodyPart (); dataSource ds = new FileDataSource (file); attach. setDataHandler (new DataHandler (ds); try {// recode the file name to solve the garbled problem attach. setFileName (new String (fileName. getBytes (), "ISO8859-1");} catc H (UnsupportedEncodingException e) {e. printStackTrace ();} return attach;}/*** processing of mail text ** @ param text * text content * @ return javax. mail. bodyPart (javaMail mail content) * @ throws MessagingException */private static BodyPart createContent (String text) throws MessagingException {BodyPart content = new MimeBodyPart (); // The Mail body is also a combined message that can contain multiple MimeBodyPart Multipart relate = new MimeMultipart ("related"); BodyP Art html = new MimeBodyPart (); html. setContent (text, "text/html; charset = gbk"); relate. addBodyPart (html); content. setContent (relate); return content;} email content structure: 3. Accept mail package com. like. email; import java. util. properties; import javax. mail. authenticator; import javax. mail. flags; import javax. mail. folder; import javax. mail. message; import javax. mail. messagingException; import javax. mail. sessi On; import javax. mail. store; import com. sun. mail. pop3.POP3Folder;/*** although the POP3 protocol is obviously inferior to the IMAP protocol, it is still widely used and not all email servers support the IMAP protocol. * @ Author like * @ E-mail kelee1987@hotmail.com * @ CreateTime 4:25:23 */public class OP3Test {public static void receive (String server, int port, String username, String password, boolean isDelete) throws MessagingException {Properties props = System. getProperties (); Authenticator auth = new POPAuthenticator (username, password); Session session = Session. getInstance (props, auth); se Ssion. setDebug (false); Store = session. getStore ("POP3"); store. connect (server, port, username, password); // get the inbox POP3Folder folder = (POP3Folder) store. getFolder ("INBOX"); try {// enable folder in read/write mode. open (Folder. READ_WRITE);} catch (MessagingException ex) {// enable folder in the system mode. open (Folder. READ_ONLY);} // int totalMessages = folder. getMessageCount (); Message m_message = null; Message [] msgs = folder. GetMessages (); for (int I = 0; I <msgs. length; I ++) {m_message = msgs [I]; String UID = folder. getUID (m_message); if (haveReceived (UID) {// insert database // mailList. add (new RecvMailTask (m_message, // p_st_attachmentParentDir, UID); // set to read, supported by IMAP protocol, POP3 protocol does not support this function m_message.setFlag (Flags. flag. SEEN, true); // POP3 protocol can delete m_message.setFlag (Flags. flag. DELETED, isDelete) ;}}/ *** the POP3 protocol does not synchronize the mail operations with the server. Whether the email is read or not. If the email on the server is not deleted after receiving the email, the email will still be received next time. * The server will give each email a unique UID, Which is saved to the database when receiving the email as the basis for whether the email has been received at the next reception. * There are two ways: (1) read all the UIDs from the database each time you receive them, and determine whether the UIDs of the received emails are included. * The advantage is that the database is read-only once, and the disadvantage is that there may be a large number of UIDs read. * (2) when receiving each email, the UID is used as the parameter to query the database and whether data is returned as the judgment basis. ** @ Param UID * @ return */private static boolean haveReceived (String UID) {// I personally think the first option is better. If there are many emails in the mailbox and receiving is frequent, therefore, a large number of database connections will be generated each time a message is received. // First, put the obtained UID Set into the Set, and use the contains () method to directly judge return true ;}