开发者

Send email to multiple recipients with SKPSMTPMessage?

开发者 https://www.devze.com 2023-03-01 02:46 出处:网络
I need to send email in background, so I have to use the library named: SMTP. And the main class I used is: SKPSMTPMessage. The problem is \"ccEmail\", when I add more than 2 recipients, it can\'t sen

I need to send email in background, so I have to use the library named: SMTP. And the main class I used is: SKPSMTPMessage. The problem is "ccEmail", when I add more than 2 recipients, it can't send email. (that takes too long time to go to delegate methods). It works well with recipient <= 2.

smtpEmail.ccEmail = @"xyz@gmail.com, xyz1@gmail.com, xyz2@gmail.com";   开发者_开发问答

Anyone knows this, please help me. Thanks you so much !


There is my changes in the parseBuffer function:

case kSKPSMTPWaitingFromReply:
    {
        if ([tmpLine hasPrefix:@"250 "]) {

            if (!multipleRcptTo) {
                NSMutableString *multipleRcptToString = [NSMutableString string];
                [multipleRcptToString appendString:[self formatAddresses:toEmail]];
                [multipleRcptToString appendString:[self formatAddresses:ccEmail]];
                [multipleRcptToString appendString:[self formatAddresses:bccEmail]];

                multipleRcptTo = [[multipleRcptToString componentsSeparatedByString:@"\r\n"] mutableCopy];
                [multipleRcptTo removeLastObject];
            } 
            if ([multipleRcptTo count] > 0) {
                NSString *rcptTo = [NSString stringWithFormat:@"%@\r\n", [multipleRcptTo objectAtIndex:0]];
                [multipleRcptTo removeObjectAtIndex:0];

                //DEBUGLOG(@"C: %@", rcptTo);
                if (CFWriteStreamWriteFully((CFWriteStreamRef)outputStream, (const uint8_t *)[rcptTo UTF8String], [rcptTo lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) < 0)
                {
                    error =  [outputStream streamError];
                    encounteredError = YES;
                }
                else
                {
                    [self startShortWatchdog];
                }
            } 
            if ([multipleRcptTo count] == 0) {
                sendState = kSKPSMTPWaitingToReply;

            }
        }
        break;
    }

and add this into header:

NSMutableArray *multipleRcptTo;

EDIT : Also change below method as multipleRcptTo is used as NSMutableString which is local declaration :

- (NSString *)formatAddresses:(NSString *)addresses {
  NSCharacterSet    *splitSet = [NSCharacterSet characterSetWithCharactersInString:@";,"];
  NSMutableString   *multipleRcpt = [NSMutableString string];

  if ((addresses != nil) && (![addresses isEqualToString:@""])) {
    if( [addresses rangeOfString:@";"].location != NSNotFound || [addresses rangeOfString:@","].location != NSNotFound ) {
        NSArray *addressParts = [addresses componentsSeparatedByCharactersInSet:splitSet];

        for( NSString *address in addressParts ) {
            [multipleRcpt appendString:[self formatAnAddress:address]];
        }
    }
    else {
        [multipleRcpt appendString:[self formatAnAddress:addresses]];
    }       
  }

  return(multipleRcpt);
}

SKPSMTPMessage sends to the SMTP address all at once, and must send one by one.

0

精彩评论

暂无评论...
验证码 换一张
取 消