JavaMail, as the name implies, provides developers with programming interfaces for dealing with e-mail. It is the API that Sun releases to handle email. It is easy to perform some common mail transfer. We can develop an application similar to Microsoft Outlook based on JavaMail.
The core classes used to process e-mail in the JavaMail package are :session,message,address,authenticator,store,transport, Folder , and so on. The session defines a basic mail session that needs to read from the properties like a mail server, user name, and password. However, the JDK does not contain the use of JavaMail to send mail requires the use of sun released Mail.jar and Activtion.jar two packages.
Mail protocol:
SMTP protocol: Simple Mail Transfer Protocol, which is simply a message transfer protocol for sending e-mail
POP3 protocol: Post Office Protocol 3, a third version of the Post Office Protocol, for receiving mail
IMAP protocol: Internet Message Access Protocol, an Internet messaging protocol, is an alternative protocol to POP3
JavaMail e-mail flow:
1. Build the James Mail server
2. Install outlook[mail client]
3. Configuring the Outlook mail client
4. Build the James Mail server
1. Build the James Mail server
James is an open source project for Apache, pure Java implementation.
First we need to download apache-james-2.3.2.zip (batch: http://pan.baidu.com/s/1pJoyg7h)
Second, run the Run.bat in the Bin directory to start the server
Then run the cmd command [Telnet localhost 4555]
Finally, configure the server via Apps\james\sar-inf\config.xml (Modify node)
2. Install outlook[Mail client],3. Configure the Outlook mail client
Enter Product key (optional, test, do not need to enter)
Product Key: PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
Create a user account, connect according to Telnet localhost 4555 ,
User name and password default to root
Enter Help to add an account (AddUser)
Install as required, next, note the following:
Hosts file:
The installation is complete and tested:
4. Build the James Mail server
Finally send mail using javamail:
Requirements: The account receives the tasks sent by JavaMail, as well as the attachment information.
Core code:
Create a class Emailauthenticator and inherit from authenticator, and populate the user name and password
Create the Mail class (set message information):
public class Mail {private String mailserver,from,to,mailsubject,mailcontent; Private String Username,password; Public mail () {//Set message information//Authentication login username Username= "[email protected]"; Authentication password password= "HQ"; Mail server mailserver= "192.168.17.176" for the certified mailbox; Sender information from= "WJ"; Recipient Information to= "[email protected]"; Message title mailsubject= "We are all good children 333"; Message content mailcontent= "This is a test mail!" If there is a similarity, it is impossible "; }//Set mail server @SuppressWarnings ("static-access") public void Send () {Properties prop=system.getproperties (); Specify Mail server Prop.put ("Mail.smtp.host", mailserver); Whether to turn on the authentication Prop.put ("Mail.smtp.auth", "true"); SMTP protocol Prop.put ("Mail.smtp.port", "25"); Generate session Service Emailauthenticator Mailauth=new emailauthenticator (username, password); Session mailsession=session.getinstance (prop, (Authenticator) mailauth); try {//Encapsulate Message Object message Message=newMimeMessage (mailsession); Message.setfrom (New InternetAddress (from)); Sender Message.setrecipient (Message.RecipientType.TO, New internetaddress (to));//Recipient Message.setsubject (Mailsubject); Set content (set character sets handling garbled problem) Message.setcontent (mailcontent, "TEXT/HTML;CHARSET=GBK"); Message.setsentdate (New Date ()); Create Transport instance, send mail Transport tran=mailsession.gettransport ("SMTP"); Tran.send (Message,message.getallrecipients ()); Tran.close (); } catch (Exception e) {e.printstacktrace (); } }
Test class:
public class MyTest {public static void Main (string[] args) { mail mail=new mail (); Mail.send (); System.out.println ("success!"); } }
Vii. Send mail with attachments
public class Mailwithattachment {private Javamailsender mailsender;//must use Javamailsender public void Setmailsender (Javamailsender MailSender) {this.mailsender = MailSender; } public void Send () throws messagingexception,ioexception{mimemessage mimemessage = mailsender.createmime Message (); Mimemessagehelper helper = new Mimemessagehelper (MimeMessage, True, "UTF-8"); Helper.setfrom ("[email protected]"); Helper.setto ("[email protected]"); Helper.setsubject ("Living life"); Helper.settext ("Life is not only the drifting!!!" "); Add Attachment 1 Classpathresource file1 = new Classpathresource ("/cn/bdqn/attachfil Es/test.doc "); Helper.addattachment (File1.getfilename (), File1.getfile ()); Add Attachment 2: The file name of the attachment is Chinese, you need to encode and convert the file name to solve the garbled problem classpathresource file2 = new Classpathresource ( "/cn/bdqn/attachfiles/attachment test file. doc"); Helper.addattachmENT (Mimeutility.encodeword (File2.getfilename ()), File2.getfile ()); Mailsender.send (MimeMessage); }}
Test class:
public class MailTest {public static void Main (string[] args) { ApplicationContext context = new CLASSPATHXMLAPPL Icationcontext ("Applicationcontext.xml"); /* Test messages with attachments */ try{ mailwithattachment Mailwithattach = (mailwithattachment) Context.getbean (" Mailwithattachment "); Mailwithattach.send (); } catch (Exception e) { System.out.print (e.tostring ());}}}
Applicationcontext.xml:
When external pressure increases, internal dynamics should be enhanced.
---warned yourself
JavaMail and James ' Secret Garden