使用exchangelib发送O365邮件遇ErrorNonExistentMailbox错误求助
解决exchangelib连接O365发送邮件时的ErrorNonExistentMailbox错误
在使用Python的exchangelib库从O365用户邮箱发送邮件时,遇到以下错误:
exchangelib.errors.ErrorNonExistentMailbox: The SMTP address has no mailbox associated with it
使用的代码如下:
credentials = Credentials(username='username', password='password') my_account = Account(primary_smtp_address="my organization email address", config=Configuration( credentials=credentials, service_endpoint='https://test/EWS/Exchange.asmx', auth_type='NTLM', # server='outlook.office365.com' ), autodiscover=False, access_type=DELEGATE) m = Message(account=my_account , folder=my_account.sent , subject=e_subject , body=HTMLBody(e_body) , to_recipients=[Mailbox(email_address='email address')]) m.send_and_save()
可能的解决办法
- 修正EWS服务端点:当前代码中的
service_endpoint是无效的测试地址,O365官方的EWS端点为https://outlook.office365.com/EWS/Exchange.asmx,替换后可确保连接到正确的Exchange服务器。 - 验证收件人邮箱有效性:确认
to_recipients中的邮箱地址拼写正确,且该邮箱确实存在(内部邮箱需在O365组织内,外部邮箱需是有效的公共邮箱)。 - 检查账号与邮箱匹配性:确保
primary_smtp_address填写的是与credentials对应的有效组织邮箱,无拼写错误。 - 调整认证与权限配置:O365优先推荐OAuth2认证,若使用NTLM需确保账号拥有EWS访问权限;同时确认
access_type设置合理——若使用自身账号发件,IMPERSONATION或DELEGATE需对应正确的权限配置。 - 启用自动发现(可选):若不确定端点配置,可将
autodiscover=True并注释手动config,让exchangelib自动获取O365的服务端点,避免手动配置错误。
修改后的示例代码
from exchangelib import Credentials, Account, Message, HTMLBody, Configuration, DELEGATE # 替换为实际的账号信息 credentials = Credentials(username='your_org_username', password='your_password') config = Configuration( credentials=credentials, service_endpoint='https://outlook.office365.com/EWS/Exchange.asmx', auth_type='NTLM' ) # 替换为实际的组织邮箱地址 my_account = Account( primary_smtp_address="your_org_email@domain.com", config=config, autodiscover=False, access_type=DELEGATE ) # 替换为有效的收件人邮箱 m = Message( account=my_account, folder=my_account.sent, subject=e_subject, body=HTMLBody(e_body), to_recipients=[Mailbox(email_address='valid_recipient@domain.com')] ) m.send_and_save()
内容的提问来源于stack exchange,提问作者Endraja Dhage
相关产品推荐
相关产品推荐

