


Java Mail Programming Legends: Develop Powerful and Efficient Email Applications
Java Mail is a Java library that allows you to develop email applications easily and conveniently. It provides a powerful set of api that allows you to send, receive and manage emails easily. Java Mail supports a variety of email protocols, including SMTP (Simple Mail Transfer Protocol), POP3 (Post Office Protocol version 3), and IMAP (Internet Message Access Protocol).
1. Send email using Java Mail
To send an email using Java Mail, you need to follow these steps:
- Create a Java Mail Session object.
- Create an email message object.
- Set the recipient, sender, subject and content of the email message.
- Send email message to SMTPserver.
The following is a demo code:
import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 设置发件人信息 String from = "your_email_address@example.com"; String passWord = "your_password"; // 设置收件人信息 String to = "recipient_email_address@example.com"; // 设置邮件主题和内容 String subject = "Hello from Java Mail!"; String content = "This is a test email sent using Java Mail."; // 创建一个Java Mail Session对象 Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); // 创建一个电子邮件消息对象 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); message.setText(content); // 将电子邮件消息发送到SMTP服务器 Transport.send(message); System.out.println("Email sent successfully!"); } }
2. Use Java Mail to receive emails
To receive emails using Java Mail, you need to follow these steps:
- Create a Java Mail Session object.
- Connect to a POP3 or IMAP server.
- Get the email message list.
- Get the details of the email message.
The following is a demo code:
import javax.mail.*; import javax.mail.internet.*; public class ReceiveEmail { public static void main(String[] args) { // 设置收件人信息 String username = "your_email_address@example.com"; String password = "your_password"; // 创建一个Java Mail Session对象 Properties props = new Properties(); props.put("mail.pop3.host", "pop3.example.com"); props.put("mail.pop3.port", "110"); props.put("mail.pop3.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); // 连接到POP3服务器 Store store = session.getStore("pop3"); store.connect(); // 获取电子邮件消息列表 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); // 获取电子邮件消息的详细信息 for (Message message : messages) { System.out.println("From: " + message.getFrom()[0].getAddress()); System.out.println("Subject: " + message.getSubject()); System.out.println("Content: " + message.getContent()); } // 关闭连接 inbox.close(); store.close(); } }
3. Use Java Mail to manage emails
Java Mail also allows you to manage email, including creating, deleting and moving emails. You can also use Java Mail to search emails.
The following is a demo code:
import javax.mail.*; import javax.mail.internet.*; public class ManageEmail { public static void main(String[] args) { // 设置收件人信息 String username = "your_email_address@example.com"; String password = "your_password"; // 创建一个Java Mail Session对象 Properties props = new Properties(); props.put("mail.imap.host", "imap.example.com"); props.put("mail.imap.port", "993"); props.put("mail.imap.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); // 连接到IMAP服务器 Store store = session.getStore("imap"); store.connect(); // 获取电子邮件消息列表 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); // 创建一个新的文件夹 Folder newFolder = inbox.createFolder("New Folder"); // 移动电子邮件消息到新的文件夹 Message[] messages = inbox.getMessages(); inbox.copyMessages(messages, newFolder); // 删除电子邮件消息 inbox.deleteMessages(messages); // 关闭连接 newFolder.close(); inbox.close(); store.close(); } }
Java Mail is a powerful library that allows you to develop email applications easily. It supports multiple email protocols and provides a rich API that allows you to easily send, receive and manage emails.
The above is the detailed content of Java Mail Programming Legends: Develop Powerful and Efficient Email Applications. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.