java利用pop3发送邮件
时间: 2023-10-11 21:17:40 浏览: 146
您可以使用JavaMail API来发送POP3邮件。以下是一个示例代码:
```java
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmailWithPOP3 {
public static void main(String [] args) {
String to = "recipient@example.com";//收件人邮箱地址
String from = "sender@example.com";//发件人邮箱地址
String host = "pop3.example.com";//POP3主机地址
Properties properties = System.getProperties();
properties.setProperty("mail.pop3.host", host);
Session session = Session.getDefaultInstance(properties);
try{
Store store = session.getStore("pop3");
store.connect(host, "<username>", "<password>");//POP3用户名和密码
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the subject line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
```
请注意,您需要替换示例代码中的收件人、发件人、主机、POP3用户名和密码,以及设置正确的主机地址和端口。
阅读全文