Before you send an email, using ASP.Net, you need to understand the basics of how it works.
The first thing you do is make sure you import the System.Net.Mail Namespace.
In C#: using System.Net.Mail;
In VB: Imports System.Net.Mail
There are two basic parts which you will define – the mailmessage and the smtpclient. The mailMessage contains all the parts you need for the message (to, from,cc,bcc, subject, body – all properties of the mailmessage object), but first you need to ‘set up’ all this stuff.
VB: Dim client as New SmtpClient("localhost") Dim msg as New MailMessage
C#: SmtpClient client = new SmtpClient("localhost"); MailMessage msg = new MailMessage();
(‘localhost’ is used here, but you would supply whatever is necessary in your specific case, for your SMTP Server.)
Then, you just add in all the pieces you need (to/from/cc/bcc/subject/body). The syntax formations are as follows:
VB: msg.From = new MailAddress("You@YourSite.Com") msg.To.Add(new MailAddress("ToAddress@OverThere.com")) ' CC and BCC properties are formed in the same manner msg.Subject = "Your Subject" msg.Body = "Whatever message you need to send" ' you can also use: msg.IsBodyHtml = true/false ' if you are including HTML markup in your body
C#: msg.From = new MailAddress("You@YourSite.Com"); msg.To.Add(new MailAddress("ToAddress@OverThere.com")); // CC and BCC properties are formed in the same manner msg.Subject = "Your Subject"; msg.Body = "Whatever message you need to send"; msg.IsBodyHtml = (true / false); //if you include HTML Markup in your body
Once you have your mailmessage pieces all put together, you use the client to send it:
client.Send(msg)
It would be very easy to create your own subroutine to do this, but you’d still need to put all the properties you’d need together. Even the most extensive overload (constructor-wise) of the MailMessage only includes the To, the From, the Subject and the Body:
Dim msg as New MailMessage("To@To.com", "From@From.Com","MySubject", "MyBody")
So one way or the other, if you need any more of the MailMessage Properties (CC/BCC/IsBodyHTML), you’re going to need to add them into the code mix.
But now you see the basic mechanics of how ASP.Net email works. You can use any basic controls to gather the information or hard code them as needed, depending on your own personal needs.
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF
Leave a reply
You must be logged in to post a comment.