call this function first
USERNAME = "ramwin"
PASSWORD = "MYPASSWORD"
config = Configuration(
server='mail.example.com',
credentials=Credentials(username=USERNAME, password=PASSWORD),
auth_type=NTLM,
verify_ssl=False,
)
EWSTimeZone.PYTZ_TO_MS_MAP["Asia/Shanghai"] = 'China Standard Time'
# the PYTZ_TO_MS_MAP only contains few location, you should add your
# special timezone refering the MS_TIMEZONE_DEFINITIONS
TZ = EWSTimeZone.timezone("Asia/Shanghai")
account = Account(primary_smtp_address=USERNAME, config=get_config(),
access_type=DELEGATE)
# a class represent mail
mailbox = Mailbox(
name="nickname",
email_address="user@example.com",
mailbox_type="MailBox",
item_id=None)
from exchangelib.folders import EmailAddress
# a class represent email for contact
emailaddress = EmailAddress(email="user@example.com", label="EmailAddress1")
- email: an email
- label: must in {'EmailAddress1', 'EmailAddress2', 'EmailAddress3'}
from exchangelib.folders import Contact
contact = Contact(
nickname="name",
email_addresses=[emailaddress], # isinstance(emailaddress, EmailAddress)
)
account.contacts.bulk_create(items=[contact])
an event
from exchangelib.folders import CalendarItem
item = CalendarItem(
start = TZ.localize(EWSDateTime(year, month, day, hour, 30)),
end = TZ.localize(EWSDateTime(year, month, day, hour+1, 30)),
subject = "Subject",
body = "Hello from python",
location = "devnull",
categories = ['foo', 'bar'],
account = account,
folder = account.calendar,
)
item2 = CalendarItem(
start = TZ.localize(EWSDateTime(year, month, day, hour+1, 30)),
end = TZ.localize(EWSDateTime(year, month, day, hour+2, 30)),
subject = "changed changed subject",
body = "Hello from python",
location = "devnull",
categories = ['foo', 'bar'],
account = account,
folder = account.calendar,
changekey = item.changekey,
# item_id = item.item_id,
)
- start: starttime
- end: endtime
- subject
- body
- location
- save
send_meeting_invitations: 'SendToNone', 'SendOnlyToChanged', 'SendToAllAndSaveCopy'
from exchangelib.folders import Attendee
attendee = Attendee(
mailbox = mailbox,
)
email message
from exchangelib.folders import Message
m = Message(
folder=account.send, # if folder is None, you can call m.save only
account=account,
subject="Subject",
body="All bodies are beautiful",
to_recipients=[Mailbox(email_address="user1@example.com"), Mailbox(email_address="user2@example.com")],
cc_recipients=[Mailbox, Mailbox],
)
m.send_and_save()
m.save() # if folder is None
- to_recipients: [Mailbox, Mailbox]
- cc_recipients: [Mailbox, Mailbox]