Last active
February 6, 2022 13:58
How to Send Word Document by Email using C++. For more information, please follow link: https://kb.aspose.com/words/cpp/how-to-send-word-document-by-email-using-cpp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment