java 获取 163 邮箱 已发送的邮件

要从 163 邮箱获取已发送的邮件,可以使用 JavaMail API。JavaMail API 提供了一组用于读取、撰写和发送电子邮件的类和接口。对于 163 邮箱这样的 IMAP 邮箱,可以使用 IMAP 协议来连接并读取邮件。

下面是一个示例代码,展示了如何通过 JavaMail API 连接到 163 邮箱并获取已发送邮件的列表。

添加依赖

如果你使用的是 Maven 项目,确保在 pom.xml 中添加 JavaMail 依赖:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
示例代码
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-password";      // Replace with your 163 email password

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // Open the Sent folder
            Folder sentFolder = store.getFolder("Sent Messages");
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // Fetch the messages from the Sent folder
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码说明

  1. 设置邮件服务器属性

    • 使用 Properties 类设置连接到 163 邮箱的 IMAP 服务器所需的属性。
  2. 连接到邮件服务器

    • 使用 Session.getDefaultInstance 方法创建一个邮件会话。
    • 使用 Store 类的 connect 方法连接到 IMAP 服务器。
  3. 打开“已发送”文件夹

    • 使用 store.getFolder 方法获取“已发送”文件夹。
    • 使用 Folder 类的 open 方法以只读模式打开该文件夹。
  4. 获取邮件

    • 使用 Folder 类的 getMessages 方法获取“已发送”文件夹中的所有邮件。
  5. 处理邮件

    • 循环遍历邮件并打印出每封邮件的主题、发件人、发送日期和内容。
  6. 关闭资源

    • 使用 Folder 类的 close 方法关闭文件夹。
    • 使用 Store 类的 close 方法断开与邮件服务器的连接。

注意事项

  • 安全性:不要在代码中硬编码电子邮件密码。可以使用环境变量或其他安全方式存储和访问密码。
  • 邮箱设置:确保 163 邮箱的 IMAP 功能已启用,并且允许第三方应用程序访问。
  • 异常处理:示例代码中简单地打印异常堆栈跟踪。在实际应用中,应该进行更全面的异常处理和日志记录。

通过以上代码,你可以从 163 邮箱中获取已发送邮件。根据具体需求,你可以进一步处理或显示这些邮件信息。

LOGIN Login error or password error at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732) 报了这个错误,但我密码确实是正确的,是否需要其他额外支持

确保从 Java 程序连接到 163 邮箱时没有遇到密码或安全性方面的问题,你可以尝试以下几个步骤:

  1. 开启 IMAP/SMTP 服务

    • 登录到你的 163 邮箱账户。
    • 进入设置,找到并开启 IMAP/SMTP 服务。
    • 部分邮箱可能需要开通“授权码”才能让第三方应用访问,而不是直接使用邮箱密码。
  2. 生成授权码

    • 如果 163 邮箱要求使用授权码而不是密码,你需要生成一个授权码。
    • 使用授权码而不是邮箱密码进行登录。
  3. 调整邮件服务器配置

    • 确保你使用的是正确的 IMAP 服务器地址和端口。对于 163 邮箱,通常是 imap.163.com 和端口 993
  4. 测试连接

    • 使用其他邮件客户端(如 Thunderbird、Outlook)配置相同的邮箱设置,确保 IMAP 服务和密码(或授权码)正确无误。

以下是调整后的代码示例,确保正确处理登录授权问题:

示例代码
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // Open the Sent folder
            Folder sentFolder = store.getFolder("Sent Messages");
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // Fetch the messages from the Sent folder
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

详细步骤

  1. 开启 IMAP/SMTP 服务

    • 登录到 163 邮箱。
    • 进入“设置” > “邮箱安全” > “客户端授权密码”,开启并设置授权密码。
  2. 生成授权码

    • 登录邮箱,进入“设置” > “安全设置”。
    • 生成并记录授权码,用它来代替邮箱密码。
  3. 测试连接

    • 配置其他邮件客户端,确保使用相同的 IMAP 服务器、端口和授权码来测试连接。
    • 确认第三方应用是否可以正确连接和访问邮件。

代码调整

  • 替换 usernamepassword 变量中的值,确保使用授权码而不是邮箱密码。
  • 运行程序,检查是否可以成功连接和获取邮件。

通过上述调整,你应该能够使用 JavaMail API 成功连接到 163 邮箱并读取已发送的邮件。

No sent folder found. 报错信息 是上面的

"Sent Messages" 文件夹的名称在不同的邮件服务中可能有所不同。例如,它在 163 邮箱中可能被称为 "Sent" 或者是中文名称 "已发送邮件"。我们需要列出所有文件夹的名称,以便找到正确的已发送邮件文件夹。

首先,修改代码以列出邮箱中的所有文件夹,然后找到正确的已发送邮件文件夹名称。

列出所有文件夹的代码

import javax.mail.*;
import java.util.Properties;

public class ListFolders {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // List all folders
            Folder[] folders = store.getDefaultFolder().list("*");
            for (Folder folder : folders) {
                System.out.println("Folder: " + folder.getName());
            }

