Knowledgebase

Send en e‑mail med Python [Med vedhæftning] - Ny vejledning

Lær, hvordan du nemt kan sende e‑mails med Python ved hjælp af Aspose API’en. Med Aspose.Email for Python via .NET‑biblioteket kan du oprette, konvertere, sende, modtage eller manipulere e‑mailbeskeder, inklusive MSG , PST , EML , EMLX og mange andre filformater.

Send en e‑mail med Python

Aspose.Email for Python via .NET er et robust og kraftfuldt e‑mailprogrammerings‑API til at oprette, manipulere og konvertere almindelige e‑mailbeskedformater uden at bekymre sig om kompleksiteten i den underliggende formatudførelse. Det er et brugervenligt klassebibliotek, der er sammensat for at levere en fremragende og robust e‑mailprogrammeringskomponent. Aspose.Email for Python sparer tid og kræfter ved at gøre det muligt hurtigt og nemt at kode kompleks håndtering af beskedfiler i dine applikationer.

Vores API kan bruges til at udføre en række opgaver i applikationer og kan integreres med enhver type applikation. Vi leverer kodeeksempler, så udviklere hurtigt kan komme i gang.

Aspose.Email for Python via .NET features:

• Konvertering og rendering af e‑mailformater
• Send og modtag almindelige e‑mailformater, inklusive vedhæftninger
• Download e‑mail fra IMAP- og POP3‑mailservere
• Manipulér (opret og opdater) opgaver ved hjælp af iCalendar
• Håndtér beskedlagringsfiler
• Håndtér e‑mailvedhæftninger
• Manipulér iCalendar‑kompatible møder eller aftaler
• TLS‑ og SSL‑understøttelse
• TNEF‑vedhæftninger

Supported file formats:

MSG – Microsoft Outlook‑ og Exchange‑format til lagring af e‑mailbeskeder, aftaler og lignende opgaver
PST – Outlook Personal Storage‑filer til lagring af e‑mails, kalendere, kontakter og andre filformater
OST – Brugerens postkassedata på den lokale maskine efter registrering med Exchange Server via Microsoft Outlook
OFT – Microsoft Outlook‑beskedskabeloner
EML – E‑mailbeskeder gemt med Outlook eller lignende relevante programmer
EMLX – Tekstfil udviklet og implementeret af Apple
MBOX – Bruges til indsamling af elektroniske mailbeskeder
ICS – iCalendar‑filformat til deling af begivenheder og planlægning af opgaver via e‑mail
VCF – Filformat til elektroniske visitkort og lagring af kontaktinformation
HTML – HyperText Markup Language‑udvidelse til websider og visning i browsere
MHTML – Dette er arkivformatet og indeholder indholdet af en webside

Opret og indstil indholdet af e‑mailen ved hjælp af 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 en e‑mail ved hjælp af Python

Aspose.Email for Python giver dig mulighed for at sende e‑mails ved hjælp af SMTP-, POP3- og IMAP‑mailservere. Du kan nemt logge ind på POP3‑servere med dine legitimationsoplysninger (brugernavn og adgangskode) eller APOP‑godkendelse for at udføre e‑mailoperationer såsom at se postkassens størrelse og antal beskeder, hente hele beskeden eller dens header, slette beskeder fra serveren samt grundlæggende POP3‑kommandoer.

Aspose.Email for Python understøtter også IMAP‑protokollen inklusive dens kommandoer samt godkendelse, valg, oprettelse, sletning, forespørgsel af mapper, gemning af beskeder, hentning, sletning og manipulation af etiketter.

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 Betalt rådgivning 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.