<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vipul Bajaj on Medium]]></title>
        <description><![CDATA[Stories by Vipul Bajaj on Medium]]></description>
        <link>https://medium.com/@vipulbajaj?source=rss-18bb3c305db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JBw2fo-cFg6NT3ZSpE7FzQ.jpeg</url>
            <title>Stories by Vipul Bajaj on Medium</title>
            <link>https://medium.com/@vipulbajaj?source=rss-18bb3c305db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 07:53:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vipulbajaj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to send emails using Python and Office365 SMTP server]]></title>
            <link>https://medium.com/@vipulbajaj/how-to-send-emails-using-python-and-office365-smtp-server-f5f2dab3432?source=rss-18bb3c305db------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5f2dab3432</guid>
            <category><![CDATA[smtp]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[email]]></category>
            <dc:creator><![CDATA[Vipul Bajaj]]></dc:creator>
            <pubDate>Sat, 15 Jan 2022 15:34:21 GMT</pubDate>
            <atom:updated>2022-01-15T15:53:24.960Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we shall learn how to use <strong><em>Python</em></strong> and <strong><em>Office365 SMTP server </em></strong>to send emails.</p><p>We shall be using Python’s built-in <a href="https://docs.python.org/3.8/library/smtplib.html"><strong><em>smtplib</em></strong></a> module to send emails and <a href="https://docs.python.org/3.8/library/email.message.html"><strong><em>EmailMessage</em></strong></a> class from the <a href="https://docs.python.org/3.8/library/email.html#module-email"><strong><em>email</em></strong></a> package to create our email message.</p><p>Following are the basic steps for sending an email using Python:</p><ol><li>Create a EmailMessage object and load data into the From, To and Subject fields.</li><li>Add body to your EmailMessage by using set_content() method of EmailMessage.</li><li>Create an SMTP session by providing the <strong><em>Office365 SMTP server</em></strong> details and your <strong><em>Office365 credentials</em></strong>.</li><li>Use the SMTP object to send the email.</li><li>Terminate the SMTP session.</li></ol><p>Let’s begin by importing the required modules and classes.</p><pre>import smtplib<br>from email.message import EmailMessage</pre><p>Now, let us create a function send_email(), which will handle the whole logic of sending the email. The send_email function definition is as follows:</p><pre># Function to handle the email sending logic<br>def send_email(senders_email, receivers_email, email_subject, email_message):</pre><p>In the above function, all the arguments are <strong>required</strong> arguments and take <strong>String</strong> as input.</p><p>The next step is to create EmailMessage an object inside the send_email() function and populate the From, To and Subject fields with appropriate data. Also, set the email body using the set_content() method of EmailMessage object.</p><pre># Create a new object of the EmailMessage class<br>msg = EmailMessage()<br># Initialise the EmailMessage object with the following parameters<br>msg[&#39;From&#39;] = senders_email<br>msg[&#39;To&#39;] = receivers_email<br>msg[&#39;Subject&#39;] = email_subject<br># Set the email body using the set_content() method of the EmailMessage Class<br>msg.set_content(email_message)</pre><p>Now, try to create an SMTP session by providing the <strong><em>Office365 SMTP server</em></strong> and <strong><em>port (default=587)</em></strong>. Once the session is created, log in using your <strong><em>Office365 credentials </em></strong>and send the email. Lastly, terminate the SMTP session.</p><pre>try:<br>    # Open a SMTP client session using smtplib.SMTP() method<br>    smtp_session = smtplib.SMTP(&#39;smtp.office365.com&#39;, 587)<br>    smtp_session.starttls()<br>    smtp_session.login(&#39;your_login_name&#39;, &#39;your_login_password&#39;)<br>    smtp_session.send_message(msg)<br>    print(&quot;Email sent out successfully!&quot;)<br>    smtp_session.quit()<br>except smtplib.SMTPException:<br>    print(&quot;Email sending failed. SMTP connection error.&quot;)</pre><p>Call the send_email() function as follows from the main flow</p><pre>send_email(senders_email=&quot;senders_email_id@senders_server.com&quot;,<br>           receivers_email=&quot;receivers_email_id@receivers_server.com&quot;,<br>           email_subject=&quot;Test mail from Python&quot;,<br>           email_message=&quot;Hi,\n\nThis is a test email sent using Python!!&quot;)</pre><p>To make things easier, here is the whole python code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f8d80cce8d08e24b2e56cfa708887ac/href">https://medium.com/media/8f8d80cce8d08e24b2e56cfa708887ac/href</a></iframe><p>Thanks for reading this article, I hope it’s helpful to you.</p><p><strong>DISCLAIMER: All the readers should independently evaluate their needs, technology, and business goals to determine whether or not — and to what extent — they use this information. The code sample written here is not to be used in production. The opinions expressed here represent my own and not those of my current or any previous employers.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5f2dab3432" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>