Sending Messages Using the SMTP Service Client in Java
SmtpClient from the com.aspose.email.services.inetmail.client package sends messages through an SMTP server. It requires a mail protocol library on the classpath. For details, see Mail Protocol Libraries Setup.
com.aspose.email package has a class with the same name, which is described in Working with SMTP Client. Use explicit imports to select the class you need.
Sending a Text Message
Create the message with InetMessage.builder(), and then pass it to send. Set the sender and at least one recipient. For a message without recipients, send throws MailClientException before it connects to the server. A message without a sender reaches its recipients with no From address, and the client writes a warning to the log.
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.port(587)
.starttls()
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
}
Each call to send opens its own connection to the server and closes it when the message has been sent.
Connecting Without Authentication
Some servers, for example a relay in a local network, accept messages without authentication. To connect to such a server, do not call credentials:
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("relay.example.com")
.port(25)
.noSecurity()
.build()) {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
}
Setting the Local Host Name
When the client connects, it sends the name of the local computer to the server. To get this name, the mail library looks up the local address in DNS. If the computer has no reverse DNS record, the lookup can take several seconds, and it repeats on every call to send. To skip the lookup, set the name with localHost:
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.credentials(new PasswordCredentials("user@example.com", "password"))
.localHost("client.example.com")
.build();
Setting Recipients and the Sender Name
The to, cc and bcc methods accept one or more addresses. To show a display name for the sender, pass the name first and the address second:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("Reports Team", "user@example.com")
.to("user@example.com", "user2@example.com")
.cc("user3@example.com")
.bcc("user4@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
Sending an HTML Message
Use htmlBody to set the HTML version of the message. If you also set textBody, the message contains both versions, and the mail client of the recipient displays the one it supports:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.htmlBody("<p>The report is <b>ready</b>.</p>")
.build();
smtp.send(message);
Adding Attachments
To add an attachment, create an InetAttachment object with a file name, a content type and the data, and then pass it to attach:
import com.aspose.email.services.inetmail.model.InetAttachment;
import com.aspose.email.services.inetmail.model.InetMessage;
import java.nio.charset.StandardCharsets;
byte[] data = "Total: 42".getBytes(StandardCharsets.UTF_8);
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is attached.")
.attach(InetAttachment.of("report.txt", "text/plain", data))
.build();
smtp.send(message);
Handling Errors
When a message cannot be sent, for example because the server rejects the credentials, send throws MailClientException, which extends java.io.IOException:
import com.aspose.email.services.inetmail.MailClientException;
import com.aspose.email.services.inetmail.model.InetMessage;
try {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("not a valid address")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
} catch (MailClientException e) {
System.out.println(e.getMessage() + ": " + e.getCause().getMessage());
}