Saturday, October 7, 2023

Build the CPP-SMTPClient library on Windows

Build the CPP-SMTPClient library on Windows

  1. SMTPClient-library


  2. Getting OpenSSL Dependency:

    You need to obtain OpenSSL, which is a dependency for the CPP-SMTPClient library. There are two methods to get OpenSSL:

    Using Chocolatey:

    • Install Chocolatey on your system if you haven't already.
    • vedio tuto https://www.youtube.com/watch?v=7Eiuvy5_dh8

    • Open a command prompt as an administrator and run the following command:
      choco install openssl
      If you want the 32-bit version, use the following command:
      choco install --forceX86 openssl
    • After installation, the OpenSSL files will be in either C:\Program Files\OpenSSL-Win64 or C:\Program Files (x86)\OpenSSL-Win32 depending on the version you installed.
    • It's recommended to create an environment variable OPENSSL_ROOT_DIR that points to your OpenSSL installation folder, e.g., C:\Program Files\OpenSSL-Win64.
    • Use the command RefreshEnv to ensure your environment variables are updated.

    Using OpenSSL Binary Distributions:


    • Download one of the prebuilt OpenSSL binaries here.
    • Extract the downloaded files to a location on your hard drive.
    • Create an environment variable OPENSSL_ROOT_DIR pointing to the OpenSSL installation folder, e.g., C:\openssl\x64.
    • Close and reopen your Windows session to update environment variables.

  3. Preparing the Build Folder:

    Navigate to the installation folder where you have the CPP-SMTPClient library source code:


  4.     cd <path to SMTPClient repo> mkdir build
        mkdir build
        cd build

  5. Configuration and Generation:

    Use CMake to configure and generate the project files based on your desired build mode (Release/Debug) and architecture (64-bit/32-bit). Run one of the following commands:

    Release Mode:

    • 64 bits (x64):
    • cmake ..

    • 32 bits (x86):
    • cmake -DCMAKE_GENERATOR_PLATFORM=Win32 -DCMAKE_BUILD_TYPE=Release -T host=x86 ..

    Debug Mode:

    • 64 bits (x64):
       cmake -DCMAKE_BUILD_TYPE=Debug ..
    • 32 bits (x86):
       cmake -DCMAKE_GENERATOR_PLATFORM=Win32 -DCMAKE_BUILD_TYPE=Debug -T host=x86 ..
  6. Troubleshooting OpenSSL Folders and Library Files:

    During the configuration and generation phase, CMake should locate OpenSSL. You can verify this by checking the output. Look for lines that mention OpenSSL, and they should point to the correct folders and libraries.

    If OpenSSL was not found correctly, you can provide the necessary information using CMake variables:

    • OPENSSL_ROOT_DIR
    • OPENSSL_INCLUDE_DIRECTORY
    • OPENSSL_LIBRARY_DIRECTORY
    • OPENSSL_CRYPTO_LIBRARY
    • OPENSSL_SSL_LIBRARY

    For example:

     cmake -DOPENSSL_ROOT_DIR=C:\openssl\x64 -DOPENSSL_INCLUDE_DIRECTORY=C:\openssl\x64\include -DOPENSSL_LIBRARY_DIRECTORY=C:\openssl\x64\lib -DOPENSSL_CRYPTO_LIBRARY=crypto -DOPENSSL_SSL_LIBRARY=ssl ..
  7. Building the Library:

    To build the library, use the following commands based on your chosen build mode and architecture:

    Release Mode:

    • 64 bits (x64):
       cmake --build . --config Release
    • 32 bits (x86):
       cmake --build . --config Release

    Debug Mode:

    • 64 bits (x64):
       cmake --build .
    • 32 bits (x86):
      cmake --build .

    The resulting library, smtpclient.dll, will be created in the build\Debug or build\Release folder, depending on your build mode.


  8. Building with Visual Studio (Optional):

    If you prefer using Visual Studio, you can also build the project from within Visual Studio:

    • Click "File" -> "Open" -> "Folder..."
    • Select the folder where you have downloaded the CPP-SMTPClient library.
    • Build the project using Visual Studio.
    • make sure you have openssl include folder in your sln directory, copy and paste it from
    • C:\Program Files\OpenSSL-Win64

That's it! You should now have successfully built the CPP-SMTPClient library on Windows.


To run the example code


use smtp.gmail.com for the "<your smtp server address>"

Create a Google app password for "App Passwword"
"

OpportunisticSecureSMTPClient client("smtp.gmail.com", 587); client.setCredentials(Credential("my@gmail.com", "App Passwword"));

try { const MessageAddress from("sender@gmail.com", "Test Address Display"); const auto to = { MessageAddress("receiver@gmail.com") }; const auto subject = "This is a test (Subject)"; const auto body = "<html><body><h1>Hello,</h1><br/><br/>How are you?</body></html>"; const auto cc = { MessageAddress("receiver@gmail.com") }; const std::vector<MessageAddress> bcc = {}; const auto attachment = {Attachment("C:\\Pictures\\test.png") }; HTMLMessage msg(from, to, subject, body, cc, bcc, attachment); client.sendMail(msg); }

catch (std::invalid_argument& err) {

std::cerr << err.what() << std::endl;

}

App Password


An "App Password" is a specific type of password generated by some online service providers, like Google, to allow secure access to your account by applications or devices that don't support or are not compatible with the standard two-factor authentication (2FA) methods. Here's how it works:

  • Two-Factor Authentication (2FA): Many online services, including email providers like Gmail, offer 2FA as an added layer of security. With 2FA enabled, in addition to your regular password, you need to provide a second authentication factor, such as a one-time code sent to your mobile device, to access your account.
  • Problem with Some Apps: While 2FA enhances security, it can be problematic for certain applications or devices that don't support the 2FA process. These apps may not be able to handle the second authentication factor.
  • App Password Solution: To overcome this limitation, some services, including Google, provide the option to generate "App Passwords." An App Password is a randomly generated, long, and complex password that can be used by a specific application or device to access your account. You only need to enter this password once for each application or device.
  • Security: App Passwords are secure because they are not easily guessable, and they provide a way to access your account without revealing your primary account password.
  • Specific to Applications: Each App Password is typically specific to the application or device you generate it for. This means that if you have multiple apps or devices that need access, you can generate a unique App Password for each one.
  • Revocable: If you ever lose control of a device or suspect that an App Password has been compromised, you can simply revoke that specific App Password without affecting your main account password.
  • How to Generate: To generate an App Password, you usually need to go to your account settings (e.g., Google Account settings), find the "Security" or "Two-Factor Authentication" section, and look for the option to generate an App Password. You'll typically need to specify the app or device for which you're generating the password.

Remember that the process for generating and managing App Passwords may vary depending on the service provider.

No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...