# SMTP Mailer

### KSMTPMailer Class Overview

* **MIME Multipart Message Creation**\
  Easily create MIME multipart messages with support for text, HTML content, and file attachments.
* **Secure and Non-Secure Connections**\
  Supports STARTTLS/TLS (default) for secure communication and non-secured connections when required.
* **Authentication Options**\
  Compatible with both OAuth 2.0 and basic authentication for flexible access to SMTP servers.
* **Automatic MX Record Lookup**\
  If the SMTP host is not specified, automatically queries DNS for the highest-priority MX record.
* **Enhanced Mail Header**\
  Automatically includes the `X-Mailer` header to identify the sending application.

{% hint style="info" %}
For servers requiring OAuth 2.0 authentication, leverage the [Simple OAuth 2.0 Framework for Authentication](https://sofa.k43.ch) (Freeware) to obtain access tokens efficiently and securely.
{% endhint %}

### Prerequisites

The *KSMTPMailer* class requires the following Jakarta/Angus Mail jar files:

* angus-activation-2.0.x.jar
* angus-mail-2.0.x.jar
* jakarta.activation-api-2.1.x.jar
* jakarta.mail-api-2.1.x.jar

If you do not already have these jar files, you may download them here. Just place the files in a directory pointed to by the JVM class path.

{% file src="/files/fJ2oMhbk2hVb9kL8clRN" %}

### Example

```java
KSMTPMailer mailer = new KSMTPMailer();

mailer.setFrom("john.doe@acme.com");
mailer.setTo("bob.smith@hotmail.com");
mailer.setSubject("Subject");
mailer.addHTML("<h1>Your requested files</h1>");
mailer.addText("Body Text");
mailer.addFile("test1.txt");
mailer.addFile("test2.txt");
mailer.addText("Regards, John");
mailer.send();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java-util.k43.ch/examples/smtp-mailer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
