Email pdf file

KRP

Member
Joined
Jan 9, 2010
Messages
9
Programming Experience
Beginner
Wondering if there is a way to email a pdf file. What I am trying to do is allow the user to fill out the textboxes and attach a pdf file and hit the send button to email these back to me.

I have two buttons on my form. One takes the inputs from textboxes and emails them. The other lets the user browse and load a pdf file and loads it using the webBrowser control which I have the visability set to false.

What I am trying to do is add code to the button that sends the email that will also send the pdf file from the webBrowser control.

Am I trying to go about this the wrong way? Here is the code that I have for the button that sends the email. Thanks for any help.




Dim mymailmsg As New MailMessage
Dim a As String = TextBox5.Text
Dim b As String = TextBox1.Text
Dim c As String = TextBox2.Text
Dim d As String = TextBox3.Text
Dim f As String = TextBox4.Text
Dim g As String = TextBox6.Text


Try

mymailmsg.From = New MailAddress(xxxx@gmail.com")
mymailmsg.To.Add(xxx@aol.com")
mymailmsg.Subject = "Waste Report"
mymailmsg.Body = a & vbCrLf & b & vbCrLf & c & vbCrLf & d & vbCrLf & f & vbCrLf & g

Dim smtp As New SmtpClient("smtp.gmail.com")
smtp.Port = 587
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "password")
smtp.Send(mymailmsg)
MsgBox("Sent")

Catch ex As Exception

End Try


And the code for the button that lets the user browse and load the pdf file

Dim OpenFileDialog1 As New OpenFileDialog()

Dim Response As DialogResult
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "PDF Files(*.pdf)|*.pdf|All Files(*.*)|*.*"
Response = OpenFileDialog1.ShowDialog()
If Response <> Windows.Forms.DialogResult.Cancel Then
If OpenFileDialog1.FileName <> "" Then
WebBrowser1.Navigate(OpenFileDialog1.FileName)
End If
End If
End Sub
 
The MailMessage class has an Attachments property, to which you can Add Attachment objects. You should check out the property documentation for a code example. It's in C# but it's fairly obvious what's going on.
 
System.Net.Mail, OH MY! is also a good resource, see for example the "3.4 Working with Attachments" section.

Learning to use the help functionality and the documentation for .Net Framework class library is still priority one, but you can always supplement that with any resource you can find on the web.
 
Thanks guys for the links. I'm new to programming and I can't figure out how to do what I want to do after reading the pages.

It looks to me that the code samples shown allows you to email a pdf file that is located on the computer by specifying a path to the file. I have tried to alter the code with no luck.

What I would like to do is let the user browse for a MSDS pdf file on their computer, select it, and have this emailed to me.

I may be going about it the wrong way but what I have so far is that I can browse for the pdf file anywhere on the computer and when I open the file it opens the pdf file in the pdfreader on the form which I have the visibility set to false.

I am trying to find a way to get the pdf file that the user opens in the mymailmsg.body to be emailed to me instead of needing to specify a file path.

Again, I really appreciate the help and sorry for my ignorance. This stuff is like a foreign language to this 42 year old brain lol.
 
I am trying to find a way to get the pdf file that the user opens in the mymailmsg.body to be emailed to me instead of needing to specify a file path.
That's probably not practical. You'd have to use a component that understood how to read PDFs as there's no PDF support built into the Framework. You'd have to get that component to read the contents of the PDF and then add that to the Body of your MailMessage. I'd suggest that it's much easier to simply attach the PDF file to the email. Is there a particular reason you don't want to do that?
 
That's probably not practical. You'd have to use a component that understood how to read PDFs as there's no PDF support built into the Framework. You'd have to get that component to read the contents of the PDF and then add that to the Body of your MailMessage. I'd suggest that it's much easier to simply attach the PDF file to the email. Is there a particular reason you don't want to do that?

Sorry, I may not be explaining what I am trying to do in the right way or misunderstanding.

I am trying to let the user tell me in the textboxes what chemical he or she has a problem with along with the company name and location. Then scan a random pdf of an MSDS sheet depending on what chemical the user's company spills (to save me time from having to look it up) and have this emailed.

Please correct me if I am wrong but from what I can tell, the samples in the links only allows you to attach a pdf file if you know the path of its location thus being stuck to that one file. My problem is that I don't know which MSDS sheet out of the thousands the user will need to send me.

Am I thinking about all this wrong? Is this not practical? Again, thank you very much for your help. This forum has taught me a lot and I appreciate greatly the resource.
 
Thanks for the help guys. I still haven't got the code right to make it work but I won't give up until I do lol.
 
Two questions:

1. Do you know how to display an OpenFileDialog and get the path of the file selected by the user?

2. Assuming that you have the file path, do you know how to attach a file to a MailMessage?

Tackle those two problems separately and then simply put them together. The output of the first, i.e. the file path, becomes the input for the second. It's basically just creating two black boxes and then connecting them with a pipe. That's how programming works in general: you solve lots of little subproblems to create components that can then be plumbed together in whatever way suits the application.
 
Two questions:

1. Do you know how to display an OpenFileDialog and get the path of the file selected by the user?

2. Assuming that you have the file path, do you know how to attach a file to a MailMessage?

Tackle those two problems separately and then simply put them together. The output of the first, i.e. the file path, becomes the input for the second. It's basically just creating two black boxes and then connecting them with a pipe. That's how programming works in general: you solve lots of little subproblems to create components that can then be plumbed together in whatever way suits the application.


Thank you for the help. This made me think about what I was trying to do in a different way. I now have it working. You guys have been a great help. Now on to the next step:D
 
Back
Top