user2264202
user2264202

Reputation: 63

Sending email to users in vb.net

ok so im trying so send a email to a user who enters their email into a textbox. i got a basic idea of whats i should do but i am confused where i should put for , CMTPClient(), the credentials, and from. what email should go there. i tryed puting in my email address and credentials but i keep getting this error " the SMTP server requires a sercure connection or the client was not autherticated". here is my code

        Try
        Dim username As String
        username = TextBox1.Text
        Dim SmtpServer As New SmtpClient("smtp.gmail.com")
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New System.Net.NetworkCredential("what username goes 
        here", "what password goes here")
        SmtpServer.Port = 587
        mail = New MailMessage()
        mail.From = New MailAddress("what email should i put here")
        mail.To.Add(username)
        mail.Subject = "Qustions"
        mail.Body = "This is for testing your mother"
        SmtpServer.Send(mail)
        MsgBox("mail send")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Upvotes: 0

Views: 857

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415830

Keep in mind that gmail isn't just open for all to use. Otherwise, well, you know: spam. Right now spammers have to jump through all kinds of hoops to find or create usable smtp gateways. If any of the major providers left themselves open, think of all the spam you get right now and multiply it by about a 1000.

So you need to have an account with gmail to use their smtp server. More than that, if you send more than about 400 e-mails per day you may need either a special account with them, or an account with a special, reputable mass mailer. These mailers will charge you per message, but it's worth it, because otherwise more than a few messages per day will attract notice and result in anything you send being filtered as spam... and this while still in transit, before it even reaches the destination's mail host to be checked there as well. You're basically paying a nuisance fee, to ensure that your messages are in earnest, and not mass junk.

That out of the way, if you know your volume is low enough, and after you have a valid gmail account, you just need a few changes:

Try
    Dim SmtpServer As New SmtpClient("smtp.gmail.com")
    Dim mail As New MailMessage()
    SmtpServer.Credentials = New System.Net.NetworkCredential("[email protected]", "your gmail password")
    SmtpServer.EnableSsl = True
    SmtpServer.Port = 587
    mail = New MailMessage()
    mail.From = New MailAddress("[email protected]")
    mail.To.Add(TextBox1.Text)
    mail.Subject = "Questions"
    mail.Body = "This is for testing your mother"
    SmtpServer.Send(mail)
    MsgBox("mail sent")
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

Upvotes: 0

Krishanu Dey
Krishanu Dey

Reputation: 6406

use SmtpServer.EnableSsl = True before calling SmtpServer.Send(mail)

"the SMTP server requires a secure connection or the client was not authenticated"

means the SMTP Server works on a SSL Enabled (encrypted) connection

Upvotes: 1

Related Questions