Last post Feb 06, 2015 02:14 PM by gerrylowry
Feb 05, 2015 05:43 PM|damon2012|LINK
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,
Feb 05, 2015 06:09 PM|gerrylowry|LINK
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.
Feb 05, 2015 06:15 PM|Markus33|LINK
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!
Feb 06, 2015 03:06 AM|damon2012|LINK
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?
Feb 06, 2015 09:37 AM|Markus33|LINK
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.
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
Note: The ampersand ‘@’ in C#
specifies an “as-is” string, where
escape sequences normally
denoted by the backslash character
static private void InitDefaultDir()
if (_defaultDir == null)
_defaultDir = Environment.GetFolderPath(
_defaultDir += @"\Albums";
static public string DefaultDir
_initializeDir = false;
_defaultDir = value;
_initializeDir = true;
Feb 06, 2015 02:14 PM|gerrylowry|LINK
(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.