imap java代码,JAVA运用IMAP、POP3、SMTP协议收发邮件
时间: 2023-08-08 07:05:54 浏览: 130
使用SMTP 和POP3 协议实现收发邮件
4星 · 用户满意度95%
以下是使用Java Mail API实现IMAP、POP3、SMTP协议收发邮件的示例代码:
1. IMAP收取邮件
```java
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class IMAPMailReceiver {
public static void main(String[] args) throws Exception {
String host = "imap.host.com";
String user = "user@host.com";
String password = "password";
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", host);
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props);
Store store = session.getStore();
store.connect(user, password);
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_ONLY);
Message[] messages = inboxFolder.getMessages();
for (Message message : messages) {
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Sent Date: " + message.getSentDate());
System.out.println("Message: " + message.getContent().toString());
}
inboxFolder.close(false);
store.close();
}
}
```
2. POP3收取邮件
```java
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class POP3MailReceiver {
public static void main(String[] args) throws Exception {
String host = "pop3.host.com";
String user = "user@host.com";
String password = "password";
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3");
props.setProperty("mail.pop3.host", host);
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props);
Store store = session.getStore();
store.connect(user, password);
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_ONLY);
Message[] messages = inboxFolder.getMessages();
for (Message message : messages) {
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Sent Date: " + message.getSentDate());
System.out.println("Message: " + message.getContent().toString());
}
inboxFolder.close(false);
store.close();
}
}
```
3. SMTP发送邮件
```java
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SMTPMailSender {
public static void main(String[] args) throws Exception {
String host = "smtp.host.com";
String user = "user@host.com";
String password = "password";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@host.com"));
message.setSubject("Test Email");
message.setText("This is a test email");
Transport transport = session.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
```
以上示例代码仅供参考,实际使用时需要根据实际情况进行调整。
阅读全文