How to Send image in Email body? - iphone

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];
}

Related

how to send the mail to the desired user from iphone app

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];
}
}

How to send the mail with attachment through SMTP using iphone

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];

MFMailComposeViewController bringing up NSException

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];
}

Mail Composer Problem with app ondevice method

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.

iphone email attachment

I used the MessageUI framework to send the mail with attachment from my application.
But i got the following error,
2009-09-07 19:52:23.483 emailTest[11711:5b17]
Error loading /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator: dlopen(/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator, 265): Library not loaded: /System/Library/PrivateFrameworks/MobileWirelessSync.framework/MobileWirelessSync
Referenced from: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator
Reason: image not found
2009-09-07 19:52:23.489 emailTest[11711:5b17] [+[AccountsManager _migrateAccountsIfNeeded]] Accounts migration failed
[Switching to process 11711 local thread 0xf03]
my code is,
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[picker setSubject:#"This is iPhone email attachment test"];
UIImage *sampleImg = [UIImage imageNamed:#"iPhone.jpg"];
NSData *imageData = UIImageJPEGRepresentation(sampleImg, 1);
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"iPhone.jpg"];
NSString *emailBody = #"I am sending the image attachment with iPhone email service";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
please help me.
You do not have to type extension in your filename. like "iphone.jpg" is not working. just write "iphone" in filename because you already define mimeType. And also you have to define path for resource.
Below is the sample code to attach "rainy.png" 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:#"rainy"];
// Fill out the email body text
NSString *emailBody = #"It is raining";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
This error seems to be related to the running mail int he simulator and not to your code. Even stock Apple's sample MailComposer reports identical error in the simulator:
2009-11-12 20:30:39.270 MailComposer[7426:4f1f] Error loading /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator: dlopen(/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator, 265): Library not loaded: /System/Library/PrivateFrameworks/MobileWirelessSync.framework/MobileWirelessSync
Referenced from: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/DataClassMigrators/AccountMigrator.migrator/AccountMigrator
Reason: image not found
2009-11-12 20:30:39.271 MailComposer[7426:4f1f] [+[AccountsManager _migrateAccountsIfNeeded]] Accounts migration failed
Add following method to dismiss the MFMailComposeViewController:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFailComposeResult)result error:(NSError*)error
{
// NEVER REACHES THIS PLACE
[self dismissModalViewControllerAnimated:YES];
NSLog (#"mail finished");
}
use this for attach image in a mail, tested in ios 4,5,6
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
UIImage *myImage = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"image"];