En

KNOWLEDGEBASE

Send an email with Python [With attachment] - New tutorial

Learn how to easily send emails with Python using the Aspose API. With Aspose.Email for Python via .NET library you can create, convert, send, receive or manipulate email messages, including MSG, PST, EML, EMLX, and much more file formats.

Send an email with Python

Aspose.Email for Python via .NET is a robust and powerful email programming API for creating, manipulating, and converting common email message formats without concern about the complexities of the underlying format execution. It’s an easy-to-use class library assembled to produce an outstanding and robust email programming component. Aspose.Email for Python saves time and effort by allowing you to quickly and easily code complex message file handling into your applications fast and easily.

Our API can be used to perform a variety of tasks in applications and can be integrated with any type of application. We provide code examples to get developers up and running quickly.

Aspose.Email for Python via .NET features:

• Conversion and rendering of email formats
• Send and receive common emails formats, including attachments
• Download email from IMAP and POP3 mail servers
• Manipulate (create and update) tasks using iCalendar
• Operate message storage files
• Operate email attachments
• Manipulate iCalendar-compliant meetings or appointments
• TLS and SSL support
• TNEF attachments

Supported file formats:

MSG - Microsoft Outlook and Exchange format for storing email messages, appointments, and similar tasks
PST - Outlook Personal Storage Files for storing emails, calendars, contacts, and other file formats
OST - User’s mailbox data on the local machine after registration with Exchange Server with Microsoft Outlook
OFT - Microsoft Outlook message template files
EML - Email messages saved with Outlook or similar relevant applications
EMLX - Text file developed and implemented by Apple
MBOX - It is used for the collection of electronic mail messages
ICS - iCalendar file format for sharing events and scheduling tasks via emails
VCF - file format for electronic business cards and storing contact information
HTML - HyperText Markup Language extension for web pages and display in browsers
MHTML - this is the archive format and contains the contents of a webpage

Create and set contents of email using Python

Create email message

With the MailMessage class, developers can create a new email message. This class represents an email message. Email properties like From, To, Subject, and Body can be easily attached to the created email message.

We will do the next steps for creating a new email message:

  1. Creating an instance of the MailMessage class
  2. Setting email message properties
  3. Saving email messages in different formats, like EML, MSG, and MHTML

The code snippet below shows you how to create a new email with different properties:
For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
eml = ae.MailMessage()
eml.subject = "New MailMessage created with Aspose.Email for Python"
eml.html_body = "<b>This line is in bold </b> while this is normal text"
eml.from_address = "from@domain.com"

eml.to.append(ae.MailAddress("to1@domain.com", "Recipient 1"))
eml.to.append(ae.MailAddress("to2@domain.com", "Recipient 2"))

eml.cc.append(ae.MailAddress("cc1@domain.com", "Recipient 3"))
eml.cc.append(ae.MailAddress("cc2@domain.com", "Recipient 4"))

#Save generated EML in different formats to disc
eml.save(dataDir + "CreateNewMailMessage_out.eml")
eml.save(dataDir + "CreateNewMailMessage_out.msg", ae.SaveOptions.default_msg_unicode)
eml.save(dataDir + "message_out.msg", ae.SaveOptions.default_msg)
eml.save(dataDir + "message_out.mhtml", ae.SaveOptions.default_mhtml)
eml.save(dataDir + "message_out.html", ae.SaveOptions.default_html)

Setting the HTML body of the message

With HtmlBody we specify the HTML content of a message body. The code snippet below shows you how to set the HTML body:
# For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
# Declare message as MailMessage instance
eml = ae.MailMessage()

# Specify HtmlBody
eml.html_body = "<html><body>This is the HTML body</body></html>"

Setting alternate text in the email message

AlternateView class specifies copies of an email message in different formats, and this class has two properties:

• LinkedResources, used when rendered, URLs within the email’s content are matched and resolved against the URLs in the Content Link of each LinkedResources object in the LinkedResources collection
• BaseUri, used by the mail reader to resolve relative URLs within the body

The example code snippet is below:
# For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
# Declare message as MailMessage instance
eml = ae.MailMessage()

# Creates AlternateView to view an email message using the content specified in the //string
alternate = AlternateView.create_alternate_view_from_string("Alternate Text")

# Adding alternate text
eml.add_alternate_view(alternate)

Adding an attachment to an email

We’ll take the following steps to add an attachment to an email:

  1. Creating an instance of the MailMessage and Attachment class
  2. Loading attachment into the Attachment instance
  3. Adding the Attachment instance into the MailMessage class instance

The code snippet below shows an example of how to add an attachment to an email:
For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
# Create an instance of MailMessage class
message = MailMessage("sender@domain.com", "receiver@domain.com")

# Load an attachment
attachment = Attachment(dataDir + "1.txt");

# Add Multiple Attachment in instance of MailMessage class and Save message to disk
message.attachments.append(attachment);
message.add_attachment(Attachment(dataDir + "1.jpg"))
message.add_attachment(Attachment(dataDir + "1.doc"))
message.add_attachment(Attachment(dataDir + "1.rar"))
message.add_attachment(Attachment(dataDir + "1.pdf"))
message.save(dataDir + "AddEmailAttachments_out.msg", SaveOptions.default_msg_unicode)

Send an email using Python

Aspose.Email for Python allows you to send emails using SMTP, POP3, and IMAP mail servers. You can easily log in to POP3 servers with your credentials (username and password) or APOP authentication to perform email operations such as viewing mailbox size and number of messages, retrieving the entire message or its header, deleting messages from the server as well as basic POP3 commands.

Aspose.Email for Python also supports the IMAP protocol including its commands as well as authentication, selecting, creating, deleting, query folders, saving messages, getting, deleting, and manipulating labels.

Sending messages with SMTP client

To send an email message with Python, we will follow these steps:

  1. Creating an instance of the MailMessage class
  2. Specifying the email addresses of the sender and receiver in the MailMessage instance
  3. Specifying the TextBody content
  4. Creating an instance of the SmtpClient class and sending the email message

The following code snippet shows you how to send a text email using Python:
For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
eml = ae.MailMessage()
eml.subject = "Message with Plain Text Body"
eml.body = "This is plain text body."
eml.from_address = "from@gmail.com"
eml.to.append(ae.MailAddress("to@gmail.com", "Recipient 1"))

#Send using Smtp Client
client = SmtpClient("smtp.gmail.com", 995, "username", "password")
client.security_options = SecurityOptions.AUTO

client.send(eml)

If you need help with your project, you can always contact our Paid consulting experts. Our team will work with you to find the best solution for your project, implement it accordingly and provide you with the best value.
You can rely on us in your further growth and development.