Forum Discussion

m_essaid's avatar
m_essaid
Valued Contributor
8 years ago

sending mail via CDO and office365.com

Hi,   we're migrating to Office365.com and I'm not longer able to use mails.   basicaly, my parameters are : - sendusing 2 - smtpserver smtp.office365.com - server port 25 or 587 (which are op...
  • m_essaid's avatar
    8 years ago

    this script works pretty well (sent by a guy from SmartBear, thanks to him)

     

    function Test()
    {
      SendEmail("FROM_EMAIL", "TO_EMAIL", "Test", "TestMessage", "");
    }
     
    function SendEmail(mFrom,  mTo,  mSubject,  mBody,  mAttachment)
    {
      var smtpServer = "smtp.office365.com";
      var smtpPort = 25;
      var userLogin = "user.name@domain.com"; 
      var userPassword = "password";
      var autentificationType = 1; // cdoBasic
      var connectionTimeout = 15;
      var useSSL = true;
     
      try {
        var schema = "http://schemas.microsoft.com/cdo/configuration/";
        var mConfig = Sys.OleObject("CDO.Configuration");
        mConfig.Fields.Item(schema + "sendusing") = 2; // cdoSendUsingPort
        mConfig.Fields.Item(schema + "smtpserver") = smtpServer;
        mConfig.Fields.Item(schema + "smtpserverport") = smtpPort;
        mConfig.Fields.Item(schema + "sendusername") = userLogin;
        mConfig.Fields.Item(schema + "sendpassword") = userPassword;
        mConfig.Fields.Item(schema + "smtpauthenticate") = autentificationType;
        mConfig.Fields.Item(schema + "smtpusessl") = useSSL;
        mConfig.Fields.Item(schema + "smtpconnectiontimeout") = connectionTimeout;
        mConfig.Fields.Update();
        
        var mMessage = Sys.OleObject("CDO.Message");
        mMessage.Configuration = mConfig;
        mMessage.From = mFrom;
        mMessage.To = mTo;
        mMessage.Subject = mSubject;
        mMessage.HTMLBody = mBody;
        
        if(0 < mAttachment.length) {
          mMessage.AddAttachment(mAttachment);
        }
        
        mMessage.Send();
      }
      catch(exception) {
        Log.Error("E-mail cannot be sent", exception.description);
        return false;  
      }
      Log.Message("Message to <" + mTo +  "> was successfully sent");
      return true;
    }