Send emails using SSL encryption over port 465

更新时间:
复制 MD 格式

For security reasons, port 25 is blocked by default on Cloud Web Hosting instances. Use the Secure Sockets Layer (SSL) encryption port (port 465) to send emails. This topic provides .NET, PHP, and ASP examples to show how to send emails using the SSL encryption port. The implementation for other languages is similar.

Background information

  • Basic principle

    The basic principle of sending emails over an SSL-encrypted port is the same as connecting a local client, such as Outlook, to a mailbox server to send emails. The host connects to the sending server of an external mailbox. It then uses the account and password configured in the program for authentication to send the email. The web hosting server itself does not send the email.

  • Key points

    The basic implementation for sending emails over an SSL-encrypted port is the same as for using port 25. However, when you switch to the SSL protocol, note the following:

    • The external mailbox server that your program calls to send an email must support the SSL encryption feature.

    • When the host connects to the mailbox server, you must enable the SSL protocol in your program.

    • Change the connection port for the external mailbox server to the SSL encryption port (port 465). For specific configuration information, consult your email service provider.

Limits

The enhanced edition of the Windows operating system for Cloud Web Hosting does not support sending emails.

Prerequisites

Confirm that the fsockopen parameter for PHP is set to Enabled.

Note

By default, the fsockopen parameter for PHP is set to Enabled. To change this setting, see Set PHP.INI parameters.

Procedure

  1. Contact your email service provider to obtain the configuration information for sending emails using SSL encryption.

    The main configuration items are as follows:

    Configuration item

    Description

    Sending mail server address

    Enter the server's mailbox address. For example, a***.example.com.

    Sending mail server port number

    The encryption port is typically port 465.

    Mailbox username

    This can be the email address or the prefix of the email address. For details, consult your email service provider.

    Mailbox client password

    For some email service providers, the logon password for the web browser interface is different from the client password. You may need to set it separately. For details, consult your email service provider.

  2. On your local host, use an email client, such as Outlook or Foxmail, to configure the client with the obtained configuration information and send a test email.

    Note

    If the email fails to send, the configuration information is incorrect. Contact your email service provider to resolve the issue.

  3. Select the program example that matches your development language and download it to your local host.

    The program examples are as follows:

    • .NET source code example

      The following is a snippet of the source code:

      MailMessage mmsg = new MailMessage();
      
      mmsg.Subject = "Subject";                // Email subject
      mmsg.BodyFormat = MailFormat.Html;
      mmsg.Body = "Body";                   // Email body
      mmsg.BodyEncoding = Encoding.UTF8;    // Body encoding
      mmsg.Priority = MailPriority.High;    // Priority
      mmsg.From = "a***@example.com";       // Sender's email address
      mmsg.To = "b***@example.com";         // Recipient's email address
      mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
      mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test01");  // Username
      mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123****"); // Password
      mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);     // Port 
      mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");      // Use SSL
      System.Web.Mail.SmtpMail.SmtpServer = "smtp.****.com";                                     // SMTP server
      
      SmtpMail.Send(mmsg);
    • PHP source code example

      The following is a snippet of the source code:

      <?php
      require 'PHPMailerAutoload.php';
      require_once('class.phpmailer.php');
      require_once("class.smtp.php");
      $mail = new PHPMailer();
      
      $mail->CharSet="UTF-8";           // Set the email encoding. The default is ISO-8859-1. If you send Chinese characters, you must set this to UTF-8.
      $mail->IsSMTP();                  // Set to use the SMTP service.
      $mail->SMTPAuth = true;           // Enable the SMTP authentication feature.
      $mail->SMTPSecure = "ssl";        // Enable SSL.
      $mail->SMTPDebug = 2;
      $mail->Host = "smtp.****.com";    // SMTP server.
      $mail->Port = 465;                // Port number of the SMTP server.
      $mail->Username = "test01";                                      // SMTP server username.
      $mail->Password = "123****";                                     // SMTP server password.
      $mail->SetFrom('a***@example.com', 'a***');                      // Set the sender's address and name.
      $mail->AddReplyTo("b***@example.com","b***");                    // Set the reply-to address and name.
      $mail->Subject = 'Email subject';                                      // Set the email subject.
      $mail->AltBody = "To view this email, please switch to an HTML-compatible email client."; // Optional, for backward compatibility.
      $mail->MsgHTML('<html>helo</html>');                             // Set the email body.
      $mail->AddAddress('c***@example.com', "c***");
      $mail->AddAttachment("images/phpmailer.gif");                    // Attachment.
      if(!$mail->Send()) {
          echo "Send failed: " . $mail->ErrorInfo;
      } else {
          echo "Congratulations, email sent successfully!";
      }
      
      ?>
    • ASP source code example

      The following is a snippet of the source code:

      <%
      Set Mail = CreateObject("CDO.Message")
      
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.****.com"
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="test01"
      Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="123****"
      Mail.Configuration.Fields.Update
      Mail.Subject="Email subject"
      Mail.From="a***@example.com"
      Mail.To="b***@example.com"
      Mail.TextBody="This is an email message."
      
      Mail.Send
      Set Mail = Nothing
      %>
      <%="Sent successfully!!!"%>
  4. Use the verified configuration information to fill in the mailbox server configuration items in the program example.

  5. Upload the completed program to your Cloud Web Hosting instance using a tool such as FileZilla, and then send a test email.

    For more information about how to upload the program to your Cloud Web Hosting instance, see Manage website program files with FileZilla.