Knil92 Posted August 8, 2017 Posted August 8, 2017 (edited) So I've created a little app in visual studio, it's a windows form app .net framework and I was wondering if some one can take a look at this code and tell me if they can spot anything that im not. I'm getting an error saying value type of 'string' cannot be converted to 'attachment'. The app is basically a form and when you are finished filling in the form, it screenshots itself and saves it as a jpeg in the same directory that the .exe file is in. What I want it to do is to send the jpeg file embedded in a html e-mail. So here's what I have for the e-mail section, it works when I remove the attachment section but then the img src doesnt work; Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click Try Dim finDt2 As String = DateTime.Now.ToString("dd-MM-yyyy") Dim Smtp_Server As New SmtpClient Dim e_mail As New MailMessage() Dim imgname As String = finDt2 & " " & TextBox1.Text & " " & TextBox2.Text & ".jpeg" Smtp_Server.UseDefaultCredentials = False Smtp_Server.Credentials = New Net.NetworkCredential("[email protected]", "t3st3r") Smtp_Server.Port = 587 Smtp_Server.EnableSsl = True Smtp_Server.Host = "smtp.gmail.com" e_mail = New MailMessage() e_mail.From = New MailAddress("[email protected]") e_mail.To.Add("[email protected]") e_mail.Subject = "Email Sending" 'The Part that isnt working e_mail.Attachments.Add(imgname) e_mail.IsBodyHtml = True e_mail.Body = " & Chr(34) & " End Try End Sub Edited August 8, 2017 by Knil92
SteveM555 Posted August 8, 2017 Posted August 8, 2017 It's because it takes an 'Attachment' type and not a string... Try this.. Dim fileAttachment As Attachment = New Attachment(imgname) e_mail.Attachments.Add(fileAttachment)
Knil92 Posted August 8, 2017 Author Posted August 8, 2017 You sir, are a genius. Can you tell me if im using the message body section correctly as well? e_mail.Body = " & Chr(34) & imgname & Chr(34) & " It puts in an empty box which is 304 pixels wide and 228 pixels high, and when i inspect the element, it comes up with it was doing the same with e_mail.Body = " & Chr(34) & " Thanks for your help by the way.
SteveM555 Posted August 8, 2017 Posted August 8, 2017 (edited) It's probably to do with the escaping of the " character.. You can double up the " to escape it.. e.g. e_mail.Body = "" would output: cid:08-08-2017 .jpeg Hope that helps? Edited August 8, 2017 by SteveM555 2
Knil92 Posted August 9, 2017 Author Posted August 9, 2017 i hate visual studio some times XD I tried escaping the " with """ but it wasnt working for me when I tried, copy and pasted your version and it likes it lol but yeah in the e-mail im getting the attached file and then a blank embedded image and when i inspect it i get; cid:09-08-2017 1234 123456.jpeg
Bedders Posted August 9, 2017 Posted August 9, 2017 I would think that is because the SRC of the image is in the body of the email, and the email server will need to be able to see that image (where it is saved)?
Knil92 Posted August 9, 2017 Author Posted August 9, 2017 Yeah, its because it can't find the src of the image, but I've attached the image to the e-mail, I though it would be a simple img src="filename.jpeg" but that doesnt work, from googling I've seen that I need to use the cid: but I think now I have to get the content ID of the attached picture.
Knil92 Posted August 11, 2017 Author Posted August 11, 2017 So it turns out that I dont really need to embed the image in the e-mail any way, as the thing im send it to takes the embedded picture and turns it into an attachment any ways. Thanks for your help any ways with getting it to attach properly, it was a huge help. 1
QwertyMash Posted August 19, 2017 Posted August 19, 2017 If you like I can help you develop this in C#
Knil92 Posted August 29, 2017 Author Posted August 29, 2017 I've had a look at the arduino IDE and from what I understand thats basically a modded version of C# and I'm not going to lie, but I found that completely confusing. Dont get me wrong, I would love to learn C# but it would probably result in you headbutting your desk, thanks for the offer though.
QwertyMash Posted August 29, 2017 Posted August 29, 2017 I've had a look at the arduino IDE and from what I understand thats basically a modded version of C# and I'm not going to lie, but I found that completely confusing. Dont get me wrong, I would love to learn C# but it would probably result in you headbutting your desk, thanks for the offer though. Hehe well tell you what PM me if you ever just want me to teach you C# and see how you like it I used to teach it and I love helping overs learn it! 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now