Encrypt the password used for SendMail

Here is one solution based on encoded package.

To create a pack you have to create a .m file (in this case mailPack.m). There is one example using gmail configuration:

BeginPackage["mailPack`"]

sendMail::usage="sendMail[subject, body, to, file] send mail using myMail";
Begin["`Private`"]

sendMail[subject_,body_,to_,file_:None]:=
SendMail[
    "To"->to,
    "Subject"->subject,
    "Body"->body,
    "From"->"[email protected]",
    "Server"->"smtp.gmail.com",
    "PortNumber"->587,
    "EncryptionProtocol"->"StartTLS",
    "UserName"->"myLogin",
    "Password"->Uncompress@"myCompressedPassword", (*Use compress to create it*)
    "Attachments"->file
]

SetAttributes[sendMail, {ReadProtected,Locked}];

End[]
EndPackage[]

Now you can open another notebook, save in the same directory, and the do this to encode the first:

  SetDirectory@NotebookDirectory[];
  Encode["mailPack.m","mailPack.m"];

Now, if you try to open your mailPack.m as txt file, you will see that it is completely unreadable.

To use your sendMailfunction, copy your pack in the same directory of your file, then use SetDirectory@NotebookDirectory[], then use Needs["mailPack"]. If you use it all the time, copy the mailPack.m for the Application directory in your Mathematica folder (this command can help you SystemOpen@$UserBaseDirectory). Then you don't need the SetDirectory part neither to put the mailPack file in the same directory of your current notebook.


In general SendMail should not access an email account that stores important emails or that functions to reset the passwords of other online accounts. Such a valuable email account should be protected by two-factor authentication as well as user-managed access such as OAuth, and so forth. Because SendMail does not support any such features it can not access an truly secure email account. Rather than accept inadequate security on an important email account, it is much better to setup a separate email account especially for SendMail to access. If this special account is on gmail, then SendMail will require the Allow less secure apps setting. In addition, because it will only be used with SendMail the need to encrypt its password will depend solely the SendMail application’s security needs.

Besides setting up an email account especially for SendMail, there are at least two other email solutions to consider.

If you’re developing web applications, then you may already be using the free version of the Wolfram Development Platform. If you’re not at least at the $20/month Explorer plan, then upgrade and you will be able to deploy and call a SendMail API in the Wolfram Cloud:

CloudDeploy[
   APIFunction[
      {  "To" -> <|"Interpreter" -> "EmailAddress"|>,
         "Subject" -> "String",
         "Body" -> "String" },
      SendMail[#] &], 
   FileNameJoin[{$CloudRootDirectory, "WolframCloudSandboxEmail"}],
   Permissions -> "Public" ]

You only need to do the above CloudDeploy once. Then every time you want to send an email, just call the API from your desktop:

URLExecute[
   FileNameJoin[{$CloudRootDirectory, "WolframCloudSandboxEmail"}],
   {  "To" -> "YOUR-NAME <YOUR-EMAIL-ADDRESS>", 
      "Subject" -> "Wolfram Cloud Sandbox Email", 
      "Body" -> "Here is the body of the message." } ]

If for some reason you don’t want a public API that sends you email, then deploy the API as a private cloud object, add the Username and Password options to URLExecute, and protect it by the encoded package approach in Murta’s answer:

URLExecute[
   FileNameJoin[{$CloudRootDirectory, "WolframCloudPrivateSandboxEmail"}],
   {  "To" -> "YOUR-NAME <YOUR-EMAIL-ADDRESS>", 
      "Subject" -> "Wolfram Cloud Private Object Sandbox Email", 
      "Body" -> "Here is the body of the message." }, 
   "Username" -> "YOUR-WOLFRAM-ID",
   "Password" -> "YOUR-WOLFRAM-CLOUD-PASSWORD" ]

On the other hand, if you’re developing applications that have the potential to generate a significant volume of email, you probably are already using or at least considering an email delivery service. Calling such a service will be among your standard approaches to sending an email. For example, you can easily setup and use a Mailgun sandbox:

URLFetch["https://api.mailgun.net/v3/YOUR-SANDBOX-DOMAIN-NAME/messages",
   "Method" -> "POST",
   "MultipartElements" ->
      {  {"from", "text/plain"} ->
            "Mailgun Sandbox <postmaster@YOUR-SANDBOX-DOMAIN-NAME>",
         {"to", "text/plain"} -> "YOUR-NAME <YOUR-EMAIL-ADDRESS>",
         {"subject", "text/plain"} -> "Mailgun Sandbox Email",
         {"text", "text/plain"} -> "Here is the body of the message." },
   "Username" -> "api",
   "Password" -> "YOUR-PRIVATE-API-KEY"]

Assuming that the mail delivery service is also being used to send other important emails, then its password could again be protected by the approach in Murta’s answer.

Comment 2/9/16:

As originally posted this answer simply alerted readers to the email delivery service alternative to SendMail and left it to them to decide on its suitablity for their application. This updated answer adds a Wolfram Development Platform alternative and specifies circumstances that might favor each solution.