python实现邮件发送


嘿嘿,看了java mail实现邮件发送,虽然有会用了但是有两个小小的问题还不清楚。

尝试用python实现一下邮件发现。

首先看到的是这篇文章。http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html

就是简单发送的代码。试了一下果然可以。呼呼~~~


然后具体看一下这个模块的使用方法:

http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html


简单的text文件这样就可以:

#encoding: utf-8

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '*********@163.com'
receiver = '********@qq.com'
subject = 'Test-Python-Mail'
smtpserver = 'smtp.163.com'
username = '*****@163.com'
password = '********'
content = 'This email is sended to validate the python main kit.just for fun test,3Q for your cooperation!'

msg = MIMEText(content,'text','utf-8')
msg['Subject'] = Header(subject,'utf-8')

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print 'your email send done!'



以下是Python实现邮件发送的示例代码: ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage # 设置发送方、接收方、邮件主题和内容 sender = 'youremail@example.com' receiver = 'recipient@example.com' subject = 'Email Subject' text = 'Email Content' # 创建一个带附件的邮件实例 message = MIMEMultipart() message['From'] = sender message['To'] = receiver message['Subject'] = subject # 添加文本内容 text_content = MIMEText(text) message.attach(text_content) # 添加图片附件 with open('image.jpg', 'rb') as f: image_data = f.read() image = MIMEImage(image_data) image.add_header('Content-ID', '<image1>') message.attach(image) # 发送邮件 try: smtpObj = smtplib.SMTP('smtp.example.com', 25) smtpObj.login('username', 'password') smtpObj.sendmail(sender, receiver, message.as_string()) print('Email sent successfully') except smtplib.SMTPException: print('Error: Unable to send email') ``` 在上面的示例代码中,我们使用了Python内置的`smtplib`模块和邮件相关的`MIME`模块来实现邮件发送。首先,我们设置了发送方、接收方、邮件主题和内容。然后,我们创建了一个带附件的邮件实例,并添加了文本内容和图片附件。最后,我们使用SMTP协议发送了邮件。在实际使用中,我们需要根据具体的邮件服务器和账户信息来设置`SMTP`服务器和登录信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值