Auto Email Code

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
4 posts Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Auto Email Code
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
Image
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: Auto Email Code
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
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Auto Email Code
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;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: Auto Email Code
Scottie1972
Yeah, That worked....Just what I was looking for.
Image
4 posts Page 1 of 1
Return to “Tutorial Requests”