Results 1 to 13 of 13

Thread: [RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,748

    Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Show the code that works with jpg or pdf please.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Quote Originally Posted by jmcilhinney View Post
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Quote Originally Posted by dbasnett View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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?

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Quote Originally Posted by topshot View Post
    Just an FYI that the System.Net.Mail.SmtpClient is being obsoleted, and it's being recommended to use the third-party library MailKit.

    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!

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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:
    1. attachment = New System.Net.Mail.Attachment(Application.StartupPath + "" + myfilespath + "\docs" + listboxdoc.SelectedItem.ToString()) 'file path
    do this:
    vb.net Code:
    1. 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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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:
    1. Dim folder As New DirectoryInfo(Path.Combine(Application.StartupPath, myfilespath, "docs"))
    2. Dim files = folder.GetFiles()
    3.  
    4. With listboxdoc
    5.     .DisplayMember = "Name"
    6.     .ValueMember = "FullName"
    7.     .DataSource = files
    8. End With
    vb.net Code:
    1. attachment = New System.Net.Mail.Attachment(CStr(listboxdoc.SelectedValue)) 'file path

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: [RESOLVED} - VB 2010 - help for *.docx as attachment in System.Net.Mail?

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Dim folder As New DirectoryInfo(Path.Combine(Application.StartupPath, myfilespath, "docs"))
    2. Dim files = folder.GetFiles()
    3.  
    4. With listboxdoc
    5.     .DisplayMember = "Name"
    6.     .ValueMember = "FullName"
    7.     .DataSource = files
    8. End With
    vb.net Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width