Implemented using Oracle's official JavaMail, Javamail:https://java.net/projects/javamail/pages/home
Adding the downloaded jar package to the project path is OK, I'm using the latest version of 1.5.2 Javax.mail.jar.
Reference to the Mail protocol: What is POP3, SMTP, and IMAP?
The following example is through my sina email ([email protected]) to the QQ mailbox ([email protected] do not know who's mailbox) to send e-mail, the following gives the implementation code:
import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage; Public class simplemail { Public Static voidMain (string[] args) {Properties props =NewProperties ();//Use the SMTP protocol to send mail, my Sina mailbox is. CN is not. com, so the SMTP server is smtp.sina.cnProps.put ("Mail.smtp.host","smtp.sina.cn"); Session session = Session.getinstance (props,NULL);Try{MimeMessage msg =NewMimeMessage (session);//Set Sender mailboxMsg.setfrom ("[email protected]");//Set up recipient mailboxesMsg.setrecipients (Message.RecipientType.TO,"[email protected]");//Set ThemeMsg.setsubject ("This is a test");//Set DateMsg.setsentdate (NewDate ());//Set body contentMsg.settext ("How was You?\nthis is a test, please do not reply!");//Send mail, parameters for mail message, sender's mailbox and sender's mailbox passwordTransport.send (MSG,"[email protected]","Here is the sender's password"); }Catch(Messagingexception Mex) {System.err.println ("Send failed! Exception: "+ Mex); } }}
The more important classes in JavaMail are session, store, and folder.
Java simple implementation of sending mail