I could not find this question anywhere.
Is it possible to craft attachment using MailKit without saving it local?
I have application linked to database and I am making Windows service for sending emails.
I read data from DB and I would like to craft .json attachment inside program and send it via mail using MailKit without saving that attachment local.
Thanks in advance.
MimeKit allows reading from and writing to MemoryStreams (and other non-File-based streams).
You can also construct messages where the content of attachments and such are stored only in memory and not on disk.
The BodyBuilder.Attachments collection allows adding attachment data as byte[] and Streams (which includes allowing MemoryStreams). The fileName parameters in these Add() methods is only to provide a file name to set on the attachment.
Related
I'm developing an app that deals with emails. What I need is to get the URL of the location the attachment is stored on the exchange server. I can't download the attachment to my local machine so the attachment has to be accessed on the exchange server.
Is this possible?
I have looked online but can't seem to find anything.
With EWS no there is no URL to download an Attachment directly you need to know the AttachmentId (generally you would get this by using the GetItem operation) then you can use the GetAttachment operation to get the contents of the attachment (which is a Base64 Stream). see https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/getattachment-operation
We are working on Asp.net web application. As per requirement, We want to programmatically generate a pdf and need to send the generated pdf using “mailto:”(default email client) feature as attachment. Is it possible to send a generated email using “mailto” feature?.
We are stuck with this issue. Any help would be appreciable.
Best Regards,
Ranish
You can first save the generated pdf into a temp folder so that you can include this path into the attachment property.
<a href='mailto:name#example.com?Subject=SubjectText&Body=BodyText&Attachment="\\Path_to_the_temp_file" '>
A more better way would be to create a method that handles the sending of the mail.
Then you can use the MailMessage and the Attachment objects.
Namespace System.Net.Mail.Attachment
I just want to display the links of attachments to download in mail body without downloading the attachment initially. coz i don't want to download and save the attachments on my server everytime.
when users clicks the link, then I want to download that particular attachment(s) from gmail server ("https://mail-attachment.googleusercontent.com/attachment/u/1/?xxxx-xxxx")
is there is anyway to do this?
MailKit does not provide any way of doing this because it's not something that IMAP supports.
That said, it might be possible to figure out how to construct the URL based on the properties of the MIME attachment.
Based on examining the Download link in one of my own emails, it looks like Google uses their X-Attachment-Id header value to construct the realattid portion of the url. They probably have other variables as well, but it's hard to know exactly where the other values come from.
The attid value in the download url appears to be a positional value of the MIME attachment in the tree, similar to MailKit's BodyPart.PartSpecifier string.
Instead of trying to recreate GMail's download link, you could always use MailKit's ImapFolder.GetBodyPart() API to download individual MIME parts when the user requests them.
In order to take advantage of this API, you'll need to call ImapFolder.Fetch() with MessageSummaryItems.BodyStructure.
You can take a look at https://github.com/jstedfast/MailKit/blob/master/samples/ImapClientDemo/ImapClientDemo/MainWindow.cs to see how to use the MessageSummary.Bodyproperty that you get back from the Fetch request.
In my current MVC project there is a requirement to open a new outlook mail item by clicking a button with pre populated body and subject.
I have used the following method to achieve that:
1. Create a MailItem with requried details like Subject, Mail body etc
2. Using SmtpDeliveryMethod as SpecifiedPickupDirectory and create an eml file
3. Force the user to download and open the file in outlook
So far so good and EML file is correctly opening in outlook but the user has to manually select his mail account from the "From dropdown".
My question is there any way to assign the detault From account in EML files in order to open in Outlook 2010
The way I got around it, and it is way "hacky" to say the least, is to open the saved .eml in C# and delete all the from/sender values before streaming it down to the browser.
I have an aspx page that accepts a bunch of querystring parameters that allow you to customize the .eml output. You can't save the .eml file without a "sender" or "from" email address, so you need to save that out first - I use a dummy email account. You can use the SMTP pickup method or the MailMessage extension class to save the .eml file. Then I open the saved file, look for my placeholder email address (in the "X-Sender" header and "From" values), replace that with an empty string, then send the altered stream to the browser. The user never gets the "from" dropdown, it just uses the default account. Works like a charm.
I am trying to figure out how to get a file that I have stored in my Database, onto the filesystem. I have looked everywhere, but all I can find is like for images pulling them out and serving them via Response. Can anyone point me in the direction of a tutorial or provide me with some input on which way to go?
From the perspective of serving the file through the web, the process is exactly the same for images as it is for any file type. The only difference would be the content type in the response header. (For a PDF, it would be application/pdf.)
Understand that over HTTP there is absolutely no concept of "files." All you have are requests and responses, each of which has headers and data. Whether it's an image, a PDF, a web page, anything... the structure of the response being delivered to the client (browser) is exactly the same. Headers tell the browser what kind of data it is and various other meta-details about it, and the data is just the raw data of the "file" being sent. The browser can choose what to do with the data.
In the case of web pages, the browser generally just renders it. In the case of other or unknown content types, the browser generally prompts the user to save it as a file. (Note that a file name can also be suggested in the header of the response.)
Edit: In response to your comment on #Pete M's answer...
The process is still very similar. "Files" as email attachments work in much the same way as "files" over HTTP. Essentially, all you need is the data stream and the content type. When building the MailMessage object, take a look at the Attachment object. As you pull your PDF file from the database (just like when pulling images from the database to serve to a browser client), set the data stream to the ContentStream on the attachment object. Then also set the ContentType (again, application/pdf) and the Name on the attachment object and you should be able to send it as part of the MailMessage.
You may need to set more options on the Attachment as well. Testing will indicate how explicit you need to be with things like encoding. If you can share some code as you attempt this, we can help you with that code.
It's no different than writing out any other kind of file. If you find a good "read/write text file" tutorial it's going to be the exact same process, you'll just get your initial stream from the database.
Can I ask why you are doing this? If it's for caching so you can return it to users who ask for it I would consider other options first. Requiring a dip into disk will often cause a bottle neck in performance.