The Secret Garden of JavaMail and James, javamailjames
JavaMail, as its name implies, provides developers with programming interfaces related to email processing. It is an API released by Sun to process emails. It can easily perform some common mail Transmission. We can develop applications similar to Microsoft Outlook based on JavaMail.
The core classes used to process emails in the JavaMail package are Session, Message, Address, Authenticator, Store, Transport, and Folder. Session defines a basic mail Session, which needs to read information similar to the mail server, user name, and password from Properties. However, JDK does not include the mail. jar and activtion. jar packages published by Sun are used to send emails using JavaMail.
Email protocol:
SMTP Protocol: Simple Mail Transfer Protocol, that is, Simple Mail transmission Protocol, used to send emails
POP3 Protocol: Post Office Protocol 3, the third version of the Post Office Protocol, used to receive emails
IMAP Protocol: Internet Message Access Protocol (Internet Message Access Protocol), which is an alternative Protocol for POP3.
JavaMail mail process:
1. Set up the James Mail Server
2. Install OutLook [mail client]
3. Configure the outLook mail client
4. Set up the James Mail Server
1. Set up the james Mail Server
James is an open-source project of apache, implemented in pure java.
First-mover download apache-james-2.3.2.zip (batch: http://pan.baidu.com/s/1pJoyg7h)
Run. bat in the bin directory to start the server.
Then run the cmd command[Telnet local host 4555]
Finally Configure the server through apps \ james \ SAR-INF \ config. xml (modify node)
2. Install OutLook [mail client],3. Configure the outLook mail client
Enter the product key (optional)
Product Key: PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
Create a user account.Telnet localhost 4555 to connect,
The default username and password are root.
Go to help to add an account (adduser)
Install it as required. Next, pay attention to the following:
Hosts file:
After installation is complete, perform the test:
4. Set up the James Mail Server
Finally, use JavaMail to send an email:
Requirement: The account receives the task and attachment information sent by javaMail.
Core code:
Create an EmailAuthenticator class and inherit from Authenticator, and implant the user name and password
Create Mail class (set Mail Information ):
Public class Mail {private String mailServer, from, to, mailSubject, mailContent; private String username, password; public Mail () {// set email information // username for authenticated login = "hq@mail.com"; // authentication password = "hq "; // The email server mailServer = "192.168.17.176" corresponding to the authenticated mailbox; // The sender information from = "wj"; // The Recipient Information to = "wj@mail.com "; // mail title mailSubject = "we are all good children 333"; // mail content mailContent = "this is a test email! It is impossible to set the email server @ SuppressWarnings ("static-access") public void send () {Properties prop = System. getProperties (); // specify the mail server prop. put ("mail. smtp. host ", mailServer); // whether to enable authentication prop. put ("mail. smtp. auth "," true "); // smtp protocol prop. put ("mail. smtp. port "," 25 "); // generate the Session service EmailAuthenticator mailauth = new EmailAuthenticator (username, password); Session mailSession = Session. getInstance (prop, (Authenticator) mailauth); try {// encapsulate the Message object Message = new MimeMessage (mailSession); message. setFrom (new InternetAddress (from); // The sender message. setRecipient (Message. recipientType. TO, new InternetAddress (to); // recipient message. setSubject (mailSubject); // sets the message content (sets the character set to handle garbled characters. setContent (mailContent, "text/html; charset = gbk"); message. setSentDate (new Date (); // create a Transport instance and send the email 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!"); } }
7. Send Mail with attachments
Public class MailWithAttachment {private JavaMailSender mailSender; // you must use JavaMailSender public void setMailSender (JavaMailSender mailSender) {this. mailSender = mailSender;} public void send () throws MessagingException, IOException {MimeMessage mimeMessage = mailSender. createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (mimeMessage, true, "UTF-8"); helper. setFrom ("hq@mail.com "); Helper. setTo ("wj@mail.com"); helper. setSubject ("life"); helper. setText ("life is more than just what you see !!! "); // Add Attachment 1 ClassPathResource file1 = new ClassPathResource ("/cn/bdqn/attachfiles/test.doc "); helper. addAttachment (file1.getFilename (), file1.getFile (); // Add Attachment 2: when the file name of the attachment is Chinese, encode and convert the file name, 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 ClassPathXmlApplicationContext ("applicationContext. xml ");/* test emails 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 motivation should be enhanced.
--- Warn yourself