How to use gmail SMTP in ASP.NET form

I am new to ASP and I was looking for a form for work. None of us here are ASP experts, since we use PHP. But I am also at the core of the PHP experience, mostly working only with HTML / CSS. My current form credentials are as follows:

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 can I submit this form to my Gmail address? I know that I have to specify port (587) somewhere, but I cannot figure out where, since this form does not match the syntax of any other forms I have seen. Any help would be greatly appreciated!

+5
source share
7 answers
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);
            }
        }
+12
source

web.config

 <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>
+16

System.Net.Mail.SmtpClient, SMTP, .

System.Smtl.MailMessage :

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

SmtpClient , web.comfig, .

+1

.

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")
0

, , , Gmail.

(, . .)

0

Google ( SSL) . :

  • ;
  • - Google ()

2 , . , , - ASP.NET.

- ASP.NET Gmail SMTP

0

, .NET.

Essentially, you want the object to System.Net.Mail.SmtpClientinteract with the SMTP server, the object System.Net.Mail.MailMessagefor storing message data and configuration data in your configuration file, in order to direct the client to the way the message is sent / sent.

-1
source

All Articles