方便起见,用代码说话(只是最简单的版本,各种定制需求可以参看rfc,不过很多效果需要客户端的支持,支持的比较好的是outlook)
# encoding: utf-8 import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText import os,datetime def send_email(**kwargs): """ method event dtstart due msg dtstamp """ ical = """ BEGIN:VCALENDAR\r\n PRODID:pyICSParser\r\n METHOD:%(method)s\r\n VERSION:2.0\r\n BEGIN:%(event)s\r\n DTSTART:%(dtstart)s\r\n DUE:%(due)s\r\n PRIORITY:1\r\n SUMMARY:%(msg)s\r\n UID:[email protected]\r\n SEQUENCE:0\r\n DTSTAMP:%(dtstamp)s\r\n STATUS:%(status)s\r\n END:VEVENT\r\n END:VCALENDAR\r\n """% kwargs eml_body = u"%(msg)s" % kwargs msg = MIMEMultipart('mixed') msg['Date'] = formatdate(localtime=True) msg['Subject'] = u"%(msg)s" % kwargs msg['To'] = ",".join(attendees) part_email = MIMEText(eml_body,"html",'utf-8') part_cal = MIMEText(ical,'calendar;method=%(method)s'%kwargs,'utf-8') msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) ical_atch = MIMEBase('application/ics',' ;name="%s"'%("invite.ics")) ical_atch.set_payload(ical) Encoders.encode_base64(ical_atch) ical_atch.add_header('Content-Disposition', 'attachment; filename="%s"'%("invite.ics")) eml_atch = MIMEBase('text/plain','') Encoders.encode_base64(eml_atch) eml_atch.add_header('Content-Transfer-Encoding', "") msgAlternative.attach(part_email) msgAlternative.attach(part_cal) s = smtplib.SMTP() s.connect('xxxx') s.login(' ', ' ') s.sendmail('[email protected]', attendees, msg.as_string()) s.close() attendees =['[email protected]'] fro = "" ddtstart = datetime.datetime.utcnow() dur = datetime.timedelta(minutes= 5) dtend = ddtstart + dur dtend = dtend.strftime("%Y%m%dT%H%M%SZ") send_email(method='PUBLISH',event='VTODO',dtstart=dtend,due=dtend,msg='publish 任务',dtstamp=dtend, status="COMPLETED") send_email(method='REQUEST',event='VTODO',dtstart=dtend,due=dtend,msg='request 任务',dtstamp=dtend, status="NEEDS ACTION") send_email(method='PUBLISH',event='VEVENT',dtstart=dtend,due=dtend,msg='publish 日历',dtstamp=dtend, status="CONFIRMED") send_email(method='REQUEST',event='VEVENT',dtstart=dtend,due=dtend,msg='request 日历',dtstamp=dtend, status='CONFIRMED')