当前位置:首页 > 服务器技术 > 正文

概述SMTP配置IMAP配置SMTP配置IMAP配置

概述

截至2026年4月,163邮箱作为中国最受欢迎的邮箱服务提供商之一,其稳定性和安全性深受用户信赖。本文旨在详细介绍如何在各种编程环境中配置和使用163邮件服务器,包括SMTP和IMAP协议的使用,以及邮件发送和接收的实战操作。本教程不仅适用于邮件客户端配置,也适用于开发者在应用程序中集成邮件功能。

SMTP配置

SMTP(Simple Mail Transfer Protocol)用于发送电子邮件。163邮箱的SMTP服务器地址为“smtp.163.com”,端口默认为25或465(SSL)或587(TLS)。以下是使用Python和Java进行邮件发送的示例代码。

Python示例

Python 3.11 示例代码:import smtplibfrom email.mime.text import MIMEText# 邮件内容msg = MIMEText('这是一封测试邮件', 'plain', 'utf-8')msg['From'] = 'your_email@163.com'msg['To'] = 'recipient@example.com'msg['Subject'] = '测试邮件'# SMTP服务器设置smtp_server = 'smtp.163.com'smtp_port = 465  # 使用SSL,可选25或587password = 'your_password'# 创建SMTP连接server = smtplib.SMTP_SSL(smtp_server, smtp_port)server.login(msg['From'], password)server.sendmail(msg['From'], msg['To'], msg.as_string())server.quit()

请注意,使用SSL连接时,默认端口为465。如果服务器要求,也可以使用端口25或587,并相应地调整代码中的smtplib.SMTP_SSLsmtplib.SMTP

Java示例

Java 17 示例代码:import javax.mail.*;import javax.mail.internet.*;import java.util.Properties;public class SendEmail {    public static void main(String[] args) {        String from = "your_email@163.com";        String to = "recipient@example.com";        String host = "smtp.163.com";        final String username = "your_email@163.com";        final String password = "your_password";        Properties properties = System.getProperties();        properties.setProperty("mail.smtp.host", host);        properties.setProperty("mail.smtp.auth", "true");        properties.setProperty("mail.smtp.port", "465"); // SSL 默认端口        properties.setProperty("mail.smtp.ssl.enable", "true");        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(username, password);            }        });        try {            Message message = new MimeMessage(session);            message.setFrom(new InternetAddress(from));            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));            message.setSubject("测试邮件");            message.setText("这是一封测试邮件");            Transport.send(message);            System.out.println("邮件发送成功!");        } catch (MessagingException e) {            e.printStackTrace();        }    }}

在Java中,使用JavaMail API进行邮件发送时,需要添加JavaMail库到项目依赖中。可以通过Maven或Gradle添加相应依赖。

IMAP配置

IMAP(Internet Message Access Protocol)用于接收电子邮件。163邮箱的IMAP服务器地址同样为“imap.163.com”,端口默认为143(非SSL)或993(SSL)。以下是使用Python和Java进行邮件接收的示例代码。

Python示例

Python 3.11 示例代码:import imaplibfrom email import message_from_stringfrom email.utils import parsedate_to_datetimefrom datetime import datetime, timedelta, timezoneimport os# IMAP服务器设置imap_server = 'imap.163.com'imap_port = 993  # 使用SSL,可选143(非SSL)或993(SSL)密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际密码)替换为实际{  \"articleContent\": \"

SMTP配置

SMTP(Simple Mail Transfer Protocol)用于发送电子邮件。以下是使用Python和Java进行邮件发送的示例代码。

Python示例

Python 3...

请注意,使用SSL连接时,默认端口为465。如果服务器要求,也可以使用端口25或587...

Java示例

Java 17 示例代码...

在Java中,使用JavaMail API进行邮件发送时,需要添加JavaMail库到项目依赖中...

IMAP配置

IMAP(Internet Message Access Protocol)用于接收电子邮件。以下是使用Python和Java进行邮件接收的示例代码。

Python示例

Python 3...
\"}