How to set up sending and receiving email messages via Gmail account

1C Developer team

10.12.2018 15 min

To set up sending and receiving email messages via the Gmail account, you have to enable SSL.
1C:Enterprice 8.3 provides tools for this. The article contains a simple example based on 1C:Subsystems Library 2.0 that shows you how to set up sending and receiving email messages using your Gmail account.

To set up sending and receiving email messages via Gmail account

1. Add the POP3UseSSL and SMTPUseSSL attributes of Boolean type to the EmailAccounts catalog (see Fig.1).

274625f2d2e42fb9b730a1446a916d6a.png
Fig.1. Adding the POP3UseSSL and SMTPUseSSL attributes to the configuration

2. Add the POP3UseSSL and SMTPUseSSL attributes of Boolean type to the AdditionalAccountParameters form of the EmailAccounts catalog, and then add them to the form (see Fig. 2).

bd2ade803c9ef9a616fa97e6b184c695.png
Fig.2. Adding the POP3UseSSL and SMTPUseSSL attributes to the form

3. Add the script that passes the attributes from the ItemForm to the AdditionalAccountParameters form and back:
  • Make the changes specified in Listing 1 to the ItemForm module.
  • Make the changes specified in Listing 2 and Listing 3 to the AdditionalAccountParameters form module.
4. Add the script that passes SSL flags to the connection parameters:
  • Make the changes specified in Listing 4 to the Email common module.
Procedure NotificationProcessing(EventName, Parameter, Source)
   
   If EventName = "SetAdditionalAccountParameters" And Source = Object.Ref Then
      Object.Timeout                   = Parameter.ServerTimeout;
      Object.KeepMessageCopiesAtServer = Parameter.KeepMessageCopiesAtServer;
      Object.KeepMessageAtServerPeriod = Parameter.KeepMessageAtServerPeriod;
      Object.SMTPUser                  = Parameter.SMTPUser;
      Object.SMTPPassword              = Parameter.SMTPPassword;
      Object.POP3Port                  = Parameter.POP3Port;
      Object.SMTPPort                  = Parameter.SMTPPort;
      Object.SMTPAuthentication        = Parameter.SMTPAuthentication;
      Object.SMTPAuthenticationMode    = Parameter.SMTPAuthenticationMode;
      Object.POP3AuthenticationMode    = Parameter.POP3AuthenticationMode;
      
      // Passing Use SSL flags to the object attributes
      Object.POP3UseSSL = Parameter.POP3UseSSL;
      Object.SMTPUseSSL = Parameter.SMTPUseSSL;
      // SSL

      
      Modified = True;
   EndIf;
   
EndProcedure

////////////////////////////////////////////////////////////////////////////////
// FORM COMMAND HANDLERS

&AtClient
Procedure AdditionalSettingsExecute()
   
   AccountStructure = New Structure;
   AccountStructure.Insert("Timeout",                   Object.Timeout);
   AccountStructure.Insert("KeepMessageCopiesAtServer", Object.KeepMessageCopiesAtServer);
   AccountStructure.Insert("KeepMessageAtServerPeriod", Object.KeepMessageAtServerPeriod);
   AccountStructure.Insert("SMTPUser",                  Object.SMTPUser);
   AccountStructure.Insert("SMTPPassword",              Object.SMTPPassword);
   AccountStructure.Insert("POP3Port",                  Object.POP3Port);
   AccountStructure.Insert("SMTPPort",                  Object.SMTPPort);
   AccountStructure.Insert("SMTPAuthentication",        Object.SMTPAuthentication);
   AccountStructure.Insert("SMTPAuthenticationMode",    Object.SMTPAuthenticationMode);
   AccountStructure.Insert("POP3AuthenticationMode",    Object.POP3AuthenticationMode);
   
   // Including Use SSL flags into the account structure
   AccountStructure.Insert("POP3UseSSL",              Object.POP3UseSSL);
   AccountStructure.Insert("SMTPUseSSL",              Object.SMTPUseSSL);
   // SSL
   
   CallParameters = New Structure("Ref, AccountStructure, ReadOnly", Object.Ref, AccountStructure, ReadOnly);
   
   OpenForm("Catalog.EmailAccounts.Form.AdditionalAccountParameters", CallParameters);
   
EndProcedure
Listing 1


&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   
   // Skipping the initialization to guarantee that the form will be received if the SelfTest parameter is passed.
   If Parameters.Property("SelfTest") Then
      Return;
   EndIf;
   
   Ref = Parameters.Ref;
   
   Fill_ListSMTPAuthentication();
   Fill_ListSMTPAuthenticationMode();
   
   AccountStructure = Parameters.AccountStructure;
   
   ServerTimeout = AccountStructure.Timeout;
   
   KeepMessageCopiesAtServer = AccountStructure.KeepMessageCopiesAtServer;
   KeepMessageCopiesDayCount = AccountStructure.KeepMessageAtServerPeriod;
   DeleteMessagesFromServer       = ?(KeepMessageCopiesDayCount = 0, False, True);
   
   SMTPPort = AccountStructure.SMTPPort;
   POP3Port = AccountStructure.POP3Port;
   
   // Passing Use SSL flags
   POP3UseSSL = AccountStructure.POP3UseSSL;
   SMTPUseSSL = AccountStructure.SMTPUseSSL;
   // SSL

   
   POP3AuthenticationMode = AccountStructure.POP3AuthenticationMode;
   
   SMTPAuthenticationMode = AccountStructure.SMTPAuthenticationMode;
   SMTPUser               = AccountStructure.SMTPUser;
   SMTPPassword           = AccountStructure.SMTPPassword;
   
   SMTPAuthentication = AccountStructure.SMTPAuthentication;
   
   SMTPAuthenticationSet = ?(SMTPAuthentication = Enums.SMTPAuthenticationVariants.NotDefined, False, True);
   
   Items.SMTPAuthenticationGroup.CurrentPage = ?(SMTPAuthentication = Enums.SMTPAuthenticationVariants.SetWithParameters, Items.ParametersGroup, Items.EmptyPageGroup);
   
