我正在做一个python的商业项目,我的团队会在一些团队中发送自动报告。发送它的代码运行良好:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment = "Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
感谢这个线程: 通过 Python 发送 Outlook 电子邮件?
无论如何都可以更改发件人的地址,例如:
mail.From = 'Team Adress'
否则问题将是人们会回复邮件到我的电子邮件地址,这将是错误的方法。或者这根本不可能,因为它必须打开我的 Outlook 帐户?
有两种可能的方法可以指定发件人:
- 如果在 Outlook 中配置了多个帐户,则可以使用 MailItem.SendUsingAccount 属性,该属性允许设置一个
Account
对象,该对象表示要发送MailItem
的帐户。例如:
public static void SendEmailFromAccount(Outlook.Application application, string subject, string body, string to, string smtpAddress)
{
// Create a new MailItem and set the To, Subject, and Body properties.
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
newMail.To = to;
newMail.Subject = subject;
newMail.Body = body;
// Retrieve the account that has the specific SMTP address.
Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
// Use this account to send the email.
newMail.SendUsingAccount = account;
newMail.Send();
}
public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
{
// Loop over the Accounts collection of the current Outlook session.
Outlook.Accounts accounts = application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the email address matches, return the account.
if (account.SmtpAddress == smtpAddress)
{
return account;
}
}
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
- 属性允许设置一个字符串,该字符串指示邮件的预期发件人的显示名称。请注意,您需要获得许可才能代表他人发送任何内容。