            // Close the store
            store.close();

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

运行此代码以列出所有文件夹的名称。找到包含已发送邮件的文件夹名称后,可以修改代码以打开并读取该文件夹。

修改获取已发送邮件的代码

假设找到的文件夹名称是 "Sent" 或 "已发送邮件",将其名称更新到代码中:

import javax.mail.*;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password
        String sentFolderName = "Sent"; // Replace with the correct folder name

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // Open the Sent folder
            Folder sentFolder = store.getFolder(sentFolderName);
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // Fetch the messages from the Sent folder
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码说明

  1. 列出所有文件夹

    • 使用 store.getDefaultFolder().list("*") 获取所有文件夹的列表。
    • 打印每个文件夹的名称,找出包含已发送邮件的文件夹。
  2. 修改获取已发送邮件的代码

    • 将正确的文件夹名称替换到 String sentFolderName = "Sent"; 中。

通过上述步骤,你应该能够找到正确的已发送邮件文件夹名称并读取其中的邮件。

javax.mail.MessagingException: A5 NO EXAMINE Unsafe Login. Please contact kefu@188.com for help; 报错如下,

这个错误表明由于安全原因,邮件服务器拒绝了登录请求。163 邮箱可能需要你进行额外的设置才能允许第三方应用程序访问邮箱。

要解决这个问题,请按照以下步骤进行操作:

步骤 1:开启 IMAP/SMTP 服务

  1. 登录到 163 邮箱

    • 打开 163 邮箱 并使用你的邮箱账户和密码登录。
  2. 进入设置

    • 点击邮箱页面右上角的“设置”或齿轮图标,进入设置页面。
  3. 开启 IMAP/SMTP 服务

    • 找到“POP3/SMTP/IMAP”设置,确保 IMAP 和 SMTP 服务已开启。
  4. 生成授权码

    • 在安全设置中生成一个授权码,记录下来。这个授权码将在程序中代替邮箱密码使用。

步骤 2:使用授权码进行登录

确保在代码中使用生成的授权码而不是邮箱密码进行登录。

步骤 3:修改代码

使用正确的文件夹名称并确保使用授权码进行连接。

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password
        String sentFolderName = "Sent Messages"; // Replace with the correct folder name

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // Open the Sent folder
            Folder sentFolder = store.getFolder(sentFolderName);
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // Fetch the messages from the Sent folder
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码说明

  1. 设置邮箱账户和授权码

    • username 替换为你的 163 邮箱地址。
    • password 替换为你生成的授权码。
  2. 连接到 IMAP 服务器

    • 使用 IMAP 服务器地址 imap.163.com 和端口 993
  3. 获取已发送邮件文件夹

    • 确保文件夹名称正确。如果 "Sent Messages" 不存在,运行之前列出所有文件夹的代码以找到正确的文件夹名称。

通过这些步骤,你应该能够解决由于安全原因引起的登录问题,并成功访问 163 邮箱的已发送邮件。

使用POP3 方式能不能获取 已发送的邮件,

POP3 协议通常只支持访问收件箱中的邮件,不能直接访问其他文件夹(例如已发送邮件)。要访问已发送邮件或其他文件夹,建议使用 IMAP 协议,因为它提供了对所有文件夹的访问。

但是,如果必须使用 POP3 协议,可以考虑以下解决方案:

  1. 使用 IMAP 协议

    • IMAP 是专为多文件夹和同步设计的,能够访问所有文件夹,包括已发送邮件。
  2. 使用邮件客户端的转发功能

