Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
greetings,
i am developing in VB.net and using VS2008
Question
How do I send multipart/alternative emails?

can it be done via Net.Mail? or is there an alternative package?
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Oct-11 2:43am    
All right, my 5.
--SA
Mehdi Gholam 5-Oct-11 3:46am    
Cheers, thanks
ok so i did it... it goes like this.
VB
Sub SendEmail()
'here we need a couple of things 
'1)an instance of System.Net.SmtpClient
'2)an instance of Net.Mail.MailMessage

'part 1
'declare
Dim oSmptServer as New System.net.SmtpClient
    oSmtpServer.Host = "my_SmtpServer.net" 'your connection url
    oSmtpServer.Timeout = 60 'integer in seconds
    oSmtpServer.Port = 25 'the connection port is by default
    oSmtpServer.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network

'part 2
    Dim username as String = "my_username"
    Dim password as String = "my_password"
    'setup the Credentials 
    oSmtpServer.Credentials = New Net.NetworkCredential(username. password)
    
    Dim email_to as String = "recepient_email@somemail.com"
    Dim email_from as String = "my_email_address@myhost.com"
    Dim Subject as String = "The Subject..."
    Dim email_body as string = "This is text/plain content"
    Dim message as Net.Mail.MailMessage(email_from, email_to)

    'set it up
    message.Subject = Subject
    message.body = email_body
    'Now upto here we have only plain text for the Email Client to display
    'next we will add the alternative!(HTML)
    
    'need an AlternativeView object
    'set it up like this
    'the Mail.AlternateView has many overloads 
    'One can provide the path to a html file for the 1st constructor property
    'the second will let the email client know we are sending       
    'multipart/alternative
     
    Dim html as New Mail.AlternateView("<html><body>Any html tah can go_         here</body></html>" , "text/html")
     'now we have to add this property to our message
     message.AlternateViews.add(html)
     'set the Encoding
     message.Encoding = System.Text.Encoding.UTF8 'standard
     
     'finally send it out
     Dim user_state as String = " -Token"
     oSmtpServer.SendAsync(message,user_state)
End Sub


This method will cover you in case the email client has html disabled!
then they will not see the markup instead they will see your text version of
the email which you would supply to the String called email_body

Thats all.
enjoy
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900