Added TLS support for SMTP

This commit is contained in:
Matt Ronge
2012-11-20 13:41:39 -06:00
parent 1766cf21d2
commit 2ac9928971
5 changed files with 54 additions and 17 deletions
+1
View File
@@ -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;
+10
View File
@@ -61,6 +61,16 @@
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]);
+18 -6
View File
@@ -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
+17 -7
View File
@@ -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 @@
goto error;
}
}
if (tls) {
if (connectionType == CTSMTPConnectionTypeStartTLS) {
success = [smtpObj startTLS];
if (!success) {
goto error;
@@ -107,13 +112,18 @@ error:
}
+ (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 @@ error:
goto error;
}
}
if (tls) {
if (connectionType == CTSMTPConnectionTypeStartTLS) {
success = [smtpObj startTLS];
if (!success) {
goto error;
+8 -4
View File
@@ -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) */