    • 如果邮件服务器不支持 POP3 访问已发送邮件,可以设置邮件客户端将发送的邮件自动转发到收件箱,然后通过 POP3 访问这些邮件。

IMAP 方式访问已发送邮件

使用 IMAP 协议来访问 163 邮箱的已发送邮件的示例代码如下:

添加依赖

确保在 pom.xml 中添加 JavaMail 依赖:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
示例代码
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password
        String sentFolderName = "Sent Messages"; // Replace with the correct folder name

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.starttls.enable", "true");
        properties.put("mail.imap.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the IMAP store object and connect with the email server
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // List all folders
            Folder[] folders = store.getDefaultFolder().list("*");
            for (Folder folder : folders) {
                System.out.println("Folder: " + folder.getName());
            }

            // Open the Sent folder
            Folder sentFolder = store.getFolder(sentFolderName);
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // Fetch the messages from the Sent folder
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用 POP3 访问收件箱邮件

如果必须使用 POP3 协议,可以通过以下代码访问收件箱中的邮件:

POP3 示例代码
import javax.mail.*;
import java.util.Properties;

public class FetchInboxEmailsUsingPOP3 {

    public static void main(String[] args) {
        String host = "pop.163.com";
        String username = "your-email@163.com"; // Replace with your 163 email
        String password = "your-authorization-code"; // Use authorization code instead of password

        // Set up properties for the connection
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "pop3");
        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", "995");
        properties.put("mail.pop3.starttls.enable", "true");
        properties.put("mail.pop3.ssl.enable", "true");

        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // Create the POP3 store object and connect with the email server
            Store store = emailSession.getStore("pop3");
            store.connect(host, username, password);

            // Open the INBOX folder
            Folder inbox = store.getFolder("INBOX");
            if (!inbox.exists()) {
                System.out.println("No inbox folder found.");
                return;
            }
            inbox.open(Folder.READ_ONLY);

            // Fetch the messages from the INBOX folder
            Message[] messages = inbox.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // Loop through the messages
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Received Date: " + message.getReceivedDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // Close the store and folder objects
            inbox.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

关键点

  1. IMAP 是最佳选择,因为它支持多文件夹访问,包括已发送邮件。
  2. POP3 仅支持访问收件箱邮件。如果需要访问已发送邮件,需使用 IMAP。
  3. 授权码 代替密码,确保邮件服务安全访问。
  4. 正确的文件夹名称:使用列出所有文件夹的代码,确保使用正确的文件夹名称。

通过上述方法,你可以正确配置和访问 163 邮箱的邮件内容。

imap 协议的话,163邮箱 需要 授权码么,163 我看到 并不需要授权码 似的 

确实,163 邮箱在某些情况下可以直接使用密码进行登录,但出于安全考虑,通常建议使用授权码。以下是使用 IMAP 协议访问 163 邮箱并获取已发送邮件的完整示例代码,无需授权码,只使用邮箱账号和密码。

使用 IMAP 协议访问 163 邮箱的已发送邮件

确保你已经在 163 邮箱设置中开启了 IMAP 服务。

添加依赖

确保在 pom.xml 中添加 JavaMail 依赖:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
示例代码
import javax.mail.*;
import java.util.Properties;

public class FetchSentEmails {

    public static void main(String[] args) {
        String host = "imap.163.com";
        String username = "your-email@163.com"; // 替换为你的163邮箱
        String password = "your-password"; // 替换为你的邮箱密码
        String sentFolderName = "已发送"; // 替换为正确的已发送邮件文件夹名称

        // 设置连接属性
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.ssl.enable", "true");

        // 创建邮件会话
        Session emailSession = Session.getDefaultInstance(properties);

        try {
            // 创建IMAP存储对象并连接到邮件服务器
            Store store = emailSession.getStore("imap");
            store.connect(host, username, password);

            // 列出所有文件夹以确保找到正确的文件夹名称
            Folder[] folders = store.getDefaultFolder().list("*");
            for (Folder folder : folders) {
                System.out.println("Folder: " + folder.getName());
            }

            // 打开已发送邮件文件夹
            Folder sentFolder = store.getFolder(sentFolderName);
            if (!sentFolder.exists()) {
                System.out.println("No sent folder found.");
                return;
            }
            sentFolder.open(Folder.READ_ONLY);

            // 获取已发送邮件文件夹中的邮件
            Message[] messages = sentFolder.getMessages();
            System.out.println("Number of messages: " + messages.length);

            // 循环处理邮件
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("Email #" + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Sent Date: " + message.getSentDate());
                System.out.println("Content: " + message.getContent());
                System.out.println();
            }

            // 关闭文件夹和存储对象
            sentFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码说明

  1. 连接属性设置

    • 使用 imap.163.com 作为 IMAP 服务器地址,端口号为 993
    • 启用 SSL 以确保连接安全。
  2. 列出所有文件夹

    • 通过 store.getDefaultFolder().list("*") 列出所有文件夹的名称,以确保找到正确的已发送邮件文件夹名称。
  3. 获取已发送邮件

    • 确保文件夹名称正确。如果你的 163 邮箱中已发送邮件文件夹名称不是 "已发送",请使用列出的文件夹名称来替换 sentFolderName

重要注意事项

  • 确保 IMAP 服务已开启:在 163 邮箱设置中确认 IMAP 服务已开启。
  • 安全访问:尽管使用邮箱密码进行连接,但出于安全考虑,建议使用授权码(如果提供)来替代密码。
  • 正确的文件夹名称:通过列出所有文件夹找到正确的已发送邮件文件夹名称。

通过这些步骤,你应该能够成功访问 163 邮箱的已发送邮件。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java发送电子邮件,您需要使用JavaMail API。以下是使用JavaMail API和163邮箱发送电子邮件的示例代码: ```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 SendEmail { public static void main(String[] args) { String to = "recipient@example.com"; // 收件人电子邮件 String from = "your_email@163.com"; // 发件人电子邮件 String password = "your_email_password"; // 发件人电子邮件密码 // 配置SMTP服务器属性 Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.163.com"); properties.put("mail.smtp.port", "25"); properties.put("mail.smtp.auth", "true"); // 获取会话对象并进行身份验证 Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(from, password); } }); try { // 创建MimeMessage对象 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("Test Email"); // 设置邮件正文 message.setText("Hello, this is a test email from Java."); // 发送电子邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 请注意,您需要将您的电子邮件地址和密码替换为示例代码中的“your_email@163.com”和“your_email_password”。另外,请确保在使用此代码之前已经在您的帐户中启用了SMTP服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值