2012-12-17 33 views
2

我試圖將圖像附加到電子郵件並將電子郵件發送到我的電子郵件地址。問題是,當我發出附有4或5張圖片的電子郵件時,該應用程序會一直處理,最終會被掛起並崩潰,並且不會發送電子郵件。它與一個圖像正常工作。我正在使用skpsmtp發送郵件。同樣的應用程序在iOS5中工作正常,但在iOS6上運行時它會掛起,我無法發送郵件,因爲它掛起。通過電子郵件發送超過2張圖片在ios6中使用skpsmtpmessage

的代碼看起來是這樣的:

- (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]; 

} 

需要一些指導。謝謝..

+0

有什麼想法嗎?需要一些幫助... – lakesh

回答

1

將圖像格式從PNG更改爲JPEG。

因此改變這一行

的NSData *的imageData = UIImagePNGRepresentation(imageV.image);

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

它會解決問題...