Sending reports by email from the 1C platform

Alexander Biryukov

11.03.2020 7 min

Earlier, we created two reports (InventoryList and ProductList) on the 1C platform. In this example, we used the built-in mechanism, the Data composition system for developing reports without writing a single line of code in our solution.

In practice, our system often requires functions when it is necessary to send the generated reports to our contractors or employees by email. Let’s now realize this feature, and this time we need to write some code.

Open the external report InventoryList.erf in the Designer. Add a form for this report (pictures 1 and 2):

1.png

2.png

Now open this form and add the new attribute ToMail to it. This attribute will contain the email address of the recipient. The type of the new attribute is String.

Immediately place a new attribute on the form (picture 3):

3.png

Then create a new form command Send. Similarly, put it on the form (picture 4) and write the program code for this command (Listing 1):

4.png

Listing 1.

&AtClient

Procedure Send(Command)

      stringTempFileName = GetTempFileName("xls");

      Result.Write(stringTempFileName,SpreadsheetDocumentFileType.XLS);

      EmailProfile = New InternetMailProfile;

      EmailProfile.SMTPServerAddress     = "your SMTP server"; //

      EmailProfile.SMTPUser              = "your email login"; //

      EmailProfile.SMTPPassword          = "your password";

      EmailProfile.SMTPUseSSL            = True;

      EmailProfile.SMTPPort              = 465;

      EmailProfile.SMTPAuthentication = SMTPAuthenticationMode.Login;

      EMail = New InternetMailMessage;

      Text                    = EMail.Texts.Add("Hello World!");

      Text.TextType           = InternetMailTextType.PlainText;

      EMail.Subject           = "Subject this message";

      EMail.From              = "your email address";

      EMail.SenderName = "From J.B.";

      EMail.To.Add(ToMail);

      EMail.Attachments.Add(stringTempFileName);

      Mail = New InternetMail;

      Try

            Mail.Logon(EmailProfile);

            Message("Connection established.");

      Except

            Message("Connection not established. Error: " + ErrorDescription());

             Return;

      EndTry;

      Try

            Mail.Send(EMail);

            Message("Message sent.");

      Except

            Message("Message not sent. Error: " + ErrorDescription());

            Return;

      EndTry;

      Mail.Logoff();

EndProcedure

Briefly understand how the program code works.

First, we save the report results to a file in Excel format:

      stringTempFileName = GetTempFileName("xls");

    Result.Write(stringTempFileName,SpreadsheetDocumentFileType.XLS);

Next, the objects “InternetMailProfile”, “InternetMailMessage” and “InternetMail” are created sequentially, and the properties of these objects are filled...

Don’t forget to add the report recipient:

EMail.To.Add(ToMail);

and add the previously generated Excel file with our report:

EMail.Attachments.Add(stringTempFileName);

After that, an attempt is made to connect to the mail server:

Mail.Logon(EmailProfile);

If the connection is successful, then an attempt is made to send an email:

Mail.Send(EMail);

Finally, close the connection to the mail server:

Mail.Logoff();

And that is all! See how easy it is!

Let’s now check how our code works. Let’s launch the 1C platform in the dialogue mode, open the external report InventoryList.erf, generate a report - for this, click the Run report button.

Now, in the field To mail, please enter the email address of the recipient of our report and click the Send button. If you have correctly specified the parameters of the SMTP server, the email with the report is sent at this moment (picture 5):

5.png

This is what the mail already came to the addressee looks like (picture 6):

6.png

As you can see - using the 1C platform, it’s effortless to send any data by email.

You can download these reports for your own application.

Please, supplement your suggestions for the development of demo applications in our forum.

Stay with us - it will be more interesting further!

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.