7

I am a ASP novice and am troubleshooting a form for work. None of us here are ASP experts as we use PHP. But I am on the bottom of PHP experience as well, mostly working with HTML/CSS alone. My current forms credentials look like:

rotected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsValid Then Exit Sub

        Dim SendResultsTo As String = "email to"
        Dim smtpMailServer As String = "email server"
        Dim smtpUsername As String = "email username"
        Dim smtpPassword As String = "password"
        Dim MailSubject As String = "Form Results"

How would I go about making this form send to a Gmail address? I know I must include the port (587) somewhere, but cannot figure out where, as this form doesn't match the syntax of any other forms I have seen. Any help would be greatly appreciated!

Aj Troxell
  • 265
  • 1
  • 4
  • 16

7 Answers7

16

You can add this in your web.config file

 <system.net>
    <mailSettings>
      <smtp from="yourMailId@gmail.com ">
        <network host="smtp.gmail.com" defaultCredentials="false"
      port="587" userName ="yourmail@gmail.com" password="yourpassword" />
      </smtp>
    </mailSettings>
   </system.net>
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • Would I remove the entire portion from my existing code? `Dim SendResultsTo As String = "email to" Dim smtpMailServer As String = "email server" Dim smtpUsername As String = "email username" Dim smtpPassword As String = "password" Dim MailSubject As String = "Form Results"` – Aj Troxell Nov 02 '11 at 15:29
  • you will not need to specify the username password and network again in the code, in your code create a SmtpClient object to send the message – huMpty duMpty Nov 02 '11 at 15:34
15
protected void SendMail()
        {
            MailMessage msg = new MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            try
            {
                msg.Subject = "Add Subject";
                msg.Body = "Add Email Body Part";
                msg.From = new MailAddress("Valid Email Address");
                msg.To.Add("Valid Email Address");
                msg.IsBodyHtml = true;
                client.Host = "smtp.gmail.com";
                System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
                client.Port = int.Parse("587");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = basicauthenticationinfo;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(msg);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
Damith
  • 62,401
  • 13
  • 102
  • 153
291086
  • 166
  • 2
  • Gmail/Google will block it from sending the email. You can change the settings from sender's gmail account to allow "less secure apps" to access it. – Shiljo Paulson Jul 19 '17 at 09:56
1

Google now blocks sign-ins from an app (even over SSL) to send an email. You have two options:

  1. Enable less-secure apps for the account you are using; OR
  2. Generate an app-password from Google (recommended)

For option 2, you will have to enable 2-factor authentication before you can generate an app pasword. Here are the steps I took to get this working on my ASP.NET web app

How to configure an ASP.NET web app with Gmail SMTP

Nicola
  • 851
  • 7
  • 11
1

Create a System.Net.Mail.SmtpClient object, set the SMTP server info you are using.

Then create a System.Smtl.MailMessage with the message data and send it:

using (System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient()) {
    using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from*where.com", "to@where.com") {
        IsBodyHtml = true,
        Subject = "Subject text",
        Body = messageBody,
    }) {
        mail.Send(message);
} // using

You can configure your SmtpClient in the constructor, we use web.comfig, so I don't have that code.

drdwilcox
  • 3,833
  • 17
  • 19
  • I have no idea how to go about doing that, hahaha. I shouldn't even be working on this project, but unfortunately I am the only one available to do it. – Aj Troxell Nov 02 '11 at 15:28
  • The web.config in the above answer sets up the SmtpClient object, so you can send a message using my code unchanged. – drdwilcox Nov 02 '11 at 15:33
  • Here is my full syntax http://pastebin.com/85Ad9swq I apologize, but I don't even know where to start. I have no viable reason to learn ASP.NET...aside from this one instance -_- – Aj Troxell Nov 02 '11 at 15:46
0

If you're using 2-step verification, don't forget to generate an application specific password and use that, not the password you use to log on to Gmail.

(Sorry I can't add this as a comment. Not enough rep at the time of posting this.)

Zack Martin
  • 373
  • 4
  • 8
0

Try this.

Dim client As New Net.Mail.SmtpClient
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential("sender@gmail.com", "password")
client.Host = "smtp.gmail.com"
client.Port = 587
client.EnableSsl = True
client.Send("sender@gmail.com","reciever@gmail.com","subject","body")
naveen
  • 53,448
  • 46
  • 161
  • 251
  • I get a runtime error. If Not Page.IsValid Then Exit Sub Dim client As New Net.Mail.SmtpClient client.UseDefaultCredentials = False client.Credentials = Net System.Net.NetworkCredential("ajtroxell67@gmail.com", "pajax6767") client.Host = "smtp.gmail.com" client.Port = 587 client.EnableSsl = True client.Send("sender@gmail.com","ajtroxell67@gmail.com","subject","body") Try Dim txtQ As TextBox = Me.FormContent.FindControl("TextBoxQ")` – Aj Troxell Nov 02 '11 at 15:51
  • 1
    please delete this. your password is revealed! – naveen Nov 02 '11 at 16:48
-1

There's no shortage of tutorials on how to send an email from within .NET.

Essentially you want a System.Net.Mail.SmtpClient object to interact with the SMTP server, a System.Net.Mail.MailMessage object to hold the message data, and configuration data in your config file to direct the client on how/where to send the message.

David
  • 208,112
  • 36
  • 198
  • 279