The basic contact form you would create would only have a text box for comments. However, you could also have a dropdownlist for the user to choose a subject, etc. The last thing you would need is a button, which, in it’s click event will have the code necessary to send the email.
The first thing you want to do is to import the mail namespace in .Net:
C#: using System.Net.Mail
VB: Imports System.Net.Mail
Here’s the basic code for the button click event handler, which is necessary for sending the email.
VB: Dim Msg as MailMessage=New MailMessage() Dim MailObj as new SmtpClient("mail.YourDomain.com") Msg.To.Add(new MailAddress(yourEmailAddress)) Msg.From = new MailAddress(yourEmailAddress) Msg.Subject = "Subject of Email" Msg.Body = YourTextBox.Text Msg.IsBodyHTML = false MailObj.Send(Msg)
C#: MailMessage Msg = new MailMessage(); SmtpClient MailObj = new SmtpClient("mail.YourDomain.com"); Msg.To.Add(new MailAddress(yourEmailAddress)); Msg.From = new MailAddress(yourEmailAddress); Msg.Subject = "Subject of Email"; Msg.Body = YourTextBox.Text; Msg.IsBodyHTML = false; MailObj.Send(Msg);
For the subject line, if you have a DropDownList for a subject line, you’d substitute the selected item as the subject (Msg.Subject)
If you needed a CC or BCC, you’d follow the same general form of the ‘To’ property:
It’s the same for VB.Net, without the semi-colons.
Since this is a contact form for users to send emails to you, most likely, you’d use your own email address for both the FROM and TO email addresses.
But that’s all there is – – put this in your button’s click event handler and you’re on your way to your first Contact form!
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF