2009-10-01 5 views
0

Dans mon application, je dois envoyer des commentaires à l'adresse e-mail du client.Envoyer un e-mail dans l'iPhone

Voici mon code,

-(void) send:(id) sender { 
    [self sendEmailTo:[txtTo text] withSubject:[txtSubject text] withBody:[txtBody text]]; 
} 

-(void) sendEmailTo:(NSString *)to withSubject:(NSString *) subject withBody:(NSString*)body { 
    NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@body=%@", 
    [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]]; 
} 

Mais cela ne fonctionne pas. Est-ce que cela nécessite n'importe quel type de paramètre SMTP ou autre chose? J'ai essayé l'exemple MailComposer d'apple mais cela ne fonctionne pas non plus.

S'il vous plaît aidez-moi.

Répondre

5

J'ai posté ce here, mais pour le bien de ne pas cliquer à nouveau ici il est:


Vous devez inclure les MessageUI.framework dans votre projet, et à l'intérieur de votre fichier d'en-tête, vous devez définir le délégué :

#import <MessageUI/MessageUI.h> 
@interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate> { 
    MFMailComposeViewController *email; 
} 

@property (nonatomic, retain) MFMailComposeViewController *email; 

une fois que vous faites cela, vous avez quelques méthodes de délégué à l'intérieur de votre dossier de mise en œuvre, vous devez inclure (vous devriez vérifier le résultat, mais je suis en train de garder aussi peu de code au besoin):

@synthesize email; 

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    [email dismissModalViewControllerAnimated:YES]; 
} 

Où que vous voulez utiliser, vous devez initialiser et configurer comme ceci:

email = [[MFMailComposeViewController alloc] init]; 
email.mailComposeDelegate = self; 

// Subject 
[email setSubject:@"Testing"]; 

// Optional Attachments 
NSData *artwork = UIImagePNGRepresentation([UIImage imageNamed:@"albumart.png"]); 
[email addAttachmentData:artwork mimeType:@"image/png" fileName:@"albumart.png"]; 

// Body 
[email setMessageBody:@"This is the body"]; 

// Present it 
[self presentModalViewController:email animated:YES]; 
0

Essayez d'utiliser un format différent:

NSString *mailString = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", 
    [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
Questions connexes