Page 1 of 1

Auto Email Code

Posted: Sat Feb 20, 2010 3:29 pm
by Scottie1972
Hello all,
I need a Module that will allow my Usercontrol to send a Basic email to the user after registration.
I do have my on SMTP so thats not a problem. Its does NOT require SSL.

I just need it to send a generated password to the user email account. I have the password generator made and working. I just need a simple code that will email the code to the user.

Is there any one that has done something like this?

Scottie

Re: Auto Email Code

Posted: Sat Feb 20, 2010 4:42 pm
by Scottie1972
I found this simple code.
For Google Gmail Server.
Code: Select all
Dim smtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        smtpServer.Credentials = New Net.NetworkCredential(smtpUser, smtpPass)
        'using gmail
        smtpServer.Port = 587
        smtpServer.Host = smtpServ
        smtpServer.EnableSsl = True
        mail = New MailMessage()
        mail.From = New MailAddress(fromUser)
        mail.To.Add(toUser)
        mail.Subject = (regSub)
        mail.Body = TextBox1.Text
        smtpServer.Send(mail)
        MsgBox("Mail Sent Succesfull!")
Now can anyone help me put it in a function instead of a click event?

Scottie

Re: Auto Email Code

Posted: Sat Feb 20, 2010 9:02 pm
by CodenStuff
Hello Scottie,

You could just put it into a sub like this:
Code: Select all
 Public Sub SendEmail(ByVal EmailAddress As String)
        Dim from As New MailAddress("your@email.com")
        Dim [to] As New MailAddress(EmailAddress)
        Dim theMailMessage As New MailMessage(from, [to])
        theMailMessage.Body = "This is the message I want to send to the user."
        theMailMessage.Subject = "Email Subject"
        Dim theClient As New SmtpClient("mail.server.com") 'EMAIL SERVER
        theClient.UseDefaultCredentials = False
        Dim theCredential As New System.Net.NetworkCredential("MyEmailUsername", "MyEmailPassword")
        theClient.Credentials = theCredential
        theClient.EnableSsl = True
        theClient.Port = 587
        Try
            theClient.Send(theMailMessage)
        Catch wex As Net.WebException
            MessageBox.Show("Could not send email...check your settings.")
        End Try
        MsgBox("E-mail successfully sent")
    End Sub
Change the server settings in the code. And then use it by using this code whenever you want to send the email:
Code: Select all
SendEmail("Someone@email.com")
And that will send an email to the address you specify.

Hope that helps cooll;

Re: Auto Email Code

Posted: Fri Feb 26, 2010 4:46 am
by Scottie1972
Yeah, That worked....Just what I was looking for.