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
207
208
| procedure TForm_Pochta.Button05Click(Sender: TObject);
var
a, i, n, OT_Count: Integer;
MessageText1,OTaddr,OTpath,OTattdir,HTML_Text,Logotup: String;
att01: TIdAttachmentFile;
HTML_Text_Lines: array[0..MAX_PATH] of String;
id: String;
ListBox_OTpath: TListBox;
begin
//Общие настройки
OTaddr := ExtractFilePath(Application.ExeName); // получения адреса папки, в которой находиться наша программа
Logotup := OTaddr+'Logotip.jpg';
//Сохранение отправленных сообщений
Pocht_yashik := Panel6.Caption;
Pocht_yashik := OTaddr+'Mail\'+Pocht_yashik;
if not DirectoryExists(Pocht_yashik) then
CreateDir(Pocht_yashik);
OTpath := Pocht_yashik+'\Otprav_Pochta';
if not DirectoryExists(OTpath) then
CreateDir(OTpath);
OTattdir := OTpath+'\Otprav_Pochta_files';
if DirectoryExists(OTattdir) then //Если папка создана, удаляю папку с файлами, для экономии места на диске
TDirectory.Delete(OTattdir, True);
if not DirectoryExists(OTattdir) then
CreateDir(OTattdir);//Создать папку для вложений, path: Mail/Pocht_yashik/Otprav_Pochta/Otprav_Pochta_files
OTattdir := OTpath+'\Otprav_Pochta_files';
{настройка компонентов перед отправкой}
IdSMTP01 := TIdSMTP.Create(nil);
with IdSMTP01 do
begin
CreateIOHandler(nil);
AuthType := satDefault; // тип подключения к серверу – с авторизацией
Host := DataModule0.FDQuery_PochtaParametr.FieldByName('AdresSMTPserver').AsString; // адрес почтового сервера
Port := DataModule0.FDQuery_PochtaParametr.FieldByName('PortSMTP').AsInteger; // порт по которому будет осуществляться подключение
Username := DataModule0.FDQuery_PochtaParametr.FieldByName('E_mail').AsString; // логин пользователя
Password := DataModule0.FDQuery_NashiSotrudniki.FieldByName('Parol_Email').AsString; // пароль пользователя
end;
{это необходимо использовать для SSL}
if DataModule0.FDQuery_PochtaParametr.FieldByName('SSL_SMTP').AsBoolean = True then
begin
IdSSLIOHandlerSocketOpenSSL01:= TIdSSLIOHandlerSocketOpenSSL.Create(nil);
with IdSSLIOHandlerSocketOpenSSL01 do
begin
Destination := IdSMTP01.Host+':'+IntToStr(IdSMTP01.Port);
Host := IdSMTP01.Host;
Port := IdSMTP01.Port;
DefaultPort := 0;
SSLOptions.Method := sslvTLSv1;
//SSLOptions.Method := sslvSSLv2;
SSLOptions.Mode := sslmUnassigned;
end;
IdSMTP01.IOHandler := IdSSLIOHandlerSocketOpenSSL01;
IdSMTP01.UseTLS := utUseImplicitTLS;
end;
IdMessage01 := TIdMessage.Create(nil);
with IdMessage01 do
begin
Subject := (Edit011.Text); //Тема сообщения
From.Address := DataModule0.FDQuery_PochtaParametr.FieldByName('E_mail').AsString; //Адрес отправителя
Recipients.EMailAddresses := Edit010.Text; //Кому отправить письмо (можно через запятую если несколько e-mail'ов)
Date := Now;
Encoding := meMIME;
//Отправка пустого сообщения без вложений
Body.Text := (RichEdit01.Text);
if (Body.Text = '') and (ListView02 = nil) then
begin
with TIdText.Create(MessageParts, nil) do
begin
Body.Text := (RichEdit01.Text);
ContentType := 'text/plain'; CharSet := 'windows-1251';
end;
//ContentType := 'multipart/alternative';
end;
//Отправка сообщения без вложений
if (Body.Text <> '') and (ListView02 = nil) then
begin
with TIdText.Create(MessageParts, nil) do
begin
for a := 0 to RichEdit01.Lines.Count do
begin
HTML_Text_Lines[a] := RichEdit01.Lines[a];
Insert('<p>', HTML_Text_Lines[a], 1);
n := Length(HTML_Text_Lines[a]);
Insert('</p>', HTML_Text_Lines[a], n+1);
HTML_Text := HTML_Text + HTML_Text_Lines[a];
end;
Insert('<div style=”line-height:0.9em;”><body>', HTML_Text, 1);
n := Length(HTML_Text);
Insert('</body>', HTML_Text, n+1);
Insert('<html>', HTML_Text, 1);
n := Length(HTML_Text);
Insert('</html>', HTML_Text, n+1);
Body.Text := HTML_Text;
ContentType := 'text/html'; CharSet := 'windows-1251';
end;
ContentType := 'multipart/alternative';
end;
//Отправка сообщения в формате HTML и связанные с ним вложения (логотип или еще что-либо)
if (Body.Text <> '') and (ListView02 <> nil) then
begin
with TIdText.Create(MessageParts, nil) do
begin
ContentType := 'multipart/related; type="multipart/alternative"';
end;
with TIdText.Create(MessageParts, nil) do
begin
ContentType := 'multipart/alternative';
ParentPart := 0;
end;
with TIdText.Create(MessageParts, nil) do
begin
for a := 0 to RichEdit01.Lines.Count do
begin
HTML_Text_Lines[a] := RichEdit01.Lines[a];
Insert('<p>', HTML_Text_Lines[a], 1);
n := Length(HTML_Text_Lines[a]);
Insert('</p>', HTML_Text_Lines[a], n+1);
HTML_Text := HTML_Text + HTML_Text_Lines[a];
end;
Insert('<div style=”line-height:0.9em;”><body>', HTML_Text, 1);
n := Length(HTML_Text);
Insert('</body><img src="cid:12345">', HTML_Text, n+1);
Insert('<html>', HTML_Text, 1);
n := Length(HTML_Text);
Insert('</html>', HTML_Text, n+1);
Body.Text := HTML_Text;
ContentType := 'text/html'; CharSet := 'windows-1251';
ParentPart := 1;
end;
with TIdAttachmentFile.Create(MessageParts, Logotup) do
begin
ContentID := '12345';
ContentType := 'image/jpeg';
ParentPart := 0;
end;
ContentType := 'multipart/related; type="text/html"';
for i := 0 to ListView02.Items.Count-1 do
begin
with TIdAttachmentFile.Create(MessageParts, ListView02.Items.Item[i].SubItems.Text) do
begin
with ListView02.Items[i] do
begin
TIdAttachmentFile.Create(MessageParts, ListView02.Items.Item[i].SubItems.Text);
ContentType := 'application/pdf';
FileName := 'IndyInDepth.pdf';
end;
end;
end;
ContentType := 'multipart/mixed';
end;
end;
{Сохранение письма в папке в которой находиться программа}
id := IdMessage01.MsgId;
id := THashMD5.GetHashString(id);
id := IdMessage01.From.Address+'-'+id;
IdMessage01.SaveToFile(OTpath+'\'+id+'.eml');
ListBox_OTpath := TListBox.Create(Form_Logistika);
with ListBox_OTpath do
begin
Parent := Form_Logistika;
Visible := False;
//Перенос почтовых файлов
SendMessage(ListBox_OTpath.Handle,LB_DIR,0,integer(pchar(OTpath+'\*.eml')));
//Узнаю количество писем
OT_Count := Items.Count;
Label6.Caption := 'Отправленных: '+IntToStr(OT_Count);
end;
FreeAndNil(ListBox_OTpath);
{отправляю письмо}
IdSMTP01.Connect();
if IdSMTP01.Connected then
try
IdSMTP01.Send(IdMessage01);
Application.ProcessMessages;
finally
IdSMTP01.Disconnect;
IdMessage01.Free;
IdSSLIOHandlerSocketOpenSSL01.Free;
IdSMTP01.Free;
Form_NovoeSoobshenie.Close;
ShowMessage('Сообщение отправлено');
end;
end; |