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];
Related
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];
}
This is my code..
if ([ [ requestURL scheme ] isEqualToString: #"mailto" ])
{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
NSString *strEmail = [NSString stringWithFormat:#"%#",requestURL];
NSString *subString = [[strEmail componentsSeparatedByString:#":"] lastObject];
[composer setToRecipients:[NSArray arrayWithObjects:subString, nil]];
[composer setSubject:#"Kreativ-Q"];
[composer setMessageBody:#"" isHTML:YES];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
[composer release];
}
}
But when i click on the link in webview it is opening in mailBox. and when i send or cancel the mail from there and go back to my app the mail was still there. They show me twice. I just want, it would open in my app only..
Thanks in advance.
May help you:
Set this delegate in your .h file if you didnt.
<MFMailComposeViewControllerDelegate>
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 ?
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 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"];