Sample code for 30+ languages & platforms
Java

Send email using SMTP STARTTLS.

Send email using SMTP STARTTLS. With STARTTLS, the SMTP client connects to the SMTP server on port 25 (non-SSL) and then issues a STARTTLS command to convert the connection to a secure TLS channel.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The mailman object is used for sending and receiving email.
        MailMan mailman = new MailMan();

        // Set the SMTP server.
        mailman.setSmtpHost("smtp.mymailserver.com");

        mailman.setSmtpUsername("myLogin");
        mailman.setSmtpPassword("myPassword");

        mailman.setStartTLS(true);

        // Create a new email object
        Email email = new Email();

        email.setSubject("This is a test");
        email.setBody("This is a test");
        email.setFrom("Chilkat Support <support@chilkatsoft.com>");
        email.addTo("Chilkat Admin", "admin@chilkatsoft.com");

        try {
            mailman.sendEmail(email);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            mailman.closeSmtpConnection();
        } catch (ChilkatException e) {
            System.out.println("Connection to SMTP server not closed cleanly.");
        }

        System.out.println("Mail Sent!");
    }
}