How to attach multiple files in single mail in iOS - iphone

I want to send multiple attachment files in single mail in iOS programatically. I have tried the following so far:
// I give the file from array
NSString *str_mail = [readingEmfReading objectAtIndex:0];
// here I can encode the file
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
//here I can attach the file with extance of .csv
[controller addAttachmentData:myData mimeType:#".cvs" fileName:retriveEmail]
//here I can set the body for mail
[controller setMessageBody:#"file" isHTML:NO];
//here code for sent mail
if (controller) [self presentViewController:controller animated:YES completion:nil];
By using this code, I can only send one attachment. I want to send multiple files however. How can I achieve this?

you add many time addAttachmentData and add muliple file
try this code :-
add the this line
NSString *str_mail = [readingEmfReading objectAtIndex:0];
// here i can encoded the file
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
NSData *myData1 = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
//here i can attach the file with extance of .csv
[controller addAttachmentData:myData mimeType:#".cvs" fileName:retriveEmail]
//here i can set the body for mail
// For second file
NSData *myData1 = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
[controller addAttachmentData:myData1 mimeType:#".cvs" fileName:retriveEmail]
[controller setMessageBody:#"file" isHTML:NO];
//here code for sent mail
if (controller) [self presentViewController:controller animated:YES completion:nil];

if([MFMailComposeViewController canSendMail])
{
[mailController setMailComposeDelegate:self];
NSString* message=#"";
NSString *filePath;
for (int i=0; i<filesarray.count; i++)
{
if (i==filesarray.count-1)
{
message=[message stringByAppendingFormat:#"%# ",[filesarray objectAtIndex:i]];
}
else if (i==filesarray.count-2)
{
message=[message stringByAppendingFormat:#"%# and ",[filesarray objectAtIndex:i]];
}
else
message=[message stringByAppendingFormat:#"%#, ",[filesarray objectAtIndex:i]];
NSString *datPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
datPath = [datPath stringByAppendingFormat:#"/%#.csv",[filesarray objectAtIndex:i]];
filePath = datPath;
[mailController addAttachmentData:[NSData dataWithContentsOfFile:filePath] mimeType:#"text/csv" fileName:[NSString stringWithFormat:#"%#.csv",[filesarray objectAtIndex:i]]];
}
[mailController setSubject:message];
[mailController setMessageBody:#"" isHTML:NO];
[self presentModalViewController:mailController animated:YES];
}

Related

Sending html text to mail as an attachment in ios as pdf format

i have to send html text to mail as an attachment with a pdf extension.
Code :
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
NSArray *toReceipents =[NSArray arrayWithObjects:#"", nil];
[mailViewController setToRecipients:toReceipents];
[mailViewController setSubject:strMailMessage];
NSLog(#"Mail Message:%# %#",strMailMessage,appDelegate.strShareText);
NSData* data = [appDelegate.strShareText dataUsingEncoding:NSUTF8StringEncoding];
[mailViewController setMessageBody:appDelegate.strShareText isHTML:YES];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"Medication file.pdf"];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
Note:when i download the pdf file i am getting the same text.but i want to show the text in table format in pdf document
Good code but for HTML try this ;) :
- (IBAction)mailCompose:(id)sender {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Hello World!"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"e-mail here or leve empty", nil];
[mail setToRecipients:toRecipients];
NSString *emailBody = #"Body message App</b><br /><a href='https://itunes.apple.com/it/app/yourApp/id'>Download Free on AppStore!</a>";
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myVoice.caf"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[mail addAttachmentData:myData mimeType:documentsDirectory fileName:#"myVoice.caf"];
[mail setMessageBody:emailBody isHTML:YES];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Saved");
break;
case MFMailComposeResultFailed:
NSLog(#"Faild");
break;
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
NSLog(#"Default");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Enjy hope this can help you

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

Not receiving attachments sent from MFMailComposer

I am trying to send out an email with a .csv attachment on the iPad. I am using the MFMailComposer per many examples given which is displayed below. When sending out the email, I can see the correct file attachment in the MFMailComposer window, but when I receive the email, nothing is attached. Any guidance as to what I may be doing wrong would be appreciated. Thank you for your time,
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:[NSString stringWithFormat:#"Results for Participant %d.", [delegate participantNumber]]];
[mailViewController setMessageBody:[NSString stringWithFormat:#"The results for Participant %d in Study: %# are as follows:", [delegate participantNumber], [[delegate getAccountData:([delegate accountItems] * [delegate accountNumberInUse])] description]] isHTML:NO];
NSData *textData = [[NSData alloc] initWithContentsOfFile:dataFileName];
[mailViewController addAttachmentData:textData mimeType:#"text/csv" fileName:[NSString stringWithFormat:#"Participant_Info_#%d.csv", [delegate participantNumber]]];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
Check the content of the textData variable, also (just guessing from your code) the dataFileName needs to contain a full system path to the file, not just it's name!
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:dataFileName ofType:nil];

Attachment doesn't displayed in mail which is sent through iPhone

I have send an attachment through iPhone application.
But when I see the mail, I can see the upin that shows that something is attached.
But when I open the mail I couldn't found any attachment?
What is the problem behind this?
-(IBAction)btnPressedExport:(id)sender{
NSArray *x=[[NSArray alloc] initWithArray:[DatabaseAccess getAllTransactionsForUserID:[BudgetTrackerAppDelegate getUserID] profileID:[BudgetTrackerAppDelegate getProfileID]]];
int i=-1,j=[x count];
NSDictionary *tmp;
NSMutableString *stringToWrite=[[NSMutableString alloc] init];
for(;i<j;i++){
if(i==-1){
[stringToWrite appendString:#"TransactionID,TransactionDate,ProfileName,ProfileType,GroupName,GroupType,CategoryName,TransactionAmt\n"];
} else {
tmp=[x objectAtIndex:i];
[stringToWrite appendFormat:#"%#,%#,%#,%#,%#,%#,%#,%#\n",
[tmp valueForKey:#"TraID"],[tmp valueForKey:#"TraDate"],[tmp valueForKey:#"ProfileName"],[tmp valueForKey:#"ProfileType"],[tmp valueForKey:#"GroupName"],[tmp valueForKey:#"GroupType"],[tmp valueForKey:#"CatName"],[tmp valueForKey:#"TraAmt"]];
}
}
[stringToWrite writeToFile:[self pathOfCSVForExport] atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];
// [stringToWrite writeToFile:[self pathOfCSVForExport] atomically:YES];
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
// picker.delegate=self;
[picker setSubject:#"Hello from Sugar!"];
//Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"sugar.srk#pqr.com"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:#"xyz.dalal#pqr.com", #"kandarp.dave#phptalent.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:#"myExport" ofType:#"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];
NSString *fileNameToSend=#"BudgetTracker.csv";//[NSString stringWithFormat:#"%#.csv",[x valueForKey:#"ProfileName"]];
[picker addAttachmentData:myData mimeType:#"text/plain" fileName:fileNameToSend];
// text/html/
// Fill out the email body text
NSString *emailBody = [NSString stringWithFormat:#"%#",#"Hello! This is export test."];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
// NSLog(#"here2");
switch (result)
{
case MFMailComposeResultCancelled: break;
case MFMailComposeResultSaved: break;
case MFMailComposeResultSent: break;
case MFMailComposeResultFailed: break;
default: break;
}
// self.navigationController.navigationBarHidden=NO;
// [self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
This looks wrong, unless it's just for testing:
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"myExport" ofType:#"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];
the path you're using is to a file in your application bundle, which is read-only, so it can't be the CSV file you just made.
if you want to write a temporary file and email it, you need to write it to someplace like your Documents directory, i.e. a path like
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/myexport.csv", docDirectory];

embed mail compose in an iPhone app

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
}