温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用JDK的JavaMailAPI发送和接收电子邮件

发布时间:2024-06-09 18:22:09 来源:亿速云 阅读:87 作者:小樊 栏目:编程语言

在使用JavaMailAPI发送和接收电子邮件之前,需要确保你已经安装了JDK,并且已经将JavaMailAPI库包含在你的项目中。以下是一个基本的示例代码,演示了如何使用JavaMailAPI发送和接收电子邮件:

  1. 发送电子邮件示例:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail {

    public static void main(String[] args) {

        final String username = "your_email@gmail.com";
        final String password = "your_password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your_email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("recipient_email@gmail.com"));
            message.setSubject("Testing JavaMailAPI");
            message.setText("Hello, this is a test email!");

            Transport.send(message);

            System.out.println("Email sent successfully!");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
  1. 接收电子邮件示例:
import java.util.Properties;
import javax.mail.*;

public class ReceiveEmail {

    public static void main(String[] args) {

        final String username = "your_email@gmail.com";
        final String password = "your_password";

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");

        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect("imap.gmail.com", username, password);

            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]);
                System.out.println("Subject: " + message.getSubject());
                System.out.println("Content: " + message.getContent());
            }

            inbox.close(false);
            store.close();

        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请确保在示例代码中替换your_email@gmail.comyour_password为你自己的邮箱地址和密码。发送电子邮件示例使用Gmail作为邮件服务器,你也可以使用其他邮件服务器,只需要相应地更改mail.smtp.hostmail.smtp.port属性。接收电子邮件示例使用IMAP协议,你可以根据需要更改为POP3协议。

以上示例代码是一个简单的示例,实际上发送和接收电子邮件可能需要更复杂的处理,比如处理附件、HTML内容等。你可以进一步研究JavaMailAPI的文档和示例代码,来了解更多高级功能。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jdk
AI