EndProcedure
Listing 2

&AtClient Function FillExtendedParameters() Result = New Structure; Result.Insert("SMTPPort", SMTPPort); Result.Insert("POP3Port", POP3Port); Result.Insert("KeepMessageCopiesAtServer", KeepMessageCopiesAtServer); If DeleteMessagesFromServer Then KeepMessageCopiesDayCount = KeepMessageCopiesDayCount; Else KeepMessageCopiesDayCount = 0; EndIf; Result.Insert("KeepMessageAtServerPeriod", KeepMessageCopiesDayCount); Result.Insert("ServerTimeout", ServerTimeout); If SMTPAuthenticationSet Then Result.Insert("SMTPAuthentication", SMTPAuthentication); If SMTPAuthentication = (SMTPAuthentication_SetWithParameters()) Then Result.Insert("SMTPUser", SMTPUser); Result.Insert("SMTPPassword", SMTPPassword); Result.Insert("SMTPAuthenticationMode", SMTPAuthenticationMode); Else Result.Insert("SMTPUser", ""); Result.Insert("SMTPPassword", ""); Result.Insert("SMTPAuthenticationMode", SMTPAuthenticationMode_None()); EndIf; Else Result.Insert("SMTPAuthentication", SMTPAuthentication_NotDefined()); Result.Insert("SMTPUser", ""); Result.Insert("SMTPPassword", ""); Result.Insert("SMTPAuthenticationMode", SMTPAuthenticationMode_None()); EndIf; Result.Insert("POP3AuthenticationMode", POP3AuthenticationMode); // Including Use SSL flags into the result structure Result.Insert("POP3UseSSL", POP3UseSSL); Result.Insert("SMTPUseSSL", SMTPUseSSL); // SSL Return Result; EndFunction

Listing 3

Function GenerateInternetProfile(Val Account, Val Password = Undefined, Val GenerateSMTPProfile = True, Val GeneratePOP3Profile = True) Export Profile = New InternetMailProfile; Profile.User = Account.User; Profile.Timeout = Account.Timeout; If ValueIsFilled(Password) Then Profile.Password = Password; Else Profile.Password = Account.Password; EndIf; If GenerateSMTPProfile Then Profile.SMTPServerAddress = Account.OutgoingMailServerSMTP; Profile.SMTPPort = Account.SMTPPort; If Account.SMTPAuthentication = Enums.SMTPAuthenticationVariants.SimilarlyPOP3 Then Profile.SMTPAuthentication = SMTPAuthenticationMode.Default; Profile.SMTPUser = Account.User; Profile.SMTPPassword = Profile.Password; ElsIf Account.SMTPAuthentication = Enums.SMTPAuthenticationVariants.SetWithParameters Then If Account.SMTPAuthenticationMode = Enums.SMTPAuthenticationModes.CramMD5 Then Profile.SMTPAuthentication = SMTPAuthenticationMode.CramMD5; ElsIf Account.SMTPAuthenticationMode = Enums.SMTPAuthenticationModes.Login Then Profile.SMTPAuthentication = SMTPAuthenticationMode.Login; ElsIf Account.SMTPAuthenticationMode = Enums.SMTPAuthenticationModes.Plain Then Profile.SMTPAuthentication = SMTPAuthenticationMode.Plain; ElsIf Account.SMTPAuthenticationMode = Enums.SMTPAuthenticationModes.None Then Profile.SMTPAuthentication = SMTPAuthenticationMode.None; Else Profile.SMTPAuthentication = SMTPAuthenticationMode.Default; EndIf; Profile.SMTPUser = Account.SMTPUser; Profile.SMTPPassword = Account.SMTPPassword; ElsIf Account.SMTPAuthentication = Enums.SMTPAuthenticationVariants.POP3BeforeSMTP Then Profile.SMTPAuthentication = SMTPAuthenticationMode.None; Profile.POP3BeforeSMTP = True; Else Profile.SMTPAuthentication = SMTPAuthenticationMode.None; EndIf; EndIf; If GeneratePOP3Profile Then Profile.POP3ServerAddress = Account.IncomingMailServerPOP3; Profile.POP3Port = Account.POP3Port; If Account.POP3AuthenticationMode = Enums.POP3AuthenticationModes.APOP Then Profile.POP3Authentication = POP3AuthenticationMode.APOP; ElsIf Account.POP3AuthenticationMode = Enums.POP3AuthenticationModes.CramMD5 Then Profile.POP3Authentication = POP3AuthenticationMode.CramMD5; Else Profile.POP3Authentication = POP3AuthenticationMode.General; EndIf; EndIf; // Passing Use SSL flags to the profile settings Profile.POP3UseSSL = Account.POP3UseSSL; Profile.SMTPUseSSL = Account.SMTPUseSSL; // SSL Return Profile; EndFunction
Listing 4

Now you can select the POP3 requires SSL and SMTP requires SSL checkboxes when you need to send or receive email messages using your Gmail account (see Fig. 3).

0250a2dd932fd6ca445b32f986f38753.png
Fig. 3. Checkboxes added by the POP3UseSSL and SMTPUseSSL


Be the first to know tips & trick on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.