Webpages Email in ASP
Sure! Here’s how you can send emails using ASP.NET Web Pages (WebMatrix) with the built-in Email helper:
Sending Email in ASP.NET Web Pages
1. Basic Email Sending
Use the Email helper to send a simple email:
@{ Email.Send( to: "recipient@example.com", subject: "Test Email from Web Pages", body: "Hello! This is a test email sent from ASP.NET Web Pages." );}<p>Email sent!</p>2. Specifying From Address and SMTP Settings
To customize the sender and SMTP, set up your web.config:
<system.net> <mailSettings> <smtp from="your-email@example.com"> <network host="smtp.example.com" port="587" userName="your-username" password="your-password" enableSsl="true" /> </smtp> </mailSettings></system.net>Replace with your SMTP server details.
fromis the default sender address.
3. Sending HTML Email
Use the optional isBodyHtml parameter:
@{ Email.Send( to: "recipient@example.com", subject: "HTML Email", body: "<h1>Hello</h1><p>This is an <strong>HTML</strong> email.</p>", isBodyHtml: true );}4. Sending Email with CC and BCC
@{ Email.Send( to: "recipient@example.com", subject: "Email with CC and BCC", body: "Check CC and BCC recipients.", cc: "ccperson@example.com", bcc: "bccperson@example.com" );}5. Handling Errors
Wrap your Email.Send call in try-catch to handle exceptions:
@{ try { Email.Send("recipient@example.com", "Subject", "Body"); <p>Email sent successfully!</p>; } catch(Exception ex) { <p>Error sending email: @ex.Message</p>; }}Summary
| Feature | Usage Example |
|---|---|
| Send basic | Email.Send(to, subject, body); |
| HTML email | Email.Send(..., isBodyHtml: true); |
| CC, BCC | Email.Send(..., cc: "...", bcc: "..."); |
| SMTP settings | Configure in web.config under <system.net> |
If you want me to help you with email templates, attachments, or more advanced mailing scenarios, just ask!