如果要發送gmail帳戶該如何做?

来源:6-2 邮件发送

精慕门_learner

2022-04-18 21:35:10

# coding:utf-8

import time
import smtplib  # python 自帶
import schedule  # pip install schedule

from email.mime.text import MIMEText  # python自帶
from email.header import Header
from email.mime.multipart import MIMEMultipart  # 使用MIMEMultipart 對象


# 第三方的smtp
mail_host = 'ssl://smtp.gmail.com:465'  # 設置服務器
mail_user = 'husenior11123@gmail.com'  # SMTP 帳號:[你的gmail帳號]
mail_password = 'wdycjpatwlswsqdo'  # SMTP 密碼:[google應用程式密碼]
# smtp 开通, 授权码

sender = 'husenior11123@gmail.com'
receivers = ['s1041026@gm.ncue.edu.tw']

#  MIMEText(email_content, email_type, encoding_type) -定義郵件發送內容的對象
# message =MIMEText('這是一個測試', 'plain', 'utf-8')  # 內容, 格式, 編碼格式
# message = MIMEText('<p style="color:red;">这是一个测试</p>', 'html', 'utf-8')  # 發送html 類型文件

message = MIMEMultipart()  # 實例化對象

message['From'] = Header(sender)
message['Subject'] = Header('python脚本测试', 'utf-8')  # subject -> Title
# print(message.as_string())

# 定義發送的內容 -使用附件先將內容讀取 -> 定義附件類型(流類型) -> 對附件進行名稱定義 -> 把附件附加到message內 ->
attr = MIMEText(open('send.py', 'rb').read(), 'base64', 'utf-8')
attr['Content-Type'] = 'application/octet-stream'  # 流的協議
attr['Content-Disposition'] = 'attachment;filename="send.py"'

message.attach(attr)  # 郵件附件添加完成
message.attach(MIMEText('这是一个带附件的邮件', 'plain', 'utf-8'))  # 加入郵件主內容

# 郵件發送
def send():
    try:  # 使用try-catch來捕獲發送失敗
        smtobj = smtplib.SMTP()  # 實例化協議對象
        smtobj.connect(mail_host, 465)  # SMTP 埠號:465
        smtobj.login(mail_user, mail_pass)
        smtobj.sendmail(sender, receivers, message.as_string())  # message.as_string() 加密字符串
    except smtplib.SMTPException as e:
        print('error: %s' % e)


if __name__ == '__main__':
    schedule.every(10).seconds.do(send)
    print('send start')
    while 1:  # 為何要用while loop, 及time.sleep() -> 當使用run_pending()時, 它會監測是否有任務要執行, if not, finish
        # 所以要用while loop, 使其不斷的監測是否有任務要執行
        schedule.run_pending()
        time.sleep(1)  # 因為不停地監測, 頻率太高 -> 為了減少cpu消耗, 提高性能, 需要sleep
'''
#message = MIMEText('<p style="color:red;">这是一个测试</p>', 'html', 'utf-8')

message['From'] = Header(sender)
message['Subject'] = Header('python脚本测试', 'utf-8')

'''

報錯:

Traceback (most recent call last):

  File "C:/Users/user/Desktop/python/auto/埭鎢/email/send.py", line 55, in <module>

    schedule.run_pending()

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\schedule\__init__.py", line 780, in run_pending

    default_scheduler.run_pending()

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\schedule\__init__.py", line 100, in run_pending

    self._run_job(job)

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\schedule\__init__.py", line 172, in _run_job

    ret = job.run()

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\schedule\__init__.py", line 661, in run

    ret = self.job_func()

  File "C:/Users/user/Desktop/python/auto/埭鎢/email/send.py", line 43, in send

    smtobj.connect(mail_host, 465)  # SMTP 埠號:465

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect

    self.sock = self._get_socket(host, port, self.timeout)

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 308, in _get_socket

    return socket.create_connection((host, port), timeout,

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\socket.py", line 787, in create_connection

    for res in getaddrinfo(host, port, 0, SOCK_STREAM):

  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo

    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):

socket.gaierror: [Errno 11001] getaddrinfo failed


写回答

1回答

时间,

2022-04-19

同学,你好!

1、socket.gaierror: [Errno 11001] getaddrinfo failed表示应用程序无法解析主机的IP地址。同学可以看下是否可以正常访问gmail邮箱

2、谷歌邮箱在国内是无法访问的,同学可以使用其他邮箱进行学习

祝学习愉快!

0

0 学习 · 4470 问题

查看课程