Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can i send an email using vb.net code?


What I have tried:

How can i send an email using vb.net code?
Posted
Updated 25-Oct-19 10:23am

the following link shows how to send email via an smtp server

How to send email from VB.NET[^]
 
Share this answer
 
Comments
Hani Mustafa 29-Aug-19 4:51am    
it's OK but an exception appears as below when sending the email:

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. o9sm1372607wrm.88 - gsmtp
Simon_Whale 29-Aug-19 4:57am    
Have a read of this https://stackoverflow.com/questions/2766928/how-to-set-username-and-password-for-smtpclient-object-in-net. I know it is in C# but it should give you what you need to set the credentials
See here: Sending an Email in C# with or without attachments: generic routine.[^]
The code is C#, but it's pretty obvious and online converters such as this one Code Converter C# to VB and VB to C# – Telerik[^] will help if you really can't cope.
 
Share this answer
 
You will have to add a few Resources for Email information.. And pass the sub some info .. But this should handle what you are looking for.. Make sure your email server can accept the number of emails being sent otherwise it might crap out. But this works in production.

Public Sub SendMailMessage(ByVal recepients As List(Of String), ByVal subject As String, ByVal body As String, Optional fle As List(Of String) = Nothing)
       ' Instantiate a new instance of MailMessage
       ' Set the sender address of the mail message
       Dim mMailMessage As New MailMessage() With {
           .From = New MailAddress(My.Resources.AdminEmailUserName + "@domain.com")
       }
       ' Set the recepient address of the mail message
       For Each r As String In recepients
           mMailMessage.To.Add(New MailAddress(r))
       Next

       ' Set the subject of the mail message
       mMailMessage.Subject = Mid(subject, 1, 50)
       ' Set the body of the mail message
       mMailMessage.Body = body

       If fle IsNot Nothing Then
           For x As Integer = 0 To fle.Count - 1
               If Not String.IsNullOrEmpty(fle.Item(x)) Then
                   mMailMessage.Attachments.Add(New Attachment(fle.Item(x)))
               End If
           Next
       End If

       ' Set the format of the mail message body as HTML
       mMailMessage.IsBodyHtml = True
       ' Set the priority of the mail message to normal
       mMailMessage.Priority = MailPriority.Normal

       ' Instantiate a new instance of SmtpClient
       'mSmtpClient.EnableSsl = True
       Dim mSmtpClient As New SmtpClient() With {
           .Host = My.Resources.ExchServer,
           .Port = 587,
           .Credentials = New NetworkCredential(My.Resources.AdminEmailUserName, My.Resources.AdminEmailPassword)
       }
       ' Send the mail message
       mSmtpClient.Send(mMailMessage)


   End Sub
 
Share this answer
 

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