Avatar of German Mikulski
German Mikulski
 asked on

Sending emails from a web form

Hello,

I am trying to make my asp.net web form send an email from @gmail.com to @gmail.com email address. Using code provided below. Obviously, "username@gmail.com" is replaced with the email address that sends an email and etc.

   Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
        Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("yourusername@gmail.com")
            mail.To.Add("recieveraddress")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window


When the button is pressed, I receive the error message:
Screenshot---27-02-2015---17-12-46.png

How can I fix it? Thanks!
ASP.NETEmail ServersVisual Basic.NET

Avatar of undefined
Last Comment
JServicesLLC

8/22/2022 - Mon
Salah Eddine ELMRABET

Hi,

It seem this is authentication problem! have you correctly define the password? also the port you use : 587 is not working I tried a telnet to google SMTP using this port without success.

Basically you can use default smtp port 25 or 465 (secured port).

Best regards.

Salah
JServicesLLC

Have you followed the following steps?

Enable IMAP in your Gmail settings

Sign in to Gmail.
Click the gear in the top right .
Select Settings.
Click Forwarding and POP/IMAP.
Select Enable IMAP.
Click Save Changes.

I have some code laying around somewhere that I have used to send email with gmail if you need it.  Just let me know.   It's in VB.NET

EDIT:
Also you are passing the password instead of "password" correct?
German Mikulski

ASKER
Hi,

I have tried to use ports your offered, but none of them worked - -25 gave the same error, and 465 was just loading for 15 minutes and did nothing eventually.

Yes, IMAP was enables.
Hm,  pretty sure password is right, I have tried two different email addresses, none works. When I type password in, does it look like "********"? Do In need any additional ''?.. Just in case

And yes, I would like to see your code if you don't mind explaining in breif how it works)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
JServicesLLC

I will find it.   I have to finish up a small project and then I will see what I can do.
Salah Eddine ELMRABET

Hi,

Do you use texbox type password in the form?? the "*********" you see is it in the web page display when you fill the form or when you edit the script??

try to use plain text texbox and see if you can send the email, if this is the case try to find what happened within you code in the script

regards.

Salah
JServicesLLC

I found it.   I am modifying to as I have a whole bunch of application specific items in it.      I will post it here in a few minutes.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
German Mikulski

ASKER
Salah,

No, I just type the password directly into the code.
ASKER CERTIFIED SOLUTION
JServicesLLC

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JServicesLLC

I just edited the code snip to include the Function definition.    ooops.