Šiame pavyzdyje išnagrinėsime, kaip išsiųsti Word Document el. paštu naudojant C++. Šiame pavyzdiniame kode pamatysite, kaip patogu siųsti DOC el. laiško tekste naudojant C++. Jums tereikia atlikti paprastus API skambučius ir atlikti kelis paprastus veiksmus MS Windows, kad tai pasiektumėte.
Veiksmai, kaip siųsti Word dokumentą el. paštu naudojant C++
- Sukonfigūruokite Aspose.Words.Cpp ir Aspose.Email.Cpp naudodami NuGet paketų tvarkyklės įrankį
- Pridėkite nuorodą į Aspose::Words ir Aspose::Email vardų erdves
- Sukurkite dokumento klasės objektą į DOCX, kad jis būtų išsiųstas el
- Išsaugokite DOCX į MHTML tarpinį formatą naudodami srautą
- Įkelkite MHTML naudodami Aspose.Email MailMessage Class egzempliorių ir nustatykite pranešimo ypatybes
- Inicijuokite SmtpClient klasės egzempliorių, kad išsiųstumėte MailMessage kaip el
Atlikdami šiuos veiksmus pastebėjome, kaip sukurti el. laišką iš DOCX naudojant C++. Tai apima DOC failo įkėlimą, skirtą siųsti kaip el. laišką, ir išsaugoti jį kaip tarpinį MHTML formatą naudojant srautą. Tada naudojant MailMessage class egzempliorių įkeliamas tarpinis MHTML formatas kartu su kitų pašto savybių nustatymu. Galiausiai, naudojant SMTP klientų klasę, el. laiškas išsiunčiamas.
Kodas, skirtas siųsti Word dokumentą el. paštu naudojant C++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/SaveFormat.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <system/exceptions.h> | |
#include <Clients/SecurityOptions.h> | |
#include <Clients/Smtp/SmtpClient/SmtpClient.h> | |
#include <Licensing/License.h> | |
#include <MsgLoadOptions.h> | |
#include <EmlLoadOptions.h> | |
#include <MhtmlLoadOptions.h> | |
#include <MailMessage.h> | |
#include <system/shared_ptr.h> | |
#include <system/object.h> | |
#include <system/io/file.h> | |
#include <system/io/memory_stream.h> | |
#include <system/io/file_stream.h> | |
using namespace Aspose::Email; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace System; | |
using namespace System::IO; | |
void DocumentToEmail() | |
{ | |
// Setting the API license for Aspose.Words for C++ | |
System::String TotalLicFilePath = u"Aspose.Total.CPP.lic"; | |
SharedPtr<Aspose::Words::License> WordsCPPLicense = System::MakeObject<Aspose::Words::License>(); | |
WordsCPPLicense->SetLicense(TotalLicFilePath); | |
// Set the license for Aspose.Email for C++ | |
SharedPtr<Aspose::Email::License> EmailLicense = System::MakeObject<Aspose::Email::License>(); | |
EmailLicense->SetLicense(TotalLicFilePath); | |
SharedPtr<Document> DocumentEmail = MakeObject<Document>( u"EmailDocument.docx"); | |
// Save into a memory stream in MHTML format. | |
SharedPtr<System::IO::Stream> stream = System::MakeObject<System::IO::MemoryStream>(); | |
DocumentEmail->Save(stream, SaveFormat::Mhtml); | |
// Reset the stream position to start so that Aspose.Email can read it. | |
stream->set_Position(0); | |
// Create an Aspose.Email message from the saved stream | |
SharedPtr<MailMessage > EmailMessage = MakeObject<Aspose::Email::MailMessage>(); | |
// Load the file in MHTML format | |
EmailMessage = MailMessage::Load(stream, System::MakeObject<MhtmlLoadOptions>()); | |
EmailMessage->set_From(u"your_from_email@email.com"); | |
EmailMessage->set_To(u"your_to_email@email.com"); | |
EmailMessage->set_Subject(u"Test Message using Aspose. Words and Aspose.Email APIs"); | |
// Initialize SMTP client and it's properties to send email | |
SharedPtr<Aspose::Email::Clients::Smtp::SmtpClient> EmailSmtpClient = MakeObject<Aspose::Email::Clients::Smtp::SmtpClient>(); | |
EmailSmtpClient->set_Host(u"smtp.gmail.com"); | |
EmailSmtpClient->set_Username(u"YourEmail@gmail.com"); | |
EmailSmtpClient->set_Password(u"Your Gamil Password"); | |
EmailSmtpClient->set_Port(587); | |
EmailSmtpClient->set_SecurityOptions(Aspose::Email::Clients::SecurityOptions::SSLExplicit); | |
// Send word email message | |
EmailSmtpClient->Forward(u"Sender@domain.com", u"Recipient@domain.com", EmailMessage); | |
} |
Aukščiau pateiktame pavyzdyje mes * siunčiame dokumentą el. paštu naudodami C++* dviem etapais. Pirmąjį etapą sudarė DOCX įkėlimas ir išsaugojimas kaip MHTML failas naudojant srautus. Antrame etape mes panaudojome MailMessage klasę, kad įkeltume MHTML ir nustatytume kitas pašto ypatybes. Tada naudodami SmtpClient klasę, išsiunčiame dokumentą el. paštu naudodami C++.
Šioje temoje mes sutelkėme dėmesį į tai, kaip sukurti el. laišką iš DOCX naudojant C++. Jei jus domina DOCX konvertavimas į Tiff failą, galite apsilankyti straipsnyje kaip konvertuoti Word į Tiff naudojant C++.