1. Related concepts of email
Email Agreement. Mainly including:
SMTP protocol: Simple Mail Transfer Protocol, which is the simple mail transfer protocol, is used to send emails
POP3 protocol: Post Office Protocol 3, the third version of the Post Office protocol, is used to receive mail
IMAP protocol: Internet Message Access Protocol, which is an alternative protocol for POP3.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Build a James mail server
James is an open source project of Apache, pure Java implementation
Build a James server
① Download apache-james-2.3.2.zip to decompress
② Run run.bat in the bin directory to start the server [Telnet localhost 4555]
③ Configure the server through apps/james/SAR-INF/config.xml
Note: Let’s go to the bin and run first. If it is not a Chinese directory, you must then open the Telnet client on the control panel.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Install OutLook [mail client]
Product Key: PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
Create a user account
1. Use telnet to connect to James' Remote Administration Tool
2. Log in as an administrator
3. Use the adduser command to add users
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. Configure the outlook mail client
For easy viewing, you can configure the Microsoft Outlook mail client to ensure that the James mail server is in the startup state and start Microsoft Outlook.
Select Tools -> Options to open the Options panel. Select "Mail Settings" and click "Email Account" to open the "Account Settings" panel. Create a new email account under the Email tab
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. Case [Build a James mail server]
Requirement description:
Build the James mail server on this machine and customize the name of the server.
Create two test users.
Configure one of the test users in Microsoft Outlook as an Outlook mail account
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6. Use JavaMail to send emails (case)
need:
Using JavaMail technology, we can send an email from Account A to Account B with the title "Conference Notice" and the email content is "Hello XX! Please go to B01 conference room on time at 16:00 tomorrow afternoon to hold a technical discussion meeting." Check whether the email sent by the mail program is successfully sent through the Outlook client.
Key Code:
Create an EmailAuthenticator class and inherit it from Authenticator, and implant the username and password
Create the Mail class to set the email information:
public class Mail { private String mailServer, from,to,mailSubject,mailContent; private String username,password; public Mail(){ //Set email information//Username for authentication login username="[email protected]"; //Authentication password password="hq"; //The mail server corresponding to the authenticated mailbox mailServer="192.168.17.176"; //Sender information from="wj"; //Recipient information to="[email protected]"; //Email title mailSubject="We are all good kids 333"; //Email content mailContent="This is a test email! If there is any similarity, it is purely impossible"; } //Set the mail 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"); //Prop.put("mail.smtp.port", "25"); //Session service EmailAuthenticator mailauth=new EmailAuthenticator(username, password); Session mailSession=Session.getInstance(prop,(Authenticator)mailauth); try { //Encapsulate Message object Message message=new MimeMessage(mailSession); message.setFrom(new InternetAddress(from)); //Sender message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //Recipient message.setSubject(mailSubject); //Set content (set character set to handle garbled problems) message.setContent(mailContent,"text/html;charset=gbk"); message.setSentDate(new Date()); //Create a Transport instance and send an 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 Attachment
public class MailWithAttachment { private JavaMailSender mailSender; //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("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("Hahaha"); helper.setText("Smile every day, be happy!!!"); //Add attachment 1 ClassPathResource file1 = new ClassPathResource( "/cn/bdqn/attachfiles/test.doc"); helper.addAttachment(file1.getFilename(), file1.getFile()); //Add attachment 2: When the attachment's file name is Chinese, the file name needs to be encoded and converted to solve the problem of garbled ClassPathResource file2 = new ClassPathResource( "/cn/bdqn/attachfiles/attachfiles.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 mail with attachment*/ try{ MailWithAttachment mailWithAttachment = (MailWithAttachment)context.getBean("mailWithAttachment"); mailWithAttach.send(); }catch(Exception e){ System.out.print(e.toString()); } }}applicationContext.xml: Large configuration
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.