//
//  Send.m
//  New PressRelease
//
//  Created by Newtok on 11/13/08.
//  Copyright 2008 company. All rights reserved.
//

#import "Send.h"
#import "Pantomime/CWTransport.h"

@implementation Send
- (IBAction) SendTestMail: (id) sender
{
	[NSThread detachNewThreadSelector:@selector(sendEmail:) toTarget:self withObject:nil];
}
- (void) sendEmail: (NSConnection *) connection
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
	CWInternetAddress *address;
	CWMessage *message;
	
	
	message = [[CWMessage alloc] init];
	[message setSubject:[self subjectField]];
	address = [[CWInternetAddress alloc] initWithString:[self fromField]];
	[message setFrom: address];
	RELEASE(address);
	NSData * data = [[self toField]dataUsingEncoding:NSASCIIStringEncoding];
	//Parse the to addres field using ','  
	[CWParser parseDestination: data
					   forType: PantomimeToRecipient
					 inMessage: message
						 quick: YES];
	NSString * mcontents = [self messageField];
	NSData* aData;				
	aData = [mcontents dataUsingEncoding: NSASCIIStringEncoding];
	[message setContentDisposition: PantomimeInlineDisposition];
	[message setContentType: @"text/plain"];
	[message setContent:aData];
	_smtp = [[CWSMTP alloc] initWithName: serverName  port: portnum];
	[_smtp setDelegate: self];
	[_smtp setMessage: message];
	RELEASE(message);
	
	//from here it automatically call the connection established method.
	[_smtp connectInBackgroundAndNotify];
	RELEASE(pool);
}
- (void) connectionEstablished: (NSNotification *) theNotification
{
	
	NSLog(@"Connection Established");
	//Automatically calling the serviceInitialized method.
}

- (void) connectionTerminated: (NSNotification *) theNotification
{
	//Stoping the progress bar.releasing connection and application.
	NSLog(@"Connection Terminated");
	RELEASE(_smtp);
	
}

- (void) messageSent: (NSNotification *) theNotification
{
	//Terminating the connectoin.By calling Connection Termainated Method.
	[_smtp close];
}

- (void) serviceInitialized: (NSNotification *) theNotification
{
	if ([MECHANISM isEqualToString: @"none"])
    {
		//Calling the messageSent method,
		[_smtp sendMessage];
	}
}

	
@end
