Python启蒙——检测邮箱是否有新邮件,并邮件通知(二)

承接上回,知道了新邮件返回的消息后,可以更近一步,来实现发邮件。

发邮件我们要用到SMTP的服务,来发送邮件。

发送邮件一般有收件人地址、抄送人地址、邮件主题、邮件内容等内容,从SMTPLIB这个类的解释和例子如下:

 主要发送思路是:

1. 登录邮箱,用SMTP方法,需要传输smtp服务器地址、端口等,参考方法如下

2. 输入对应地址,主题,内容

3. 发送

xi小试代码如下:

def sendMail():
    user = 'abc@gmail.com'
    customer = 'efg@gmail.com'
    password = '123456'
    M = smtplib.SMTP('smtp.gmail.com', '25')
    M.login(user,password)
    msg = 'Have a new mail'
    M.sendmail(user,customer,msg)
    print('Send the email to remind customer')
    M.quit

if __name__ == "__main__":
    print(sendMail())

小测一下

 可以看到邮件发送成功。但是邮件没有主题(Subject),还不知道中文传输会不会有问题。大家可以预测一下,小改以后会不会有问题。

import smtplib

def sendMail():
    user = 'abc@gmail.com'
    customer = 'efg@gmail.com'
    password = '123456'
    M = smtplib.SMTP('smtp.gmail.com', '25')
    M.login(user,password)
    msg = '您有新的邮件!请到abc@gmail.com查看'
    M.sendmail(user,customer,msg)
    print('Send the email to remind customer')
    M.quit

if __name__ == "__main__":
    print(sendMail())

下章揭示会不会出问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于Python的股票检测邮件通知的代码示例,仅供参考: ```python import tushare as ts import pandas as pd import smtplib from email.mime.text import MIMEText from email.header import Header # 邮箱配置信息 sender = '[email protected]' password = 'your_password' smtp_server = 'smtp.example.com' smtp_port = 25 receiver = '[email protected]' # 股票代码 stock_code = '600000' # 获取股票数据 df = ts.get_k_data(stock_code) # 计算均线和RSI指标 df['ma5'] = df['close'].rolling(window=5).mean() df['ma10'] = df['close'].rolling(window=10).mean() df['rsi'] = ta.RSI(df['close'].values) # 定义股票检测函数 def detect_stock(df): if df['close'] > df['ma5'] and df['close'] > df['ma10'] and df['rsi'] > 70: return True else: return False # 检测股票是否符合条件 if detect_stock(df): # 发送邮件通知 content = f"股票代码:{stock_code},当前价格:{df.iloc[-1]['close']}" message = MIMEText(content, 'plain', 'utf-8') message['From'] = Header(sender, 'utf-8') message['To'] = Header(receiver, 'utf-8') message['Subject'] = Header('股票检测通知', 'utf-8') try: smtpObj = smtplib.SMTP(smtp_server, smtp_port) smtpObj.login(sender, password) smtpObj.sendmail(sender, [receiver], message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("邮件发送失败") ``` 这段代码使用了tushare库获取股票数据,并使用pandas和ta-lib库计算了均线和RSI指标。在detect_stock函数中定义了符合条件的判断逻辑。当检测到符合条件的股票时,会发送一封邮件通知。需要注意的是,你需要替换代码中的邮箱配置信息和股票代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追求快乐

创作不易,支持原创

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值