Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gmail SMTP in ASP.NET form

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!

like image 801
Aj Troxell Avatar asked Nov 02 '11 15:11

Aj Troxell


2 Answers

You can add this in your web.config file

 <system.net>
    <mailSettings>
      <smtp from="[email protected] ">
        <network host="smtp.gmail.com" defaultCredentials="false"
      port="587" userName ="[email protected]" password="yourpassword" />
      </smtp>
    </mailSettings>
   </system.net>
like image 78
huMpty duMpty Avatar answered Sep 30 '22 17:09

huMpty duMpty


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);
            }
        }
like image 40
291086 Avatar answered Sep 30 '22 16:09

291086