File: smtp_2.adb

package info (click to toggle)
libaws 20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: ada: 95,505; python: 2,270; ansic: 1,017; makefile: 829; xml: 235; javascript: 202; java: 112; sh: 106
file content (206 lines) | stat: -rw-r--r-- 6,665 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2008-2017, AdaCore                     --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

with Ada.Exceptions;
with Ada.Text_IO;

with AWS.Attachments;
with AWS.Headers;
with AWS.Messages;
with AWS.MIME;
with AWS.Net.SSL;
with AWS.SMTP.Client;
with AWS.SMTP.Server;

with SMTP_Pck;

with Get_Free_Port;

procedure SMTP_2 is

   use Ada;
   use Ada.Exceptions;
   use AWS;

   From_Name  : constant String := "My Name";
   From_Email : constant String := "my.name@righthere.fr";
   Filename   : constant String := "ada.gif";

   Port   : Positive := 9025;
   Family : Net.Family_Type;
   Host   : SMTP.Receiver;
   Server : SMTP.Server.Handle;
   Status : SMTP.Status;
   Attac  : Attachments.List;
   Alter  : Attachments.Alternatives;
   EOL    : constant String := ASCII.CR & ASCII.LF;

begin
   Get_Free_Port (Port);

   if Net.IPv6_Available then
      Family := Net.Family_Inet6;
   else
      Family := Net.Family_Inet;
   end if;

   Host := SMTP.Initialize
             (Net.Localhost (Net.IPv6_Available), Port,
              AWS.Net.SSL.Is_Supported, Family => Family, Timeout => 1.0);

   SMTP.Server.Start (Server, Host, SMTP_Pck.Dump_Mail'Access);

   --  Send simple message

   Text_IO.Put_Line ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1");

   SMTP.Client.Send
     (Host,
      From    => SMTP.E_Mail ("Pascal Obry", "obry@nowhere.org"),
      To      => SMTP.E_Mail ("John Doe", "john.doe@nothere.net"),
      Subject => "First message",
      Message => "First message body" & EOL & "." & EOL & ".al" & EOL & "..br",
      Status  => Status);

   SMTP_Pck.Callback.Wait; Text_IO.Flush;

   --  Send simple message, plus attachment

   Text_IO.Put_Line ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2");

   SMTP.Client.Send
     (Host,
      From        => SMTP.E_Mail (From_Name, From_Email),
      To          => (1 => SMTP.E_Mail ("Pascal Obry", "aws@obry.net")),
      Subject     => "Sending e-mail + attachment from Ada code",
      Message     => "Thanks to AWS/SMTP, it's easy !",
      Attachments => (1 => SMTP.Client.File (Filename)),
      Status      => Status);

   SMTP_Pck.Callback.Wait; Text_IO.Flush;

   --  Send alternative messages plus attachment

   Text_IO.Put_Line ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3");

   Attachments.Add
     (Alter,
      Attachments.Value
        ("this is the default plain text",
         Content_Type => MIME.Text_Plain));
   Attachments.Add
     (Alter,
      AWS.Attachments.Value
        ("<p>this is the default <i>HTML</i> text",
         Content_Type => MIME.Text_HTML));

   Attachments.Add (Attac, Alter);

   SMTP.Client.Send
     (Host,
      From        => SMTP.E_Mail (From_Name, From_Email),
      To          => (1 => SMTP.E_Mail ("Pascal Obry", "aws@obry.net")),
      Subject     => "Sending alternative parts e-mail from Ada code",
      Attachments => Attac,
      Status      => Status);

   SMTP_Pck.Callback.Wait; Text_IO.Flush;

   --  Send alternative messages plus attachment

   Text_IO.Put_Line ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 4");

   Attachments.Add
     (Attac,
      Filename   => Filename,
      Content_Id => "ada.gif",
      Encode     => Attachments.Base64);

   SMTP.Client.Send
     (Host,
      From        => SMTP.E_Mail (From_Name, From_Email),
      To          => (1 => SMTP.E_Mail ("Pascal Obry", "aws@obry.net")),
      Subject     =>
        "Sending alternative parts e-mail plus attachment from Ada code",
      Attachments => Attac,
      Status      => Status);

   SMTP_Pck.Callback.Wait; Text_IO.Flush;

   --  Check multiple Content_Type, ensure the one in Data is used

   declare
      CT_Attac   : Attachments.List;
      CT_Headers : Headers.List;
   begin
      CT_Headers.Add (Messages.Content_Type_Token, MIME.Text_Plain);

      Attachments.Add
        (CT_Attac,
         Name => "utf8",
         Data => Attachments.Value
           ("this is the default plain text UTF-8",
            Encode       => Attachments.Base64,
            Content_Type => MIME.Text_Plain & "; charset=UTF-8"),
         Headers => CT_Headers);

      SMTP.Client.Send
        (Host,
         From        => SMTP.E_Mail (From_Name, From_Email),
         To          => (1 => SMTP.E_Mail ("Pascal Obry", "aws@obry.net")),
         Subject     => "Sending an UTF-8 attachement from Ada code",
         Attachments => CT_Attac,
         Status      => Status);

      SMTP_Pck.Callback.Wait; Text_IO.Flush;
   end;

   --  Check multiple Content_Transfer_Encoding, ensure the one in Data is
   --  used.

   declare
      CTE_Attac   : Attachments.List;
      CTE_Headers : Headers.List;
   begin
      CTE_Headers.Add (Messages.Content_Transfer_Encoding_Token, "8bits");

      Attachments.Add
        (CTE_Attac,
         Name => "8bits",
         Data => Attachments.Value
           ("this is the default plain text UTF-8",
            Encode => Attachments.Base64),
         Headers => CTE_Headers);

      SMTP.Client.Send
        (Host,
         From        => SMTP.E_Mail (From_Name, From_Email),
         To          => (1 => SMTP.E_Mail ("Pascal Obry", "aws@obry.net")),
         Subject     => "Sending an 8bits-8 attachement from Ada code",
         Attachments => CTE_Attac,
         Status      => Status);

      SMTP_Pck.Callback.Wait; Text_IO.Flush;
   end;

   SMTP.Server.Shutdown (Server);

exception
   when E : others =>
      Text_IO.Put_Line ("SMTP_2 exception:" & Exception_Information (E));
end SMTP_2;