4 * PWLib application source file for emailtest
6 * Main program entry point.
8 * Copyright (c) 2004 Post Increment
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Post Increment
24 * Contributor(s): ______________________________________.
27 * Revision 1.1 2004/08/11 07:39:05 csoutheren
32 #include "precompile.h"
36 #include <ptlib/sockets.h>
37 #include <ptclib/inetmail.h>
39 PCREATE_PROCESS(Emailtest
);
41 Emailtest::Emailtest()
42 : PProcess("Post Increment", "emailtest", MAJOR_VERSION
, MINOR_VERSION
, BUILD_TYPE
, BUILD_NUMBER
)
46 void Emailtest::Main()
48 PArgList
& args
= GetArguments();
58 "o-output:" "-no-output."
59 "t-trace." "-no-trace."
64 PTrace::Initialise(args
.GetOptionCount('t'),
65 args
.HasOption('o') ? (const char *)args
.GetOptionString('o') : NULL
,
66 PTrace::Blocks
| PTrace::Timestamp
| PTrace::Thread
| PTrace::FileAndLine
);
69 PRFC822Channel
email(PRFC822Channel::Sending
);
71 PString to
= args
.GetOptionString("to");
72 PString from
= args
.GetOptionString("from");
74 email
.SetToAddress(to
);
75 email
.SetFromAddress(from
);
76 email
.SetSubject(args
.GetOptionString("re"));
78 PStringArray attachments
= args
.GetOptionString("attachment").Lines();
80 PString server
= args
.GetOptionString("server");
84 PTCPSocket
socket("smtp 25");
85 if (!socket
.Connect(server
)) {
86 PError
<< "error: could not connect to SMTP server " << server
<< endl
;
90 PSMTPClient smtpClient
;
91 if (!smtpClient
.Open(socket
)) {
92 PError
<< "error: could not open SMTP server " << server
<< endl
;
96 if (!email
.Open(smtpClient
)) {
97 PError
<< "error: cannot open email message " << server
<< endl
;
101 if (!smtpClient
.BeginMessage(from
, to
)) {
102 PError
<< "error: could not begin SMTP message " << smtpClient
.GetErrorText() << endl
;
107 if (attachments
.GetSize() > 0) {
108 boundary
= email
.MultipartMessage();
111 for (PINDEX i
= 0; i
< args
.GetCount(); ++i
) {
112 email
.Write((const char *)args
[i
], args
[i
].GetLength());
116 if (attachments
.GetSize() > 0) {
117 for (PINDEX i
= 0; i
< attachments
.GetSize(); ++i
) {
118 PFilePath filename
= attachments
[i
];
119 PFile
file(filename
, PFile::ReadOnly
);
121 email
.NextPart(boundary
);
122 email
.SetContentAttachment(filename
.GetFileName());
123 PString fileType
= filename
.GetType();
124 PString contentType
= PMIMEInfo::GetContentType(fileType
);
125 if ((fileType
*= "txt") || (fileType
== "html"))
126 email
.SetTransferEncoding("7bit", FALSE
);
128 email
.SetTransferEncoding("base64", TRUE
);
131 if (!file
.Read(buffer
, sizeof(buffer
)))
133 email
.Write(buffer
, file
.GetLastReadCount());
139 smtpClient
.EndMessage();
145 // End of File ///////////////////////////////////////////////////////////////