How to implement the UI of "New Message" of Mail app in iPhone.?
Use the MFMailComposeViewController class.
Don't worry about UI to integrate mail in your app. Everything will be taken care by MFMailComposeViewController Framework. Add this framework and Write the below code
#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
}
Related
I am sending image with some text in Email Body using this link my code is given below
NSString *urlString = #"url";
NSString *searchingurl = [NSString stringWithFormat:#"%#%#", urlString,idnumber];
NSString *string = [NSString stringWithFormat:#"%#<br><br>Item No :%#<br>Type :%#<br>Size : %#",searchingurl,str123,mytype,itemsize];
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0f);
NSString *encodedString = [imageData base64Encoding];
[emailBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64,%#'></b></p>",encodedString]];
[emailBody appendString:string];
[emailBody appendString:#"</body></html>"];
NSLog(#"%#",emailBody);
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:#""];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
Screenshow for above code
After Sending Mail to Yahoo it show my image and text on both Mackbook and in iphone device , when i send mail to Gmail and checked it in iphone device it show image with text but When i checked gmail in Mackbook it not show my image
Some one Can guide me why my image not show in Gmail on PC.Any help will be appriated.Thanks
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your subject"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
NSLog(#"Video size >> %d",(videodta.length/1024)/1024);
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"testapp"];
// Fill out the email body text
NSString *emailBody = #"Type your message here";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
I m working on iPhone eBook app.
In my app books are created using HTML.
In Index.html page i have to put the email functionality which email(send) the contents of particular chapter.
Now by using webview i show the index.html and in that i have created UIActionSheet which will show the Email button.
Please suggest me that how can i identify the index of different links to send
email of particular chapter.
Below code will work even if you have not configured email in your device.
Here is the code:
- (IBAction) sendEmail:(id)sender
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from DShah!"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
NSString *path = [[NSBundle mainBundle] pathForResource:#"userdata" ofType:#"abc"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.abc", #"userdata"]];
NSData *myData = [NSData dataWithContentsOfFile:fullPath];
[picker addAttachmentData:myData mimeType:#"csv" fileName:#"userdata.abc"];
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)launchMailAppOnDevice
{
NSString *recipients = #"mailto:first#example.com&subject=Hello from DShah!";
NSString *body = #"&body=It is cold in Winter!";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
Then implement the Delegate method as below....
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
message = #"Result: canceled";
break;
case MFMailComposeResultSaved:
message = #"Result: saved";
break;
case MFMailComposeResultSent:
message = #"Result: sent";
break;
case MFMailComposeResultFailed:
message = #"Result: failed";
break;
default:
message = #"Result: not sent";
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email Demo" message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self dismissModalViewControllerAnimated:YES];
}
Check out Apple's MailComposer example. This will show you everything you need. http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865
Im new to Iphone develoment can any one helpme in getting sample code in send the mail with attachment through SMTP using iphone.
i have tried the sample code from this following URL
http://code.google.com/p/skpsmtpmessage/
Thanks
Below is the sample code to attach file with mail.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"rainy" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"myFile"];
// Fill out the email body text
NSString *emailBody = #"Message body : my first email sending ";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
Here is how we send attachments with our mail messages (the following attaches a jpeg and assumes fileName has been set elsewhere to a location within your bundle, but really any NSData object will work however you initilize it as long as you set your mimeType correctly):
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setMessageBody:#"Some Message" isHTML:YES];
[mailViewController setSubject:#"My Subject"];
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:fileName] mimeType:#"image/jpeg" fileName:#"PrettyPicture.jpg"];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
I have done a search for this problem, and have not found an answer, which is leading me to write my firs tquestion here.
The problem is when I press the button which calls the metho below, I get an exception on this line: [self presentModalViewController:mailViewController animated:YES]; and the exception is:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x20f6c'
In addition, I am running my app on the simulator if that helps.
I got the magority of the code from here.
Here is my code (with the email address blanked out):
// set up the email address array
NSArray *email = [[NSArray alloc] initWithObjects:#"foo", nil];
// Set up the view controller
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
// Set up the email to send to
[mailViewController setSubject:#"foo"];
[mailViewController setToRecipients:[email objectAtIndex:0]];
// Get the path to the plist
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"foo.plist"];
// Get the plist from the path
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailViewController addAttachmentData:myData mimeType:#"plist" fileName:#"foo"];
// Fill out the email body text
NSString *emailBody = #"Attached PLIST file";
[mailViewController setMessageBody:emailBody isHTML:NO];
// Release the array
[email release];
if (mailViewController != nil) {
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
What appears to be causing the exception?
In setToRecipients you have to specify an array, not a string. So you could pass your email-array.
-(IBAction)btnemail:(id)sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"subject"];
NSString *emailid=btnEmail.titleLabel.text;
NSArray *toRecipients = [NSArray arrayWithObject:emailid];
[picker setToRecipients:toRecipients];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:NO];
if ([MFMailComposeViewController canSendMail]) {
[self.parentViewController presentModalViewController:picker animated:YES];
}else {
}
[picker release];
}
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
}