Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added TLS support for SMTP

  • Loading branch information...
commit 2ac9928971ca8601d4f9d2d3f8f3d21b191ee36c 1 parent 1766cf2
@mronge mronge authored
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,8 +36,7 @@
/** Constants for fetching messages **/
-typedef enum
-{
+typedef enum {
CTFetchAttrDefaultsOnly = 0,
CTFetchAttrBodyStructure = 1 << 0,
CTFetchAttrEnvelope = 1 << 1,
@@ -45,8 +44,7 @@ typedef enum
/** 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) */
Please sign in to comment.
Something went wrong with that request. Please try again.