| 1 |
// |
| 2 |
// This code is public domain. Do whatever you want with it. |
| 3 |
// |
| 4 |
// This test application shows how to send a simple message |
| 5 |
// using the SMTP protocol. See the "define section" below |
| 6 |
// for various settings you can activate. |
| 7 |
// |
| 8 |
// *** PLEASE READ Pantomime/Documentation/README *** |
| 9 |
// |
| 10 |
// Author: Ludovic Marcotte <[email protected]> |
| 11 |
// |
| 12 |
|
| 13 |
#import <AppKit/AppKit.h> |
| 14 |
|
| 15 |
|
| 16 |
// |
| 17 |
// You can safely either #include or #import Pantomime headers. |
| 18 |
// Include/import Pantomime.h to have access to every class |
| 19 |
// or include/import them individually. |
| 20 |
// |
| 21 |
#import <Pantomime/Pantomime.h> |
| 22 |
|
| 23 |
// |
| 24 |
// Modify those defines to reflect your environment. |
| 25 |
// |
| 26 |
#define FROM_ADDRESS @"Ludovic Marcotte <[email protected]>" |
| 27 |
#define TO_ADDRESS @"Ludovic Marcotte <[email protected]>" |
| 28 |
|
| 29 |
#define SERVER_NAME @"smtp.gmail.com" |
| 30 |
#define SERVER_PORT 587 |
| 31 |
#define USE_SECURE 2 // 0 -> NO, 1 -> SSL (usually port 465), 2 -> TLS (usually port 25 or 587) |
| 32 |
#define USERNAME @"[email protected]" |
| 33 |
#define PASSWORD @"foobarbaz" |
| 34 |
#define MECHANISM @"PLAIN" // use "none" for no SMTP authentication |
| 35 |
// "PLAIN", "LOGIN" and "CRAM-MD5" are supported. |
| 36 |
|
| 37 |
// |
| 38 |
// How many messages would you like to send over the same connection? |
| 39 |
// |
| 40 |
static int number_of_messages = 3; |
| 41 |
|
| 42 |
// |
| 43 |
// Our class interface. |
| 44 |
// |
| 45 |
@interface SimpleSMTP : NSObject |
| 46 |
{ |
| 47 |
@private |
| 48 |
CWSMTP *_smtp; |
| 49 |
} |
| 50 |
@end |
| 51 |
|
| 52 |
|
| 53 |
// |
| 54 |
// Our class implementation. |
| 55 |
// |
| 56 |
@implementation SimpleSMTP |
| 57 |
|
| 58 |
- (void) applicationDidFinishLaunching: (NSNotification *) theNotification |
| 59 |
{ |
| 60 |
CWInternetAddress *address; |
| 61 |
CWMessage *message; |
| 62 |
|
| 63 |
// We create a simple message object. |
| 64 |
message = [[CWMessage alloc] init]; |
| 65 |
[message setSubject: @"Pantomime Test!"]; |
| 66 |
|
| 67 |
// We set the "From:" header |
| 68 |
address = [[CWInternetAddress alloc] initWithString: FROM_ADDRESS]; |
| 69 |
[message setFrom: address]; |
| 70 |
RELEASE(address); |
| 71 |
|
| 72 |
// We set the "To: header |
| 73 |
address = [[CWInternetAddress alloc] initWithString: TO_ADDRESS]; |
| 74 |
[address setType: PantomimeToRecipient]; |
| 75 |
[message addRecipient: address]; |
| 76 |
RELEASE(address); |
| 77 |
|
| 78 |
// We set the Message's Content-Type, encoding and charset |
| 79 |
[message setContentType: @"text/plain"]; |
| 80 |
[message setContentTransferEncoding: PantomimeEncodingNone]; |
| 81 |
[message setCharset: @"us-ascii"]; |
| 82 |
|
| 83 |
// We set the Message's content |
| 84 |
[message setContent: [@"This is a simple content." dataUsingEncoding: NSASCIIStringEncoding]]; |
| 85 |
|
| 86 |
// We initialize our SMTP instance |
| 87 |
_smtp = [[CWSMTP alloc] initWithName: SERVER_NAME port: SERVER_PORT]; |
| 88 |
[_smtp setDelegate: self]; |
| 89 |
[_smtp setMessage: message]; |
| 90 |
RELEASE(message); |
| 91 |
|
| 92 |
// We connect to the server _in background_. That means, this call |
| 93 |
// is non-blocking and methods will be invoked on the delegate |
| 94 |
// (or notifications will be posted) for further dialog with |
| 95 |
// the remote SMTP server. |
| 96 |
NSLog(@"Connecting to the %@ server...", SERVER_NAME); |
| 97 |
[_smtp connectInBackgroundAndNotify]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
// |
| 102 |
// This method is automatically called once the SMTP authentication |
| 103 |
// has completed. If it has failed, -authenticationFailed: will |
| 104 |
// be invoked. |
| 105 |
// |
| 106 |
- (void) authenticationCompleted: (NSNotification *) theNotification |
| 107 |
{ |
| 108 |
NSLog(@"Authentication completed! Sending the message..."); |
| 109 |
[_smtp sendMessage]; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
// |
| 114 |
// This method is automatically called once the SMTP authentication |
| 115 |
// has failed. If it has succeeded, -authenticationCompleted: will |
| 116 |
// be invoked. |
| 117 |
// |
| 118 |
- (void) authenticationFailed: (NSNotification *) theNotification |
| 119 |
{ |
| 120 |
NSLog(@"Authentication failed! Closing the connection..."); |
| 121 |
[_smtp close]; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
// |
| 126 |
// This method is automatically called when the connection to |
| 127 |
// the SMTP server was established. |
| 128 |
// |
| 129 |
- (void) connectionEstablished: (NSNotification *) theNotification |
| 130 |
{ |
| 131 |
NSLog(@"Connected!"); |
| 132 |
|
| 133 |
if (USE_SECURE == 1) |
| 134 |
{ |
| 135 |
NSLog(@"Now starting SSL..."); |
| 136 |
[(CWTCPConnection *)[_smtp connection] startSSL]; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
// |
| 142 |
// This method is automatically called when the connection to the |
| 143 |
// server is abruptly closed by the server. GMail does that for example |
| 144 |
// when you call close. You should make sure your message is sent |
| 145 |
// in this delegate and not assume it only happens when after you |
| 146 |
// invoke -close. |
| 147 |
// |
| 148 |
- (void) connectionLost: (NSNotification *) theNotification |
| 149 |
{ |
| 150 |
NSLog(@"Connection lost to the server!"); |
| 151 |
RELEASE(_smtp); |
| 152 |
[NSApp terminate: self]; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
// |
| 157 |
// This method is automatically called when the connection to |
| 158 |
// the SMTP server was terminated avec invoking -close on the |
| 159 |
// SMTP instance. |
| 160 |
// |
| 161 |
- (void) connectionTerminated: (NSNotification *) theNotification |
| 162 |
{ |
| 163 |
NSLog(@"Connection closed."); |
| 164 |
RELEASE(_smtp); |
| 165 |
[NSApp terminate: self]; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
// |
| 170 |
// This method is automatically called when the message has been |
| 171 |
// successfully sent. |
| 172 |
// |
| 173 |
- (void) messageSent: (NSNotification *) theNotification |
| 174 |
{ |
| 175 |
NSLog(@"Sent!\nClosing the connection."); |
| 176 |
|
| 177 |
number_of_messages--; |
| 178 |
|
| 179 |
if (number_of_messages == 0) |
| 180 |
{ |
| 181 |
[_smtp close]; |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
// If you wish to either change the recipients, message or |
| 186 |
// the message data, you should do so in -transactionResetCompleted: |
| 187 |
// before calling again -sendMessage. The -reset method will NOT |
| 188 |
// change either the previously used recipients, message or |
| 189 |
// message data. |
| 190 |
[_smtp reset]; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// |
| 195 |
// This method is automatically invoked once the SMTP service |
| 196 |
// is fully initialized. One can send a message directly (if no |
| 197 |
// SMTP authentication is required to relay the mail) or proceed |
| 198 |
// with the authentication if needed. |
| 199 |
// |
| 200 |
- (void) serviceInitialized: (NSNotification *) theNotification |
| 201 |
{ |
| 202 |
if (![(CWTCPConnection *)[[theNotification object] connection] isSSL] && USE_SECURE == 2) |
| 203 |
{ |
| 204 |
[[theNotification object] startTLS]; |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
if (USE_SECURE == 1) |
| 209 |
{ |
| 210 |
NSLog(@"SSL handshaking completed."); |
| 211 |
} |
| 212 |
|
| 213 |
if ([MECHANISM isEqualToString: @"none"]) |
| 214 |
{ |
| 215 |
NSLog(@"Sending the message..."); |
| 216 |
[_smtp sendMessage]; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
NSLog(@"Available authentication mechanisms: %@", [_smtp supportedMechanisms]); |
| 221 |
[_smtp authenticate: USERNAME password: PASSWORD mechanism: MECHANISM]; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// |
| 226 |
// This method is invoked once the transaction has been reset. This |
| 227 |
// can be useful if one when to send more than one message over |
| 228 |
// the same SMTP connection. |
| 229 |
// |
| 230 |
- (void) transactionResetCompleted: (NSNotification *) theNotification |
| 231 |
{ |
| 232 |
NSLog(@"Sending the message over the same connection..."); |
| 233 |
[_smtp sendMessage]; |
| 234 |
} |
| 235 |
|
| 236 |
@end |
| 237 |
|
| 238 |
|
| 239 |
// |
| 240 |
// Main entry point for the test application. |
| 241 |
// |
| 242 |
int main(int argc, const char *argv[], char *env[]) |
| 243 |
{ |
| 244 |
NSAutoreleasePool *pool; |
| 245 |
SimpleSMTP *o; |
| 246 |
|
| 247 |
pool = [[NSAutoreleasePool alloc] init]; |
| 248 |
o = [[SimpleSMTP alloc] init]; |
| 249 |
|
| 250 |
[NSApplication sharedApplication]; |
| 251 |
[NSApp setDelegate: o]; |
| 252 |
[NSApp run]; |
| 253 |
RELEASE(o); |
| 254 |
RELEASE(pool); |
| 255 |
|
| 256 |
return 0; |
| 257 |
|
| 258 |
} |