All the Contact Us forms needs an e-mail system
In this article you will learn how to send emails from you .aspx site, using plain format, html format also add attachments by two ways, the quick (easy way) and the profesional ( i like this way )
The first step is be sure that the SMTP Server is locally installed at SMTP Service of the IIS
This is an easy and quick way to do that, we will call this form ContactMe.aspx
First, we have to import the System.Web.Mail
Then in the code behind add the namespace
Now build your message
string To = "firstname.lastname@example.org";
string From = "email@example.com";
string Subject = "The Famous";
string Body = "Hello World";
What is the SmtpMail?
this class has a method called Send that allows the user to send an email, in this way we set four parameters:
SmtpMail.Send("firstname.lastname@example.org","email@example.com","The Famous","Hello World");
As you can see this is the easy and quick way to do that. Now you will lear how to do the professional way (interesting). Here you will send an email including an attachment and CC, we can use the same parameters above
Professional Way (I like this Way)
We have to create an object (MailMessage) called myMail
MailMessage myMail = new MailMessage();
myMail.To = To;
myMail.Cc = "firstname.lastname@example.org";
myMail.From = From;
myMail.Subject = Subject;
myMail.BodyFormat = MailFormat.Html;
string body = "<html><body> Bolded message <br> <p>The Message</p> </body></html>";
myMail.Body = body;
Now go and check your inbox and see what happend.
So what I did in this code?
As you can see I used an enum from MailMessage class (MailFormat) this enum has 2 options: Text and HTML, I set the HTML and build the Body Message.
You will not receive a value about the sucess of the email message. The reason is that the email is writted into the Inetput folder, also if the mail fails you can go and check the Badmail folder into inetpub, this way if you set an invalid To or From email, the message will be set in this folder.