-
Jun 17th, 2019, 10:04 AM
#1
Thread Starter
Member
[RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?
Hi, friends! can you help me, please?
I am developing a emall program that sends emails with attached documents. The documents are of the type: jpg and pdf.
But I'd like to send docx-type files. However, there is no type of MediatypeName that matches the docx format. The office does not work with rtf files. I've already tried using the media type "Octet", but the file gets corrupted. Below is my code about send e-mails and attachments.
Code:
Dim IMG As LinkedResource
Private Sub btnsendemail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsendemail.Click
If listboxdoc.SelectedIndex >= 0 Then
If txtboxemaildestinatario.Text.Length > 0 Then
Dim myfilespath As String = listboxclients.SelectedItem.ToString()
txtboxhtmlemail.Text = "Hi! document attached. Best Regards."
Dim MyHTML As String = txtboxhtmlemail.Text
Dim typeHTML As AlternateView = AlternateView.CreateAlternateViewFromString(MyHTML, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)
Try
If listboxdoc.SelectedItem.ToString().Contains(".jpg") Then
IMG = New LinkedResource(Application.StartupPath + "\" + myfilespath + "\docs\" + listboxdoc.SelectedItem.ToString(), System.Net.Mime.MediaTypeNames.Image.Jpeg)
End If
If listboxdoc.SelectedItem.ToString().Contains(".pdf") Then
IMG = New LinkedResource(Application.StartupPath + "\" + myfilespath + "\docs\" + listboxdoc.SelectedItem.ToString(), System.Net.Mime.MediaTypeNames.Image.Jpeg)
End If
IMG.ContentId = "Files"
Dim messages As MailMessage = New MailMessage
messages.AlternateViews.Add(typeHTML)
messages.From = New MailAddress("lopezadvocates@gmail.com")
messages.To.Add(txtboxemaildestiny.Text)
messages.Subject = "documents from " + listboxclients.SelectedItem.ToString()
typeHTML.LinkedResources.Add(IMG)
Dim MISMT As SmtpClient = New SmtpClient("SMTP.GMAIL.COM")
MISMT.EnableSsl = True
MISMT.Port = "587"
MISMT.Credentials = New Net.NetworkCredential("blahblahblah", "blahblahblah")
MISMT.Send(messages)
MsgBox("Message sent!", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("Please, insert destiny e-mail.", MsgBoxStyle.Exclamation)
End If
Else
MsgBox("Please, select a document.", MsgBoxStyle.Exclamation)
End If
End Sub
So, friends, how can I send attachments in docx format? do I need to use another newer version of Visual Studio? if this is a solution, I will unfortunately not be able to complete my program. Because I'm working on a machine that uses Windows 7 64 bit. Versions after VS 2010 do not work well here. Please help me.
Thank you all in advance.
Last edited by vbnewbieuser; Jun 17th, 2019 at 12:40 PM.
-
Jun 17th, 2019, 10:30 AM
#2
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
You say that you want to attach documents but you're not. You're embedding them. If you just attached them then you wouldn't have this issue. Is there a specific reason that you're not just adding attachments? The only reason that I've used embedded resources before is to display an image in an HTML email. That's probably why there's no value of the type you're looking for: because embedding a Word document doesn't make sense.
-
Jun 17th, 2019, 10:30 AM
#3
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Show the code that works with jpg or pdf please.
-
Jun 17th, 2019, 10:44 AM
#4
Thread Starter
Member
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Originally Posted by jmcilhinney
You say that you want to attach documents but you're not. You're embedding them. If you just attached them then you wouldn't have this issue. Is there a specific reason that you're not just adding attachments? The only reason that I've used embedded resources before is to display an image in an HTML email. That's probably why there's no value of the type you're looking for: because embedding a Word document doesn't make sense.
Thank you for your help, Sir. But, with linkedresource, I can send pdf documents too. I work in an office which handles documents of type jpg, pdf and docx only. I also tried attaching using the vb.net "attachment" method, even then, it did not work for docx. Bur, thank you for your suggestion.
-
Jun 17th, 2019, 10:44 AM
#5
Thread Starter
Member
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Originally Posted by dbasnett
Show the code that works with jpg or pdf please.
Hi,Sir.
The functional code for jpg and pdf files is described in my first post. Thanks in advance.
Last edited by vbnewbieuser; Jun 17th, 2019 at 10:52 AM.
-
Jun 17th, 2019, 10:51 AM
#6
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
You can send any type of file using attachments. That includes JPG, PDF, DOCX and any other file type you care to mention. If you tried to do it and it didn't work then you did it wrong. If you show us what you did and tell us what happened when you did it, we can probably tell you what's wrong with it.
That said, the LinkedResource constructor is overloaded and will accept a String containing a file path or a Stream containing data, with or without a content type or media type. What happens if you just create a FileStream on your DOCX file and pass that alone to the LinkedResource constructor?
-
Jun 17th, 2019, 11:08 AM
#7
Thread Starter
Member
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Originally Posted by jmcilhinney
You can send any type of file using attachments. That includes JPG, PDF, DOCX and any other file type you care to mention. If you tried to do it and it didn't work then you did it wrong. If you show us what you did and tell us what happened when you did it, we can probably tell you what's wrong [...]
Got it, sir. I used the attachment method as test and erased it. When I sent the email, the docx file became corrupted. I'm going to do a test again using this method, just selecting the docx. I'll put the code available here soon. I just need some time for that, because I'm on a work schedule with many things to be resolved at the same time. But I will make the code available as soon as I finish. Thank you very much for your help.
-
Jun 17th, 2019, 12:12 PM
#8
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Just an FYI that the System.Net.Mail.SmtpClient is being obsoleted, and it's being recommended to use the third-party library MailKit.
-
Jun 17th, 2019, 12:26 PM
#9
Thread Starter
Member
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
Originally Posted by topshot
I managed to find a solution and I'll post next. Sir, I greatly appreciate your suggestion. As soon as I'm home, I'll search on and download the library you mentioned. Thank you very much!
-
Jun 17th, 2019, 12:38 PM
#10
Thread Starter
Member
Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?
I'll consider the case resolved. I wrote the code again using the atachament method. I think the first time, I must have made some mistake that caused the error of not sending the file properly. Now it's working. I'll put the following functional code. Again, as always, I appreciate the help of the forum and specially Sir jmcilhinney, with his always welcome suggestions.
Fixed and working code:
Code:
Private Sub btnsendemail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsendemail.Click
If listboxdoc.SelectedIndex >= 0 Then
If txtboxemaildestiny.Text.Length > 0 Then
Dim myfilespath As String = listboxclients.SelectedItem.ToString()
'Dim arquivosdir As New System.IO.DirectoryInfo(Application.StartupPath + "\" + myfilespath + "\docs\" + listboxdoc.SelectedItem.ToString())
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim attachment As System.Net.Mail.Attachment
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blahblahblahblah", "blahblablahblahblah") ' well, can't show the company credentials, right? XD
'sending failure
Smtp_Server.Port = "587" 'cannot be read
Smtp_Server.EnableSsl = True 'cannot be read
Smtp_Server.Host = "SMTP.GMAIL.COM" 'cannot be read
e_mail = New MailMessage()
e_mail.From = New MailAddress("lopezadvocates@gmail.com")
e_mail.To.Add(txtboxemaildestiny.Text)
e_mail.Subject = "Requested documentation."
e_mail.IsBodyHtml = True
e_mail.Body = "Hi! the requested document is attached."
'this line here excute correctly but if a user didd'nt attach a file, sending fails..
'i want to send even w/o an attach file..
attachment = New System.Net.Mail.Attachment(Application.StartupPath + "\" + myfilespath + "\docs\" + listboxdoc.SelectedItem.ToString()) 'file path
e_mail.Attachments.Add(attachment) 'attachment
Smtp_Server.Send(e_mail)
MsgBox("Message sent.", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("Please, select a e-mail destiny.", MsgBoxStyle.Exclamation)
End If
Else
MsgBox("Please, select a document.", MsgBoxStyle.Exclamation)
End If
End Sub
Best Regards.
Last edited by vbnewbieuser; Jun 18th, 2019 at 09:49 AM.
-
Jun 17th, 2019, 07:54 PM
#11
Re: [RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?
Be sure to dispose each Attachment object after sending the message, otherwise the files will remain locked. Also, you should acquaint yourself with the Path.Combine method. Rather than this:
vb.net Code:
attachment = New System.Net.Mail.Attachment(Application.StartupPath + "" + myfilespath + "\docs" + listboxdoc.SelectedItem.ToString()) 'file path
do this:
vb.net Code:
attachment = New System.Net.Mail.Attachment(IO.Path.Combine(Application.StartupPath, myfilespath, "docs", listboxdoc.Text)) 'file path
That method will make sure that one and only one separator is used between each pair of partial paths, whether either or both include leading or trailing separators or not.
-
Jun 17th, 2019, 07:58 PM
#12
Re: [RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?
That said, maybe you should populate the ListBox with both the name and path of the files. If you create a DirectoryInfo for the folder containing the files, call GetFiles and bind the result to the ListBox, you can display only the file names but then get the full path from the selection, e.g.
vb.net Code:
Dim folder As New DirectoryInfo(Path.Combine(Application.StartupPath, myfilespath, "docs")) Dim files = folder.GetFiles() With listboxdoc .DisplayMember = "Name" .ValueMember = "FullName" .DataSource = files End With
vb.net Code:
attachment = New System.Net.Mail.Attachment(CStr(listboxdoc.SelectedValue)) 'file path
-
Jun 18th, 2019, 09:52 AM
#13
Thread Starter
Member
Re: [RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?
Originally Posted by jmcilhinney
That said, maybe you should populate the ListBox with both the name and path of the files. If you create a DirectoryInfo for the folder containing the files, call GetFiles and bind the result to the ListBox, you can display only the file names but then get the full path from the selection, e.g.
vb.net Code:
Dim folder As New DirectoryInfo(Path.Combine(Application.StartupPath, myfilespath, "docs"))
Dim files = folder.GetFiles()
With listboxdoc
.DisplayMember = "Name"
.ValueMember = "FullName"
.DataSource = files
End With
vb.net Code:
attachment = New System.Net.Mail.Attachment(CStr(listboxdoc.SelectedValue)) 'file path
Sorry for taking time to respond. My expedient is 12 hours a day, sometimes I get home, take a shower and sleep in the blink of an eye. One more time I thank you, Sir jmcilhinney. In this way you have suggested, it is much more efficient than the way I have been doing it. Thank you! awesome!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|