简体   繁体   中英

ASP.NET Send Email

I am trying to send a email when user clicks submit in a contact us page but for some reason its not working, what am I doing wrong? (PS email & password was omited from this code snippet but included in actual solution.

Thanks

Code in web.config:

<system.net>
<mailSettings>
  <smtp from="order@test.com">
    <network host="smtp.gmail.com"
             userName="" //my email
             password="" //password deleted for privacy reasons
             defaultCredentials="false"
             port="456"
             enableSsl="true" />
  </smtp>
</mailSettings>

code in asp.net contact form:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(new MailAddress("test@gmail.com"));
        mail.Subject = "Test";
        mail.Body = "Message was sent from" + txtName.text + txtComment.text;
        SmtpClient smtp = new SmtpClient();
        smtp.SendAsync(mail, null);
    }

Gmails uses port 465 , not 456 for SSL SMTP. From here :

Outgoing Mail (SMTP) Server - requires TLS1 or SSL: smtp.gmail.com

Use Authentication: Yes

Port for TLS/STARTTLS: 587

Port for SSL: 465

The "other" possible reason:

  • Does your code "work" when developing locally, but stops working when you "publish"/ftp/copy, etc. your files to your web host?

  • If Yes : check your host's trust settings for ASP.Net. If it's medium trust (which is likely in shared hosting), then note that you cannot use any port other than port 25 for SMTP in medium trust .

It works locally (dev) because in local dev/VS environment, ASP.Net runs in full trust .

REF (MSDN Blogs): SMTP Problems when ASP.Net is not running in full-trust

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM