admin 管理员组

文章数量: 1184232

最近在做一个ai网站,需要给网站加一个客服邮箱,不想使用个人邮箱,就想着使用域名邮箱。我是觉得域名邮箱更便宜,企业邮箱太臃肿了。这篇文章记录了注册域名邮箱和使用SMTP和IMAP的代码。

要注册工作或者个人用的域名邮箱,需要以下步骤:

1. 访问邮箱官网

  • 域名邮箱的服务商有很多,以烽火邮箱举例子,我使用这个服务商,是因为他们可以按月付款,如果不想要了可以随时注销。打开浏览器,访问 fenghuomail。

2. 注册账号

  • 点击“注册”或“创建账号”按钮。

  • 填写必要信息,如用户名、密码、手机号等。

3. 选择域名

  • 在注册过程中,选择或输入你自己的域名(如@fenghuomail)。

4. 验证信息

  • 完成信息填写后,系统会发送验证码到你的手机或备用邮箱,输入验证码以验证身份。

5. 设置邮箱

  • 验证成功后,设置邮箱别名、签名等个性化选项。

6. 完成注册

  • 确认所有信息无误后,提交注册申请,等待系统确认。

7. 登录使用

  • 注册完成后,使用你的邮箱地址和密码登录,开始使用烽火邮箱。

注意事项:

  • 域名可用性:确保你选择的域名未被占用。

  • 密码安全:设置强密码并妥善保管。

  • 备用邮箱/手机:确保备用联系方式有效,以便找回密码或接收重要通知。

如有问题,可联系邮箱的客服支持。

使用SMTP发送邮件的python代码

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 邮件配置
smtp_server = "smtp.example"  # SMTP服务器地址(例如:smtp.gmail)
smtp_port = 587  # SMTP端口(通常是587或465)
sender_email = "your_email@example"  # 发件人邮箱
sender_password = "your_password"  # 发件人邮箱密码或授权码
receiver_email = "receiver_email@example"  # 收件人邮箱

# 创建邮件内容
subject = "这是一封测试邮件"  # 邮件主题
body = """
你好,

这是一封通过Python发送的测试邮件。

祝好!
"""  # 邮件正文

# 创建MIMEMultipart对象
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject

# 添加邮件正文
message.attach(MIMEText(body, "plain"))

# 发送邮件
try:
    # 连接到SMTP服务器
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()  # 启用TLS加密
        server.login(sender_email, sender_password)  # 登录邮箱
        server.sendmail(sender_email, receiver_email, message.as_string())  # 发送邮件
    print("邮件发送成功!")
except Exception as e:
    print(f"邮件发送失败: {e}")

使用IMAP收取邮件的python代码

import imaplib
import email
from email.header import decode_header

# IMAP配置
imap_server = "imap.example"  # IMAP服务器地址(例如:imap.gmail)
username = "your_email@example"  # 邮箱地址
password = "your_password"  # 邮箱密码或授权码

# 连接到IMAP服务器
try:
    # 创建IMAP4对象
    mail = imaplib.IMAP4_SSL(imap_server)
    mail.login(username, password)  # 登录邮箱
    print("登录成功!")

    # 选择邮箱文件夹(默认是收件箱)
    mail.select("INBOX")

    # 搜索邮件
    status, messages = mail.search(None, "ALL")  # 搜索所有邮件
    if status != "OK":
        print("搜索邮件失败")
        exit()

    # 获取邮件ID列表
    mail_ids = messages[0].split()  # 邮件ID列表
    print(f"共找到 {len(mail_ids)} 封邮件")

    # 遍历邮件
    for mail_id in mail_ids:
        # 获取邮件内容
        status, msg_data = mail.fetch(mail_id, "(RFC822)")  # 获取原始邮件内容
        if status != "OK":
            print(f"获取邮件 {mail_id} 失败")
            continue

        # 解析邮件内容
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_bytes(response_part[1])  # 将字节数据解析为邮件对象

                # 解析邮件头
                subject, encoding = decode_header(msg["Subject"])[0]  # 解码主题
                if isinstance(subject, bytes):
                    subject = subject.decode(encoding if encoding else "utf-8")

                from_, encoding = decode_header(msg.get("From"))[0]  # 解码发件人
                if isinstance(from_, bytes):
                    from_ = from_.decode(encoding if encoding else "utf-8")

                date = msg.get("Date")  # 获取邮件日期

                print(f"\n邮件ID: {mail_id}")
                print(f"主题: {subject}")
                print(f"发件人: {from_}")
                print(f"日期: {date}")

                # 解析邮件正文
                if msg.is_multipart():
                    for part in msg.walk():
                        content_type = part.get_content_type()
                        content_disposition = str(part.get("Content-Disposition"))
                        try:
                            body = part.get_payload(decode=True).decode()
                        except:
                            pass
                        if content_type == "text/plain" and "attachment" not in content_disposition:
                            print("正文:")
                            print(body)
                else:
                    body = msg.get_payload(decode=True).decode()
                    print("正文:")
                    print(body)

    # 关闭连接
    mail.logout()
    print("已退出邮箱")

except Exception as e:
    print(f"发生错误: {e}")

本文标签: 收发邮件 流程 邮箱 代码 域名