Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Sign an email and send the signed bytes over SMTP

See more DKIM Examples

Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.

Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkMailMan mail = new CkMailMan();
    mail.put_SmtpHost("smtp.example.com");
    mail.put_SmtpPort(465);
    mail.put_SmtpSsl(true);
    mail.put_RequireSslCertVerify(true);
    mail.put_SmtpUsername("SMTP_USERNAME");
    mail.put_SmtpPassword("SMTP_PASSWORD");
    CkEmail email = new CkEmail();
    email.put_From("Workshop team <updates@example.com>");
    email.put_Subject("Workshop registration");
    email.put_Body("Registration opens on Monday.");
    success = email.AddTo("Newsletter editor","editor@example.net");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    //  Render after all headers, recipients, and content are final.
    CkBinData message = new CkBinData();
    success = mail.RenderToMimeBd(email,message);
    if (success != true) {
        Log.i(TAG, mail.lastErrorText());
        return;
        }

    //  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
    CkPrivateKey privateKey = new CkPrivateKey();
    success = privateKey.LoadAnyFormatFile("dkim-private.pem","");
    if (success != true) {
        Log.i(TAG, privateKey.lastErrorText());
        return;
        }

    CkDkim signer = new CkDkim();
    signer.put_DkimDomain("example.com");
    signer.put_DkimSelector("mail2026");
    signer.put_DkimAlg("rsa-sha256");
    success = signer.SetDkimPrivateKey(privateKey);
    if (success != true) {
        Log.i(TAG, signer.lastErrorText());
        return;
        }

    success = signer.DkimSign(message);
    if (success != true) {
        Log.i(TAG, signer.lastErrorText());
        return;
        }

    //  archive@example.com is an envelope-only blind copy.
    String recipients = "editor@example.net,archive@example.com";
    success = mail.SendMimeBd("updates@example.com",recipients,message);
    if (success != true) {
        Log.i(TAG, mail.lastErrorText());
        return;
        }

    Log.i(TAG, "SMTP relay accepted the signed message.");
    success = mail.CloseSmtpConnection();
    if (success != true) {
        Log.i(TAG, "The SMTP session did not close cleanly.");
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}