{"id":2002,"date":"2026-04-14T15:36:46","date_gmt":"2026-04-14T07:36:46","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2002"},"modified":"2026-03-28T04:41:27","modified_gmt":"2026-03-28T04:41:27","slug":"how-to-use-applicationmaillogon-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationmaillogon-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.MailLogon in the xlwings API way"},"content":{"rendered":"\n<p>The MailLogon member of the Application object in Excel&#8217;s object model is a method that allows you to log on to a MAPI mail system programmatically. This functionality is useful when you need to automate email-related tasks, such as sending workbooks or reports directly from Excel via the default email client. In xlwings, you can access this method through the Application object, enabling you to integrate email operations into your Python scripts for enhanced automation and productivity. Note that this method relies on the underlying MAPI (Messaging Application Programming Interface) system, so it requires a compatible email client like Microsoft Outlook to be installed and configured on the machine.<\/p>\n\n\n\n<p>The syntax for calling the MailLogon method via xlwings is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.MailLogon(Name, Password, DownloadNewMail)<\/code><\/pre>\n\n\n\n<p>Here, <code>app<\/code> refers to the xlwings Application object, typically obtained using <code>xw.App()<\/code> or <code>xw.apps.active<\/code>. The parameters are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Name<\/strong> (optional, Variant): A string specifying the mail profile name. If omitted, the default profile is used.<\/li>\n\n\n\n<li><strong>Password<\/strong> (optional, Variant): A string for the mail password. If omitted, the stored password is used if available.<\/li>\n\n\n\n<li><strong>DownloadNewMail<\/strong> (optional, Variant): A Boolean value that determines whether new mail is downloaded upon logon. Set to <code>True<\/code> to download new mail, or <code>False<\/code> otherwise. The default is <code>True<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The parameters can be passed by position or as keyword arguments. For example, to log on with a specific profile and download new mail, you might use <code>app.api.MailLogon(\"MyProfile\", \"mypassword\", True)<\/code>. It&#8217;s important to handle security and password storage carefully in scripts to avoid exposing sensitive information.<\/p>\n\n\n\n<p>Below is a code example demonstrating the use of MailLogon with xlwings. This example assumes Excel and a MAPI-compliant email client are running, and it logs on to a mail profile before performing a simple email-related task, such as sending the active workbook. Note that in practice, you should ensure proper error handling and user authentication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start or connect to an Excel application\napp = xw.App(visible=True) # Set visible=False for background operation\n\n# Open a workbook (optional, for context)\nwb = app.books.open('example.xlsx')\n\n# Log on to the mail system using MailLogon\ntry:\n# Using default profile and downloading new mail\n    app.api.MailLogon(DownloadNewMail=True)\n    print(\"Logged on to mail system successfully.\")\n\n    # Example: Send the active workbook as an email attachment\n    # This uses the Mailer object via the Workbook's Mailer property (deprecated in newer Excel versions)\n    # For modern email automation, consider using Outlook's COM object or other libraries\n    wb.api.Mailer.Send()\n    print(\"Workbook sent via email.\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n# Clean up: Log off from mail and close Excel\n    app.api.MailLogoff()\n    wb.close()\n    app.quit()<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MailLogon member of the Application object in Excel&apos;s object model is a method that allows you t&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2002","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2002"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2002\/revisions"}],"predecessor-version":[{"id":3053,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2002\/revisions\/3053"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}