Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to generate dynamically PDF our own design in run time and mail as attachment and send that PDF.

Please tell me the solution.

Thanks in Advance
Jagan Mohana Rao
Posted

This is the most frequently asked question, google "create pdf file c#" for thousands of existing answers.
 
Share this answer
 
Comments
BillWoodruff 27-Jan-16 10:36am    
I suggest you post statements like this (without direct technical content) as comments, not solutions.
F-ES Sitecore 27-Jan-16 10:56am    
Normally I would, but this question is asked so many times there really is no excuse to keep asking it; it is just lazy and the result of zero research and effort. By posting as an answer it takes it off the "unanswered" list.
Sergey Alexandrovich Kryukov 27-Jan-16 11:00am    
I would say, you are not 100% right with this answer.
The question is not about PDF. Essentially, this is also about putting it all together in a mail message, which is not so simple. Even though it's easy to figure out from the documentation, we don't have many answers with the road map. It's not so trivial as some make think — please see Solution 2.
—SA
F-ES Sitecore 27-Jan-16 11:04am    
"How to send email" is the second-most commonly asked question, my argument still stands :D There is no need to ask questions like these when the answers are so easy to obtain.
Sergey Alexandrovich Kryukov 27-Jan-16 11:04am    
And you probably know that "lazy" and other considerations like that we usually put only in comments.
To me the criterion would be this: do you mention anything specific to the inquirer's question in your "solution". No...
You do post many useful comments and solutions, too; it's just a matter of proper placement...
—SA
This is a set of references to PDF libraries you can use: http://csharp-source.net/open-source/pdf-libraries.

This is probably the most used library: iTextSharp, a .NET PDF library | SourceForge.net[^].

Now, about mail "attachment". This is a very confusing work. In fact, nothing is "attached". E-mail is a pure-text block of data with optional "parts". Each part can have it's own content type and content disposition. Please see:
MailMessage Class (System.Net.Mail)[^],
MailMessage.Attachments Property (System.Net.Mail)[^],
AttachmentBase.ContentType Property (System.Net.Mail)[^],
Attachment.ContentDisposition Property (System.Net.Mail)[^],
ContentDisposition Class (System.Net.Mime)[^].

Content types are maintained and standardized by IANA, here:
Media Types,
Media Types (Text).

As you can see, you need "application/pdf". The data for this content (as with most other cases), should be represented in base64 form (Base64 - Wikipedia, the free encyclopedia[^]). As to ContentDisposition, it shows the mail message viewer how to display the part. It can be rendered in the single flow with main content of offer downloading, hence you have these options:
DispositionTypeNames.Inline Field (System.Net.Mime)[^],
DispositionTypeNames.Attachment Field (System.Net.Mime)[^].

With PDF, you certainly need the last name value for ContextDisposition, System.Net.Mime.DispositionTypeNames.Attachment.

Finally, sending mail. In .NET FCL, SMTP is supported: SmtpClient Class (System.Net.Mail)[^].

For other client protocols, you can easily find 3rd-party solutions, but SMTP is usually enough.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900