Click here to Skip to main content
15,885,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I coded this so I am able to send emails.
VB
Imports System.Net.Mail
Public Class Form1
    Function SendEmail(ByVal Recipients As List(Of String), _
                      ByVal FromAddress As String, _
                      ByVal Subject As String, _
                      ByVal Body As String, _
                      ByVal UserName As String, _
                      ByVal Password As String, _
                      Optional ByVal Server As String = "smtp.gmail.com", _
                      Optional ByVal Port As Integer = 587, _
                      Optional ByVal Attachments As List(Of String) = Nothing) As String
        Dim Email As New MailMessage()
        Try
            Dim SMTPServer As New SmtpClient
            For Each Attachment As String In Attachments
                Email.Attachments.Add(New Attachment(Attachment))
            Next
            Email.From = New MailAddress(FromAddress)
            For Each Recipient As String In Recipients
                Email.To.Add(Recipient)
            Next
            Email.Subject = Subject
            Email.Body = Body
            SMTPServer.Host = Server
            SMTPServer.Port = Port
            SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
            SMTPServer.EnableSsl = True
            SMTPServer.Send(Email)
            Email.Dispose()
            Return "Email to " & Recipients(0) & " from " & FromAddress & " was sent."
        Catch ex As SmtpException
            Email.Dispose()
            Return "Sending Email Failed. Smtp Error."
        Catch ex As ArgumentOutOfRangeException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        Catch Ex As InvalidOperationException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        End Try
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Recipients As New List(Of String)
        Recipients.Add(MailTextBox.Text)
        Dim FromEmailAddress As String = Recipients(0)
        Dim Subject As String = "Test From VB."
        Dim Body As String = "email body text, if you are reading this from your gmail account, the program worked."
        Dim UserName As String = "GMAIL USERNAME WITHOUT  (@GMAIL>COM)"
        Dim Password As String = "Password"
        Dim Port As Integer = 587
        Dim Server As String = "smtp.gmail.com"
        Dim Attachments As New List(Of String)
        MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
    End Sub
End Class

My problem is that it doesn't send the email to the email adress specified in the mailtextbox! In fact, it doesn't send it at all!Can you help me? Can you help me to modify the code so I can send the email to multiple recipients?
Thanks!!!
Posted
Updated 5-Aug-14 9:34am
Comments
[no name] 5-Aug-14 16:01pm    
So what is the error that you are getting?
Mike Vlast 5-Aug-14 16:08pm    
It doesn't show any error, it just does not send the email to the mail adress specified in the mailtextbox.Hope you understand.
[no name] 5-Aug-14 16:19pm    
Well given that I do not have access to your screen or what email address it is that you are using, there are only 2 things that I would suggest, 1) use a valid email address and gmail credentials and 2) set the using default credentials to false.
Don.Coleone 6-Aug-14 23:35pm    
Perhpas it has something to do with SSL/TLS. You know...for safety consideration, your application needs to do more rather than some simple steps based on SMTP. My suggesstion is that you check out respondings from your SMTP mail server. Sorry I'm not a .NET developer, so my way of doing so is using winsock control provided by VB to send SMTP cmds and check data returned. Usually, a [-ERR + Error Description] formatted response is returned if an error occurs.

Works or not, Just my guess...

I think it's a firewall / antivirus problem... check if your antivirus / firewall blocks the application.
I have tried the code and it works 100% but system mechanics professional asked me to allow the connection to RemotePort 587 because it is not a 'common one'.
 
Share this answer
 
try Adding this
VB
Email.[To].Add(New MailAddress(Recipient))
instead of
VB
Email.To.Add(Recipient)


hope this help :)
 
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