如何向此电子邮件代码添加多个收件人?

时间:2016-12-27 11:03:06

标签: python-3.x email smtp mime-types smtplib

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

fromaddr = "YOUR EMAIL"
toaddr = ["FISRT EMAIL ADDRESS YOU SEND TO", "SECOND EMAIL ADDRESS YOU SEND TO"]

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"

body = "TEXT YOU WANT TO SEND"

msg.attach(MIMEText(body, 'plain'))

filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")

这是我得到一个错误的说法,无法编码列表元素     text = msg.as_string()     server.sendmail(fromaddr,toaddr,text)     server.quit()

1 个答案:

答案 0 :(得分:0)

如果要在电子邮件中添加多个收件人,则应从数据库中获取用户并添加到循环中。

导入System.Net;

导入System.Net.Mail;

  1. 根据您的情况获取用户数据。  enter code here var data = dataContext.website_request_logs.Where(" condition"); //我们使用datacontext从website_request_logs表中获取数据。

  2. foreach (website_request_log item in data) string EmailTo = item.email; string EmailFrom = item.fromEmail; string EmailSubject = item.emailSubject;

  3. EmailUser(EmailTo ,EmailFrom ,EmailSubject );// call the function and send parameter in this.

  4. 4 .//我们可以使用电子邮件助手类发送电子邮件。

    `private void EmailUser             (                                字符串EmailTo,                                字符串EmailFrom,                                字符串EmailSubject              )         {

             EmailHelper.SendEmail
                        (EmailFrom,
                        EmailTo,
                       EmailSubject,
                       );
    
    
    
        }`
    
    1. SendEmail(){MailMessage msg = new MailMessage(); msg.From = new MailAddress(fromEmail,fromName);

      mail.Send(MSG);

    2. }