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:
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:
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!