Answered by:
How can I attach a pdf to an email in memory?

Question
-
User-257409430 posted
Hi,
I have a pdf generatoir that returns pdfresult. That can be retrieved by clicking on a browser link and the pdf is downloaded to a local directory.
But how can I take that pdf and add it as an attachment to an email message. I can already send the emails. I can store the pdf in a var, but it errors if I try to do something like email.attachment(varpdf).
Do I need to do something like use a stream reader or writer first before attempting to attach it to an email?
Any help appreciated,
Thanks
Thursday, February 5, 2015 5:43 PM
Answers
-
User1421620300 posted
You must have a directory by default it will be store whatever you save to it once you create this directory you can start from there everything is stored in memory. Try the Stackoverflow.com website for some examples from experiecne programmers there may be some samples there.
Courtesy of: Erik Brown Windows froms programming in C#
CurrentDirectory Gets or sets the fully qualified path of the current
directory for this process.using System.IO;
2 Define static members for the
default directory and whether this
directory has been initialized.
static private string _defaultDir = null;
static private bool _initializeDir = true;
3 Define a static InitDefaultDir
method to initialize the default
directory setting.
Note: The ampersand ‘@’ in C#
specifies an “as-is” string, where
escape sequences normally
denoted by the backslash character
are ignored.
static private void InitDefaultDir()
{
if (_defaultDir == null)
{
_defaultDir = Environment.GetFolderPath(
Environment.SpecialFolder.Personal);
_defaultDir += @"\Albums";
}
Directory.CreateDirectory(_defaultDir);static public string DefaultDir
{
get
{
if (_initializeDir)
{
InitDefaultDir();
_initializeDir = false;
}
return _defaultDir;
}
set
{
_defaultDir = value;
_initializeDir = true;}
}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 6, 2015 9:37 AM -
User-434868552 posted
(a) "current directly" usually means the directory from which a computer program was launched.
Example: you've written a program to perform some task and you save a file specifying only the file's name ... that file will likely be saved to the same directory as the one where the program exists.
Note: it's generally better to specify a full path so that you can control the location of saved files.
The current directory is often called the working directory. This Linux article gives some clarification (from a Linux perspective).
N.B.: in the given example code, the file was attached from the current directory as a matter of convenience, i.e., to simplify the example code.
(b) https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx "Attachment Class"
"Attachment content can be a String, Stream, or file name. You can specify the content in an attachment by using any of the Attachment constructors."
FWIW, your code is likely to be simpler if you save the file first to your chosen location and then attach the file from that location.
Pro: if the e-mail gets lost in cyberspace, you still have a copy of the attachment
Con: you need sufficient hard disk space for the number of attachments that you will be storing
Con: from time to time, you may need to delete or archive the saved attachments.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 6, 2015 2:14 PM
All replies
-
User-434868552 posted
it depends on how your creating your e-mails.
for example, see https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx
the example code at the above address simply attaches a file from the current directory.
Thursday, February 5, 2015 6:09 PM -
User1421620300 posted
I would try to present some code to help you, but I dont have that much familiarity in the area of emails. It may be possible to store the attachment in memory though. Take a look at this website it mentions filestream etc. Let me know what you come up with! Markus
How to send an Email using C# – complete features
www.intstrings.com/.../how-to-send-an-email-using-c-net-with-complete-..
Thursday, February 5, 2015 6:15 PM -
User-257409430 posted
thanks, when you say the "current directory" what do you mean by this? Which directory is this?
Also, is it possible to add an attachment without saving the attachment to a particular directory first?Friday, February 6, 2015 3:06 AM -
User1421620300 posted
You must have a directory by default it will be store whatever you save to it once you create this directory you can start from there everything is stored in memory. Try the Stackoverflow.com website for some examples from experiecne programmers there may be some samples there.
Courtesy of: Erik Brown Windows froms programming in C#
CurrentDirectory Gets or sets the fully qualified path of the current
directory for this process.using System.IO;
2 Define static members for the
default directory and whether this
directory has been initialized.
static private string _defaultDir = null;
static private bool _initializeDir = true;
3 Define a static InitDefaultDir
method to initialize the default
directory setting.
Note: The ampersand ‘@’ in C#
specifies an “as-is” string, where
escape sequences normally
denoted by the backslash character
are ignored.
static private void InitDefaultDir()
{
if (_defaultDir == null)
{
_defaultDir = Environment.GetFolderPath(
Environment.SpecialFolder.Personal);
_defaultDir += @"\Albums";
}
Directory.CreateDirectory(_defaultDir);static public string DefaultDir
{
get
{
if (_initializeDir)
{
InitDefaultDir();
_initializeDir = false;
}
return _defaultDir;
}
set
{
_defaultDir = value;
_initializeDir = true;}
}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 6, 2015 9:37 AM -
User-434868552 posted
(a) "current directly" usually means the directory from which a computer program was launched.
Example: you've written a program to perform some task and you save a file specifying only the file's name ... that file will likely be saved to the same directory as the one where the program exists.
Note: it's generally better to specify a full path so that you can control the location of saved files.
The current directory is often called the working directory. This Linux article gives some clarification (from a Linux perspective).
N.B.: in the given example code, the file was attached from the current directory as a matter of convenience, i.e., to simplify the example code.
(b) https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx "Attachment Class"
"Attachment content can be a String, Stream, or file name. You can specify the content in an attachment by using any of the Attachment constructors."
FWIW, your code is likely to be simpler if you save the file first to your chosen location and then attach the file from that location.
Pro: if the e-mail gets lost in cyberspace, you still have a copy of the attachment
Con: you need sufficient hard disk space for the number of attachments that you will be storing
Con: from time to time, you may need to delete or archive the saved attachments.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 6, 2015 2:14 PM