在ios6中使用skpsmtpmessage通过电子邮件发送2个以上的图像

时间:2012-12-17 09:48:38

标签: ios email ios6 smtp

我正在尝试将图片附加到电子邮件中,并将电子邮件发送到我的电子邮件添加。问题是,当我发送附有4或5个图像的电子邮件时,该应用程序将继续处理并最终被绞死和崩溃,并且不会发送电子邮件。一张图片工作正常。我正在使用skpsmtp发送邮件。相同的应用程序在iOS5中工作正常,但是当我在iOS6上运行它时它会挂起,我无法发送邮件,因为它挂起。

代码如下所示:

- (IBAction)    SendEmail {
    //supports multiple emails
    BOOL bIsEmailValid = NO;
    if ([txtTO.text rangeOfString:@","].location != NSNotFound) {
        NSArray *arrEmails = [txtTO.text componentsSeparatedByString:@","];
        DLog(@"Emails: %@", arrEmails);
        if ([arrEmails count] > 0) {
            for (int ctr = 0; ctr < [arrEmails count] ; ctr++) {
                NSString *strEmail = [(NSString*)[arrEmails objectAtIndex:ctr] stringByReplacingOccurrencesOfString:@" " withString:@""];
                if ([self IsValidEmail:strEmail]) {
                    bIsEmailValid = YES;
                } else {
                    bIsEmailValid = NO;
                    break;
                }
            }
        }
    } else {    // only 1 email entered
        if ([self IsValidEmail:txtTO.text]) {
            bIsEmailValid = YES;
        } else {
            bIsEmailValid = NO;
        }
    }

    if (bIsEmailValid) {
        [NSThread detachNewThreadSelector:@selector(ActivityViewLoading) toTarget:self withObject:nil];
        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
        testMsg.fromEmail       = FROM_EMAIL;
        testMsg.toEmail         = txtTO.text;
        testMsg.relayHost       = RELAY_HOST;
        testMsg.requiresAuth    = YES;
        testMsg.login           = EMAIL_LOGIN;
        testMsg.pass            = PASSWORD;
        testMsg.subject         = txtSubj.text;
        testMsg.wantsSecure     = YES;
        testMsg.delegate        = self;
        testMsg.bccEmail        = BCC_EMAIL;


        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        NSString *defStrName     = [defaults stringForKey:@"keyEName"]; 
        NSString *defStrContact  = [defaults stringForKey:@"keyEContNo"];
        NSString *defStrEmail    = [defaults stringForKey:@"keyEEmailAdd"]; 
        NSString *defStrDescr    = [defaults stringForKey:@"keyEShortMessage"];
        DLog(@"%@ %@ %@ %@",defStrName, defStrContact, defStrEmail, defStrDescr);

        NSMutableArray* parts = [[NSMutableArray alloc] init];

        NSString *strhtml = [NSString stringWithFormat:@"<div>%@</div>",[txtDesc.text stringWithNewLinesAsBRs]];

        NSString *defStrWebLinks        = [defaults stringForKey:@"keyEwebLinks"];
        NSArray *array     = [defStrWebLinks componentsSeparatedByString:@"|"];
        NSString *strTemp1 = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *strTemp2 = [[array objectAtIndex:1] stringByReplacingOccurrencesOfString:@" " withString:@""];

        NSString *strFormat1 = [NSString stringWithFormat:@"<a href=\"\%@\"\>%@</a>",
                                strTemp1,strTemp1];

        NSString *strFormat12 = [NSString stringWithFormat:@"<a href=\"\%@\"\>%@</a>",
                                strTemp2,strTemp2];

        NSString *strComb = [NSString stringWithFormat:@"%@ | %@",strFormat1,strFormat12];

        strhtml = [strhtml stringByReplacingOccurrencesOfString:defStrWebLinks withString:strComb];
        //DLog(@"%@",strhtml);

        NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/html",kSKPSMTPPartContentTypeKey,
                                   strhtml,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];


        [parts addObject:plainPart];

        for (int nCtr = 0; nCtr < [arrPix count]; nCtr++) {
            UIImageView *imageV = [arrPix objectAtIndex:nCtr];
            if (imageV.image) {
                NSData *imageData = UIImagePNGRepresentation(imageV.image);
                NSString *strFileName = [NSString stringWithFormat:@"MyPicture-%d.jpeg",nCtr];

                NSString *strFormat = [NSString stringWithFormat:@"image/jpeg;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",strFileName];
                NSString *strFormat2 = [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",strFileName];
                NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:strFormat,kSKPSMTPPartContentTypeKey,
                                         strFormat2,kSKPSMTPPartContentDispositionKey,[imageData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

                [parts addObject:vcfPart];
            }
        }


        testMsg.parts = parts;
        [testMsg send];

}

需要一些指导。感谢..

1 个答案:

答案 0 :(得分:1)

将图像格式从PNG更改为JPEG。

因此改变这一行

NSData * imageData = UIImagePNGRepresentation(imageV.image);

NSData * imageData = UIImageJPEGRepresentation(imageV.image,0.9);

它将解决问题...