Added trace log at level 6 for helping find PSafeObject reference/dereference errors.
[pwlib.git] / samples / emailtest / main.cxx
blob68c3c64725c0c43e8c3a3b3c75b758dc764282fa
1 /*
2 * main.cxx
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
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Post Increment
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.1 2004/08/11 07:39:05 csoutheren
28 * Initial version
32 #include "precompile.h"
33 #include "main.h"
34 #include "version.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();
50 args.Parse(
51 "-server:"
52 "-to:"
53 "-from:"
54 "-re:"
55 "-attachment:"
57 #if PTRACING
58 "o-output:" "-no-output."
59 "t-trace." "-no-trace."
60 #endif
63 #if PTRACING
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);
67 #endif
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");
81 if (server.IsEmpty())
82 server = "127.0.0.1";
84 PTCPSocket socket("smtp 25");
85 if (!socket.Connect(server)) {
86 PError << "error: could not connect to SMTP server " << server << endl;
87 return;
90 PSMTPClient smtpClient;
91 if (!smtpClient.Open(socket)) {
92 PError << "error: could not open SMTP server " << server << endl;
93 return;
96 if (!email.Open(smtpClient)) {
97 PError << "error: cannot open email message " << server << endl;
98 return;
101 if (!smtpClient.BeginMessage(from, to)) {
102 PError << "error: could not begin SMTP message " << smtpClient.GetErrorText() << endl;
103 return;
106 PString boundary;
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());
113 email << "\n";
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);
120 if (file.IsOpen()) {
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);
127 else
128 email.SetTransferEncoding("base64", TRUE);
129 BYTE buffer[1024];
130 for (;;) {
131 if (!file.Read(buffer, sizeof(buffer)))
132 break;
133 email.Write(buffer, file.GetLastReadCount());
139 smtpClient.EndMessage();
141 email.Close();
145 // End of File ///////////////////////////////////////////////////////////////