# Sending Emails

Emails are sent using `SendMailSession`. Mail session encapsulates the process of preparing emails, opening and closing transport connections, and sending emails.

Mail session is created by the `MailServer`.

```java
SmtpServer smtpServer = MailServer.create()
    .host("http://mail.com")
    .port(21)
    .buildSmtpMailServer();
...
SendMailSession session = smtpServer.createSession();
session.open();
session.sendMail(email1);
session.sendMail(email2);
session.close();
```

Since opening session and sending emails may produce `EmailException`, it is necessary to wrap methods in `try`-`catch` block and closing the session in the `finally` block.

### Sending using SSL

The preferred way of sending e-mails is by using SSL protocol. *Jodd* supports secure e-mail sending. Just set the `ssl()` flag while creating the server.

Here is an example of sending e-mail via [Gmail](http://gmail.com) (port `465` is set by default):

```java
SmtpServer smtpServer = MailServer.create()
    .ssl(true)
    .host("smtp.gmail.com")
    .auth("user@gmail.com", "password")
    .buildSmtpMailServer();
...
SendMailSession session = smtpServer.createSession();
session.open();
session.sendMail(email);
session.close();
```

Everything is the same, just a different session provider is used.


---

# 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://mail.jodd.org/sending-emails.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.
