I want saved email addresses of different users, and I want to open mailBox's new mail when I click the email, same as phonebook email in iphone,
what should I to do ???
You should use the MFMailComposeViewController class, and the implement MFMailComposeViewControllerDelegate protocol,
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
Once you sent you will get delegate callback in mailComposeController :
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"It's gone!");
}
[self dismissModalViewControllerAnimated:YES];
}
Create new message within Mail:
NSString *subject = #"The subject";
NSString *body = #"The message";
NSString *address = #"mail#address.com";
NSString *cc = #"mail#address.com";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
Related
I am sending email from iphone app and i want that email sent to should be the value or address the var emailTO has should be in TO automatically.
NSString*emailTO=#"ali#yahoo.com";
[picker setRecipients:[NSArray arrayWithObject:emailTO]];
but it does not work any idea how to implement in this way thanks
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:[NSArray arrayWithObject:[NSString stringWithFormat:#"%#,#"ur Receipent data"]]]];
Try to use this one...
NSArray *array = [NSArray arrayWithObject:#"ali#yahoo.com"];
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:array];
[self presentViewController:mailViewController animated:YES completion:NULL];
}
else
{
NSLog(#"Device is unable to send email in its current state.");
}
Try This. Hope will Help You.Note that you will need to add the MessageUI framework to your app.
#import <MessageUI/MessageUI.h>
and
MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];
mail.mailComposeDelegate = self;
[mail setToRecipients:[NSArray arrayWithObject:#"email#gmail.com"]];
[mail setSubject:#"Set Your Subject Here"];
[self presentModalViewController:mail animated:YES];
Use MFMailComposeViewControllerDelegate protocol: Delegate Function for conform the result
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
}
Description
setToRecipients:
Sets the initial recipients to include in the email’s “To” field.
- (void)setToRecipients:(NSArray*)toRecipients
Parameters
toRecipients
An array of NSString objects, each of which contains the email address of a single recipient.
try this
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[picker setRecipients:[NSArray arrayWithObject:#"your email address"]];
[self presentViewController:mailViewController animated:YES completion:nil];
If you want to open iPhone Email application, then use below code.So, you have not any need to add MFMailComposeViewController.
NSString *subject = #"";
NSString *body = #"";
NSString *address = #"test#gmail.com";
NSString *cc = #"";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url];
You should use
[picker setRecipients:[NSArray arrayWithObjects:emailTO,nil]];
I am sending mail from iphone application
i want that instead of this static reception user should enter in to field in mail and mail should be sent to them.
- (void)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Game"];
// Set up recipients
NSArray * toRecipients = [NSArray arrayWithObject:#"husain_2000#yahoo.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString * emailBody = [NSString stringWithFormat:#"My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
NSString *recipients = #"mailto:husain_2000#yahoo.com?&subject=Game";
NSString *body = [NSString stringWithFormat:#"&body=My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
}
The email should be configured in the iPhone.
Then Try this code
- (void)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Game"];
// Leave recipients as blank
// Fill out the email body text
NSString * emailBody = [NSString stringWithFormat:#"My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
I have used the MFMailComposeViewController for sending the email in my code. I have also used the reachability code to check the internet connection. Internet connection is working fine. Whenever I used to send a mail from my code I got the message that email has been sent. But I didn't get any mail. There is no email which is sending from the app. I don't what is the reason behind this. If someone know how to get rid of this problem please provide me some solution.
-(void)sendemail
{
emailBody = #"";
mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Report"];
NSURL *url = [NSURL URLWithString:imagePath];
NSData *data = [NSData dataWithContentsOfURL:url];
[mail addAttachmentData:data mimeType:#"image/jpg" fileName:#"licence.png"];
NSMutableString *breakline = [[NSMutableString alloc]init];
[breakline appendString:[NSString stringWithFormat:#"<br>"]];
NSArray *toRecipients = [NSArray arrayWithObject:#"m.garg#ldh.01s.in"];
[mail setToRecipients:toRecipients];
emailBody = [emailBody stringByAppendingFormat:#"%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#",#"Name: ",namestr,breakline,#"Address: ", addresstr,breakline,#"Landmark: ",landmarkstr,breakline,#"City: ", citystr,breakline,#"State: ", statestr,breakline,#"PIN: ", pinstr,breakline,#"Contact No: ",phonestr,breakline,#"Licence:",licencestr,breakline,#"Email Id", emailstr];
[mail setMessageBody:emailBody isHTML:YES];
if (mail != nil) {
[self presentModalViewController: mail animated: YES];
[mail release];
}
}
Thanks to all.
Simply try something like this:
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
[controller setToRecipients:[NSArray arrayWithObject:#"123#abc.com.pk"]];
controller.mailComposeDelegate = self;
[controller setSubject:#""];
[controller setMessageBody:#"" isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
}
and don't forget to add the following delegate method for returning to your app:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
I wonder if your "to recipients" array is being freed before the email got sent.
Are your recipent emails showing in the "Send To" field when the mail composer view controller appears ?
I am using this code for mail
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *msgTitle = #"Sample Title";
[picker setSubject:msgTitle];
NSArray *toRecipients =[[NSArray alloc] init];
NSArray *ccRecipients =[[NSArray alloc] init];
NSArray *bccRecipients =[[NSArray alloc] init];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
NSString *sum = #"The Email Body string is here";
NSString *emailBody;
emailBody = [NSString stringWithFormat:#"%#",sum];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
it works,
i want to know,is it required to modify
-(void)launchMailAppOnDevice
{
NSString *recipients = #"mailto:first#example.com?cc=second#example.com,third#example.com&subject=Hello from California!";
NSString *body = #"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
if yes then
how i modify in this method.
Please help me.
If email address is given statically and displayed in To address.
You can use this code,
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mail setToRecipients:[NSArray arrayWithObjects:#"AAA#BBB.com",nil]];
[self presentModalViewController:mail animated:YES];
}
[mail release];
And
see my answer
Best of luck.
I want my app to send mail. I can use the mailto: URL scheme, but it terminates my app when launching iPhone mail. The newsreader from The Independent (British paper) seems to bring up a mail compose view within the app. When you send or cancel, the app reappears immediately.
Does anyone know how to do this?
thanks,
You need to use 3.0 Message UI Framework!
#import <MessageUI/MessageUI.h>
#interface ViewReminderViewController_iPhone : UIViewController
<MFMailComposeViewControllerDelegate>
{
UiButton *mailButton;
}
- (IBAction)EmailButton:(id)sender;
#end
#implementation ViewController
- (IBAction)EmailButton:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your EMail Subject"];
//SET UP THE RECIPIENTS (or leave not set)
//NSArray *toRecipients = [NSArray arrayWithObjects:#"first#example.com", nil];
//[picker setToRecipients:toRecipients];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//[picker setCcRecipients:ccRecipients];
//NSArray *bccRecipients = [NSArray arrayWithObjects:#"four#example.com", nil];
//[picker setBccRecipients:bccRecipients];
//ATTACH FILE
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"MediaFiles"];
path = [path stringByAppendingPathComponent:MyFileName];
NSLog(#"Attaching file: %#", path);
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) //Does file exist?
{
NSLog(#"File exists to attach");
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/octet-stream"
fileName:#"DesredFileName.mov"];
}
//CREATE EMAIL BODY TEXT
NSString *emailBody = #"Your Email Body";
[picker setMessageBody:emailBody isHTML:NO];
//PRESENT THE MAIL COMPOSITION INTERFACE
[self presentModalViewController:picker animated:YES];
[picker release];
}
Delegate To Clear Compose Email View Controller
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES]; //Clear the compose email view controller
}