Objective-C
Objective-C
Verify Email Recipients Without Sending
See more SMTP Examples
Demonstrates the Chilkat MailMan.VerifyRecips method, which begins the SMTP send process for an email, sends the recipient addresses to the SMTP server, and then aborts before sending the message content. Recipient addresses rejected by the server are returned in the StringArray. This example checks two recipients and lists any that were rejected.
Background: During an SMTP transaction each recipient is offered with a
RCPT TO command, and the server accepts or rejects it before any message content is transferred. VerifyRecips exploits that: it runs the transaction just far enough to collect those verdicts, then aborts — letting you validate an address list without actually delivering mail. Note that many servers accept everything at this stage and bounce later, so a clean result is not a guarantee of deliverability.Chilkat Objective-C Downloads
#import <CkoMailMan.h>
#import <CkoEmail.h>
#import <CkoStringArray.h>
BOOL success = NO;
// Demonstrates the MailMan.VerifyRecips method, which begins the SMTP send process for an
// email, sends the recipient addresses to the SMTP server, and then aborts before sending
// the message content. Recipient addresses rejected by the server are returned in the
// StringArray.
CkoMailMan *mailman = [[CkoMailMan alloc] init];
// Configure the SMTP server connection.
mailman.SmtpHost = @"smtp.example.com";
mailman.SmtpPort = [NSNumber numberWithInt:465];
mailman.SmtpSsl = YES;
mailman.SmtpUsername = @"user@example.com";
mailman.SmtpPassword = @"myPassword";
// Build an email with the recipients to verify.
CkoEmail *email = [[CkoEmail alloc] init];
email.Subject = @"Recipient check";
email.From = @"alice@example.com";
[email AddTo: @"Bob" emailAddress: @"bob@example.com"];
[email AddTo: @"Nobody" emailAddress: @"nobody@example.com"];
// Ask the server which recipients it will accept. Rejected addresses are returned.
CkoStringArray *badAddrs = [[CkoStringArray alloc] init];
success = [mailman VerifyRecips: email saBadAddrs: badAddrs];
if (success == NO) {
NSLog(@"%@",mailman.LastErrorText);
return;
}
int n = [badAddrs.Count intValue];
NSLog(@"%@%d",@"Rejected recipients: ",n);
int i;
for (i = 0; i <= n - 1; i++) {
NSLog(@"%@",[badAddrs GetString: [NSNumber numberWithInt: i]]);
}
// Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
// connects and authenticates -- using the property settings above -- whenever a server
// operation requires it. Calling the explicit connect/authenticate methods can still be
// helpful to determine whether a failure occurs while connecting or while authenticating.