wpf "emailto" - with attachments - wpf

I am developing a WPF app in MVVM presentation pattern. I have a grid and I am trying to have an "emailto" hyperlink and when the user clicks that i am trying to export all the data to an excel and open the default email client with a draft new message window(email client could be Lotus/Outlook) and attach the excel as an attachment to the mail. I am able to define a "Mailto" hyperlink and when i click that i am able to open the draft message email window. But i am not sure how to send the excel as an attachment. Any help is greatly appreciated.

By sending an Excel file you don't mean generating of this file somehow. Right? So you just need to attach a file.
I always use Andrew Baker's MAPI wrapper class which seems to be very reliable and has never failed for last 6 years. It's just 18Kb of C# code and it does exactly what you need.
var message = new MapiMailMessage(subject, body);
message.Recipients.Add(mailAddress);
message.Files.Add(filePath);
message.ShowDialog();

You could use Simple MAPI API to solve your problem:
var mapi = new Mapi();
mapi.Logon(IntPtr.Zero);
foreach (var filePath in files)
mapi.Attach(filePath);
mapi.Send("subject", "body text", true /* show send message dialog to user */);
mapi.Logoff();

Related

Discord JDA using a local image in an embed?

Is it possible to use a local image file as a thumbnail/image in an embedded message with Discord JDA?
For one of my commands i'm building an image programmatically and uploading it via the Imgur API before displaying it in an embedded message using the Imgur URL.
I know I can send the file to the channel directly but i'd like it to be contained within an embed that displays other relevant info.
Cheers
You can use attachment://filename.ext as described in the documentation for setImage.
For instance, if you have a file called cat-final-copy-final-LAST.png you can send it like this:
// the name locally is not cat.png but we can still call it cat.png when we send it with addFile
File file = new File("cat-final-copy-final-LAST.png");
EmbedBuilder embed = new EmbedBuilder();
// this URI "attachment://cat.png" references the attachment with the name "cat.png" that you pass in `addFile` below
embed.setImage("attachment://cat.png");
Then send it, with 5.X like this:
// this name does not have to be the same name the file has locally, it can be anything as long as the file extension is correct
channel.sendMessage(embed.build())
.addFiles(FileUpload.fromData(file, "cat.png"))
.queue();
Or with JDA 4.X:
// this name does not have to be the same name the file has locally, it can be anything as long as the file extension is correct
channel.sendMessage(embed.build())
.addFile(file, "cat.png")
.queue();

Sending attachments as an email from a form - NOT from local files

I have created a HTML email form which allows a user to enter To, subject, message, content and attachments however I cannot get the attachments to send.
I have researched online and came across many variations of this code:
messageBodyPart = new MimeBodyPart();
String filename = "/home/manisha/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
But is there a way of sending attachments input into the form instead of adding the file path to a file in the code?
Thanks
First, the files need to be uploaded from the browser to the server using the html form. Depending on what you're using to manage the uploaded data, you can store the file data in memory or in files on the server. If you store it in memory, you can use a ByteArrayDataSource instead of FileDataSource in your code above.

sending a .rtf file with javaMail

I am trying to get a file with the .rtf extension as an attachment with an email. I cannot seem to get it in my mailbox.
the code I currently use
try
{
Message msg = new MimeMessage(session);
msg.setFrom( ); // this is filled in but hidden for this question
msg.addRecipient();// this is filled in but hidden for this question
msg.setSubject("test email");
msg.setText("body test content");
msg.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(receiveFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.attachFile(backupFile);
messageBodyPart.setFileName("reportFile.rtf");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
receiveFile is the rtf file in question that needs to be send as an attachement.
Do not bother wtih server settings and such. I have send emails using this code so that works all just fine :). and had success sending .txt or .doc files as well so I know my info is correct. just when I try to send it as reportFile.rtf the mail just does not arrive. and I have tried 2 systems both together (the datahandler + source path and the attachFile path) and both did not really give me what I wanted.
Is a rtf file as attachment possible using javaMail or am I looking in the wrong direction?
After looking at it more closely with my co-worker we figured out that the email I used as sender was interperted as a spam and thus dumped it in the spam folder. now in itself this is not a problem since it was fine for testing. However it seems that if you keep spamming with that address the mail server indeed blocks like a good percentage of the mails i was sending. So this made it hard to debug since it sometimes got through and sometimes did not. the issues has been resolved now.

java mail attachments getting corrupted

This is my code for attaching the files to the mail:
Multipart mp=new MimeMultipart("mixed");
BodyPart mbody=new MimeBodyPart();
mbody.setHeader("Content-Type", "text/html; charset=us-ascii");
mbody.setHeader("Content-Transfer-Encoding","7bit");
mbody.setContent(content2, "text/html");
mp.addBodyPart(mbody);
for(File file:f){
BodyPart mbody2=new MimeBodyPart();
DataSource ds=new FileDataSource(file.getAbsolutePath());
mbody2.setDataHandler(new DataHandler(ds));
mbody2.setFileName(ds.getName());
mbody2.setHeader("Content-Type", "multipart/mixed");
mbody2.setHeader("Content-Transfer-Encoding", "base64");
mp.addBodyPart(mbody2);
}
m.setContent(mp);
content2 is the html content I am embedding in the E-mail, and I am adding files from an arraylist f.
The problem here is that although the files get attached and I receive the E-mail fine, I am unable to open the attachments because the data is corrupt. This happens for all the files I've tried to attach like jpegs, pdfs, spreadsheets, word docs and txt files.
I read here: https://community.oracle.com/thread/1589120 that this could happen because JavaMail uses encoding that messes up the binary data of the file and adding mbody2.setHeader("Content-Transfer-Encoding", "base64"); should fix the problem but that doesn't work for me.
Any ideas on what could be wrong?
Thanks
Time for some debugging...
First, remove all of the setHeader calls; some of them are wrong and none of them should be necessary.
Next, determine if the problem is on the sending end or the receiving end. Try multiple mail readers to see if they all have problems with the attachments.
Try sending plain text attachments. Are they also corrupted?
Post the protocol trace showing what happens when you send a simple message with a simple attachment that fails, so we can see if the message is being constructed correctly.
What version of JavaMail are you using?
What mail reader are you using to view the attachments?

ExtJS Custom Vtypes

Iam using Vtypes for Changpassword window.
My requirment is need to use only vtypes for all required/validations fields
So with out enter data clik on save its shows bubbles for required fields,but also show vtype for oldpassword not match.So how can use vtype after hitting database(From server) So is it possible?How
please provide some idea
Thanks in advance
You cannot use Ext.form.VTypes on the server unless you use some sort of JavaScript server (node.js with an ExtJS adapter - if there is one). You didn't mention the programming language you use on the server, so the answer is quite generic. To return errors from form posts that will be displayed as form field errors, your response to the form submit must conform to the following format:
{
success: false,
errors: {
oldpassword: "Your current password does not match"
}
}
The important part is the errors-structure. It contains key-value-pairs with the key being the name of the form field you'd like to display the error on and the value being the error message that will be displayed.

Resources