1

我是这个话题的新手,请帮助我...

我正在使用帮助 Skpsmtpmessage 类将用户信息发送到 SMTP 服务器。但我收到“操作无法完成”。在-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error)

请帮帮我。

- (void) sendMessage {

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];

    NSString *myString;

    testMsg.relayHost = @"smtp.gmail.com";
    testMsg.requiresAuth = YES;
    testMsg.login =  @"my gmail id";

    testMsg.pass = @"my gmail password"; 

    testMsg.subject =  [NSString stringWithFormat:@"CLIENT iPhone App - %@", self.navigationItem.title];
    testMsg.bccEmail = emailField.text;
    testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
    testMsg.fromEmail = emailField.text;
    NSString *ff = [[UIDevice currentDevice] uniqueIdentifier];
    NSString *gg = [[UIDevice currentDevice] model];
    NSString *hh = [[UIDevice currentDevice] localizedModel];
    NSString *ii = [[UIDevice currentDevice] name];
    NSString *jj = [[UIDevice currentDevice] systemName];
    NSString *kk = [[UIDevice currentDevice] systemVersion];
    NSString *path;
    if ([self.navigationItem.title isEqualToString:@"Request Authorization"])
    {
        path = [[NSBundle mainBundle] pathForResource:@"ThankYou" ofType:@"txt"];
        testMsg.toEmail = @"myname@gmail.com";
    }
    else
    {
        path = [[NSBundle mainBundle] pathForResource:@"ThankYou2" ofType:@"txt"];
        testMsg.toEmail = @"myanme@gmail.com";
    }

    NSString *fileText = [NSString stringWithContentsOfFile:path];
    myString = [NSString stringWithFormat:@"LEADERS %@-\n\n%@\n\nName:  %@\nCompany:  %@\nAddress:  %@\nCity:  %@\nState:  %@\nZip:  %@\nPhone:  %@\nFax:  %@\nEmail:  %@\nComments:  %@\nRequire New Merchant Account:  %@\nReferral Code:  %@\nIdentifier:  %@\nModel:  %@\nLocalized Model:  %@\nName:  %@\nSystem Name:  %@\nSystem Version:  %@",self.navigationItem.title,fileText, firstNameField.text,companyField.text,addressField.text,cityField.text,stateField.text,zipCodeField.text,phoneField.text,faxField.text,emailField.text,countryField.text,customerIdField.text,lastNameField.text,ff,gg,hh,ii,jj,kk];

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

    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil,nil];
    testMsg.delegate= self;

    [testMsg send]; 

    [self.navigationController popViewControllerAnimated:TRUE];
}
4

2 回答 2

0
-(void)sendMail
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = [defaults objectForKey:@"fromEmail"];

    testMsg.toEmail   = [defaults objectForKey:@"toEmail"];
    testMsg.bccEmail  = [defaults objectForKey:@"bccEmal"];
    testMsg.relayHost = [defaults objectForKey:@"relayHost"];

    testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];

    NSLog(@"%@",testMsg.fromEmail);

    if (testMsg.requiresAuth) 
    {
        testMsg.login = [defaults objectForKey:@"login"];
        testMsg.pass  = [defaults objectForKey:@"pass"];
    }

    testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS!

    testMsg.subject = @"SMTPMessage Test Message";

    // Only do this for self-signed certs!
    // testMsg.validateSSLChain = NO;

    testMsg.delegate = self;

    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                               @"This is a tést messåge.",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.jpg"];

    NSData *jpgData = [NSData dataWithContentsOfFile:getImagePath];

    NSDictionary *jpgPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"savedImage.jpg\"",kSKPSMTPPartContentTypeKey,
                                 @"attachment;\r\n\tfilename=\"savedImage.jpg\"",kSKPSMTPPartContentDispositionKey,[jpgData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

    testMsg.parts = [NSArray arrayWithObjects:plainPart,jpgPart,nil];

    [testMsg send];


}

- (void)messageSent:(SKPSMTPMessage *)message
{
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"SMTP Mail"
                              message:@"Yay! Message was sent!"
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil,
                              nil];
    [alertView show];
    [alertView release];
    [message release];
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    NSString *str = [NSString stringWithFormat:@"Darn! Error!\n%i: %@\n%@", [error code], [error localizedDescription], [error localizedRecoverySuggestion]];
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"SMTP Mail"
                              message:str
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil,
                              nil];
    [alertView show];
    [alertView release];
    [message release];
}
于 2012-09-06T08:44:28.587 回答
0

我们必须使用发送邮件

-(void)sendMail
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = [defaults objectForKey:@"fromEmail"];

    testMsg.toEmail   = [defaults objectForKey:@"toEmail"];
    testMsg.bccEmail  = [defaults objectForKey:@"bccEmal"];
    testMsg.relayHost = [defaults objectForKey:@"relayHost"];

    testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];

    NSLog(@"%@",testMsg.fromEmail);

    if (testMsg.requiresAuth) 
    {
        testMsg.login = [defaults objectForKey:@"login"];
        testMsg.pass  = [defaults objectForKey:@"pass"];
    }

    testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; 
    // smtp.gmail.com doesn't work without TLS!

    testMsg.subject = @"SMTPMessage Test Message";

    // Only do this for self-signed certs!
    // testMsg.validateSSLChain = NO;

    testMsg.delegate = self;

    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain", kSKPSMTPPartContentTypeKey, @"This is a tést messåge.", kSKPSMTPPartMessageKey, @"8bit", kSKPSMTPPartContentTransferEncodingKey, nil];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.jpg"];

    NSData *jpgData = [NSData dataWithContentsOfFile:getImagePath];

    NSDictionary *jpgPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"savedImage.jpg\"",kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"savedImage.jpg\"",kSKPSMTPPartContentDispositionKey,[jpgData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

    testMsg.parts = [NSArray arrayWithObjects:plainPart,jpgPart,nil];

    [testMsg send];
}
于 2012-09-06T08:47:30.987 回答