views:

184

answers:

2

Hi all,

In my application I have to send feedback to the client's email.

Here is my 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]];
}

But this does not work. Is it require any type of SMTP setting or something else? I tried apple's MailComposer sample but it also does not work.

Please help me.

+2  A: 

I have posted this here, but for the sake of not clicking again here it is:


You need to include the MessageUI.framework into your project, and inside your header file you need to set the delegate:

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

@property (nonatomic, retain) MFMailComposeViewController *email;

Once you do that, you have a few delegate methods inside your implementation file you need to include (You should check to see the result, but I am trying to keep as little code as needed):

@synthesize email;

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

Wherever you want to use this, you need to initialize and set it up like this:

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];
Garrett
A: 

Try to use different format:

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