Permalink
Browse files

Merge branch 'smtp_tls'

  • Loading branch information...
2 parents 405965d + 2ac9928 commit 3449724ef51fd2cd0c69fd0bc2375070b590bdf7 @mronge mronge committed Nov 25, 2012
Showing with 54 additions and 17 deletions.
  1. +1 −0 Source/CTSMTP.h
  2. +10 −0 Source/CTSMTP.m
  3. +18 −6 Source/CTSMTPConnection.h
  4. +17 −7 Source/CTSMTPConnection.m
  5. +8 −4 Source/MailCoreTypes.h
View
1 Source/CTSMTP.h
@@ -48,6 +48,7 @@
- (id)initWithResource:(mailsmtp *)smtp;
- (BOOL)connectToServer:(NSString *)server port:(unsigned int)port;
+- (BOOL)connectWithTlsToServer:(NSString *)server port:(unsigned int)port;
- (BOOL)helo;
- (BOOL)startTLS;
- (BOOL)authenticateWithUsername:(NSString *)username password:(NSString *)password server:(NSString *)server;
View
10 Source/CTSMTP.m
@@ -61,6 +61,16 @@ - (BOOL)connectToServer:(NSString *)server port:(unsigned int)port {
return YES;
}
+- (BOOL)connectWithTlsToServer:(NSString *)server port:(unsigned int)port {
+ /* first open the stream */
+ int ret = mailsmtp_ssl_connect([self resource], [server cStringUsingEncoding:NSUTF8StringEncoding], port);
+ if (ret != MAIL_NO_ERROR) {
+ self.lastError = MailCoreCreateErrorFromSMTPCode(ret);
+ return NO;
+ }
+ return YES;
+}
+
- (BOOL)helo {
/* The server doesn't support esmtp, so try regular smtp */
int ret = mailsmtp_helo([self resource]);
View
24 Source/CTSMTPConnection.h
@@ -30,6 +30,7 @@
*/
#import <Foundation/Foundation.h>
+#import "MailCoreTypes.h"
/**
This is not a class you instantiate! It has only two class methods, and that is all you need to send e-mail.
@@ -49,13 +50,19 @@
@param username The username, if there is none then pass in an empty string. For some servers you may have to specify the username as username@domain
@param password The password, if there is none then pass in an empty string.
@param port The port to use, the standard port is 25
- @param tls Pass in YES, if you want to use SSL/TLS
+ @param connectionType What kind of connection, either: CTSMTPConnectionTypePlain, CTSMTPConnectionTypeStartTLS, CTSMTPConnectionTypeTLS
@param auth Pass in YES if you would like to use SASL authentication
@param error Will contain an error when the method returns NO
@return Returns YES on success, NO on error
*/
-+ (BOOL)sendMessage:(CTCoreMessage *)message server:(NSString *)server username:(NSString *)username
- password:(NSString *)password port:(unsigned int)port useTLS:(BOOL)tls useAuth:(BOOL)auth error:(NSError **)error;
++ (BOOL)sendMessage:(CTCoreMessage *)message
+ server:(NSString *)server
+ username:(NSString *)username
+ password:(NSString *)password
+ port:(unsigned int)port
+ connectionType:(CTSMTPConnectionType)connectionType
+ useAuth:(BOOL)auth
+ error:(NSError **)error;
/**
Use this method to test the user's credentials.
@@ -65,12 +72,17 @@
@param username The username, if there is none then pass in an empty string. For some servers you may have to specify the username as username@domain
@param password The password, if there is none then pass in an empty string.
@param port The port to use, the standard port is 25
- @param tls Pass in YES, if you want to use SSL/TLS
+ @param connectionType What kind of connection, either: CTSMTPConnectionTypePlain, CTSMTPConnectionTypeStartTLS, CTSMTPConnectionTypeTLS
@param auth Pass in YES if you would like to use SASL authentication
@param error Will contain an error when the method returns NO
@return Returns YES on success, NO on error
*/
-+ (BOOL)canConnectToServer:(NSString *)server username:(NSString *)username password:(NSString *)password
- port:(unsigned int)port useTLS:(BOOL)tls useAuth:(BOOL)auth error:(NSError **)error;
++ (BOOL)canConnectToServer:(NSString *)server
+ username:(NSString *)username
+ password:(NSString *)password
+ port:(unsigned int)port
+ connectionType:(CTSMTPConnectionType)connectionType
+ useAuth:(BOOL)auth
+ error:(NSError **)error;
@end
View
24 Source/CTSMTPConnection.m
@@ -41,13 +41,18 @@
//TODO Add more descriptive error messages using mailsmtp_strerror
@implementation CTSMTPConnection
+ (BOOL)sendMessage:(CTCoreMessage *)message server:(NSString *)server username:(NSString *)username
- password:(NSString *)password port:(unsigned int)port useTLS:(BOOL)tls useAuth:(BOOL)auth error:(NSError **)error {
+ password:(NSString *)password port:(unsigned int)port connectionType:(CTSMTPConnectionType)connectionType
+ useAuth:(BOOL)auth error:(NSError **)error {
BOOL success;
mailsmtp *smtp = NULL;
smtp = mailsmtp_new(0, NULL);
CTSMTP *smtpObj = [[CTESMTP alloc] initWithResource:smtp];
- success = [smtpObj connectToServer:server port:port];
+ if (connectionType == CTSMTPConnectionTypeStartTLS || connectionType == CTSMTPConnectionTypePlain) {
+ success = [smtpObj connectToServer:server port:port];
+ } else if (connectionType == CTSMTPConnectionTypeTLS) {
+ success = [smtpObj connectWithTlsToServer:server port:port];
+ }
if (!success) {
goto error;
}
@@ -60,7 +65,7 @@ + (BOOL)sendMessage:(CTCoreMessage *)message server:(NSString *)server username:
goto error;
}
}
- if (tls) {
+ if (connectionType == CTSMTPConnectionTypeStartTLS) {
success = [smtpObj startTLS];
if (!success) {
goto error;
@@ -107,13 +112,18 @@ + (BOOL)sendMessage:(CTCoreMessage *)message server:(NSString *)server username:
}
+ (BOOL)canConnectToServer:(NSString *)server username:(NSString *)username password:(NSString *)password
- port:(unsigned int)port useTLS:(BOOL)tls useAuth:(BOOL)auth error:(NSError **)error {
+ port:(unsigned int)port connectionType:(CTSMTPConnectionType)connectionType
+ useAuth:(BOOL)auth error:(NSError **)error {
BOOL success;
mailsmtp *smtp = NULL;
smtp = mailsmtp_new(0, NULL);
-
+
CTSMTP *smtpObj = [[CTESMTP alloc] initWithResource:smtp];
- success = [smtpObj connectToServer:server port:port];
+ if (connectionType == CTSMTPConnectionTypeStartTLS || connectionType == CTSMTPConnectionTypePlain) {
+ success = [smtpObj connectToServer:server port:port];
+ } else if (connectionType == CTSMTPConnectionTypeTLS) {
+ success = [smtpObj connectWithTlsToServer:server port:port];
+ }
if (!success) {
goto error;
}
@@ -126,7 +136,7 @@ + (BOOL)canConnectToServer:(NSString *)server username:(NSString *)username pass
goto error;
}
}
- if (tls) {
+ if (connectionType == CTSMTPConnectionTypeStartTLS) {
success = [smtpObj startTLS];
if (!success) {
goto error;
View
12 Source/MailCoreTypes.h
@@ -36,17 +36,15 @@
/** Constants for fetching messages **/
-typedef enum
-{
+typedef enum {
CTFetchAttrDefaultsOnly = 0,
CTFetchAttrBodyStructure = 1 << 0,
CTFetchAttrEnvelope = 1 << 1,
} CTFetchAttributes;
/** Constants for IDLE **/
-typedef enum
-{
+typedef enum {
CTIdleNewData = 0,
CTIdleTimeout,
CTIdleCancelled,
@@ -55,6 +53,12 @@ typedef enum
/** Connection Constants **/
+typedef enum {
+ CTSMTPConnectionTypePlain = 0,
+ CTSMTPConnectionTypeStartTLS,
+ CTSMTPConnectionTypeTLS
+} CTSMTPConnectionType;
+
/* when the connection is plain text */
#define CTConnectionTypePlain CONNECTION_TYPE_PLAIN
/* when the connection is first plain, then, we want to switch to TLS (secure connection) */

0 comments on commit 3449724

Please sign in to comment.