The examples in this article share the specific implementation code of the email sending and receiving function for your reference. The specific content is as follows
Preparation work, environment construction:
1. Build a mail server locally
Easymail server, eyoumailserversetup.exe
2. Create a new email account
Zhang San sent an email to Li Si.
Step 1:
Create a new domain name: Tools, Server settings, enter itcast.com in the single domain name box
Step 2:
Create a new email account: [email protected]
[email protected]
3. Install foxmail
Configure the email sending server (smtp): localhost 25
Email receiving server (pop3): localhost 110
Create a new account and you can receive emails!
Notice
If it is a web project, because Javaee comes with email function, there may be problems!
We need to use our own mail.jar file function! You need to delete the mail package in javaee!
use:
For JavaMail development, first introduce jar files:
activation.jar [If you use jdk1.6 or above, you can do not use this jar file]
mail.jar [Email sending core package]
/** * 1. Send a normal email* @author Jie.Yuan * */public class App_SendMail { @Test public void testSend() throws Exception { //0. Mail parameters Properties prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // Specify the protocol prop.put("mail.smtp.host", "localhost"); // Host stmp.qq.com prop.put("mail.smtp.port", 25); // Port prop.put("mail.smtp.auth", "true"); // User password authentication prop.put("mail.debug", "true"); // Debug mode//1. Create a session of email Session session = Session.getDefaultInstance(prop); //2. Create a mail body object (whole email object) MimeMessage message = new MimeMessage(session); //3. Set the mail body parameters: //3.1 Title message.setSubject("My first email"); //3.2 Mail sending time message.setSentDate(new Date()); //3.3 Sender message.setSender(new InternetAddress("[email protected]")); //3.4 Recipient message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); //3.5 Content message.setText("Hello, it has been sent successfully! Text...."); // Simple plain text email message.saveChanges(); // Save email (optional) //4. Send Transport trans = session.getTransport(); trans.connect("zhangsan", "888"); // Send email trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}With pictures
/** * Email with image resources* @author Jie.Yuan * */public class App_2SendWithImg { // Initialization parameters private static Properties prop; // Sender private static InternetAddress sendMan = null; static { prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // Specify the protocol prop.put("mail.smtp.host", "localhost"); // Host stmp.qq.com prop.put("mail.smtp.port", 25); // Port prop.put("mail.smtp.auth", "true"); // User password authentication prop.put("mail.debug", "true"); // Debug mode try { sendMan = new InternetAddress("[email protected]"); } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. Create a mail session Session session = Session.getDefaultInstance(prop); // 2. Create a mail object MimeMessage message = new MimeMessage(session); // 3. Set parameters: title, sender, recipient, send time, content message.setSubject("Mail with picture"); message.setSender(sendMan); message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); message.setSentDate(new Date()); /************************Set email content: Multi-function user email (related)*********************/ // 4.1 Build a multi-function mail block MimeMultipart related = new MimeMultipart("related"); // 4.2 Build the multi-function mail block content = text on the left + image resource on the right MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // Set specific content: a. Resource (picture) String filePath = App_2SendWithImg.class.getResource("8.jpg").getPath(); DataSource ds = new FileDataSource(new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID("8.jpg"); // Set the resource name and reference the foreign key // Set the specific content: b. Text content.setContent("<img src='cid:8.jpg'/> OK! ", "text/html;charset=UTF-8"); related.addBodyPart(content); related.addBodyPart(resource); /*********4.3 Add the built complex emails to the emails *******/ message.setContent(related); // 5. Send Transport trans = session.getTransport(); trans.connect("zhangsan", "888"); trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}Pictures + attachments
/** * 3. Email with image resources and attachments* @author Jie.Yuan * */public class App_3ImgAndAtta { // Initialization parameters private static Properties prop; // Sender private static InternetAddress sendMan = null; static { prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // Specify the protocol prop.put("mail.smtp.host", "localhost"); // Host stmp.qq.com prop.put("mail.smtp.port", 25); // Port prop.put("mail.smtp.auth", "true"); // User password authentication prop.put("mail.debug", "true"); // Debug mode try { sendMan = new InternetAddress("[email protected]"); } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. Create a mail session Session session = Session.getDefaultInstance(prop); // 2. Create a mail object MimeMessage message = new MimeMessage(session); // 3. Set parameters: title, sender, recipient, send time, content message.setSubject("Mail with picture"); message.setSender(sendMan); message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); message.setSentDate(new Date()); /* * Mail development with attachment (picture)*/ // Construct a total mail block MimeMultipart mixed = new MimeMultipart("mixed"); // --> Total mail is fast, set to message.setContent(mixed); // Left: (Text + Image Resource) MimeBodyPart left = new MimeBodyPart(); // Right: Attachment MimeBodyPart right = new MimeBodyPart(); // Set to total mail block mixed.addBodyPart(left); mixed.addBodyPart(right); /******* Attachment**********/ String attr_path = this.getClass().getResource("a.docx").getPath(); DataSource attr_ds = new FileDataSource(new File(attr_path)); DataHandler attr_handler = new DataHandler(attr_ds); right.setDataHandler(attr_handler); right.setFileName("a.docx"); /************************ Set email content: Multifunction user email (related)**********************/ // 4.1 Build a multifunctional email block MimeMultipart related = new MimeMultipart("related"); // ---> Set to the left.setContent(related); // 4.2 Build the multi-functional mail block content = text on the left + image resource on the right MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // Set specific content: a. Resource (picture) String filePath = App_3ImgAndAtta.class.getResource("8.jpg").getPath(); DataSource ds = new FileDataSource(new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID("8.jpg"); // Set the resource name and reference the foreign key // Set the specific content: b. Text content.setContent("<img src='cid:8.jpg'/> OK! ", "text/html;charset=UTF-8"); related.addBodyPart(content); related.addBodyPart(resource); // 5. SendTransport trans = session.getTransport(); trans.connect("zhangsan", "888"); trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}The above is all about this article, I hope it will be helpful for everyone to learn Java programming.