This entry will describe how to send email using Asp.Net and C#. There will be a couple examples maybe jumbled together, but it will cover these topics:
1. Sending Text Email
2. Sending HTML Email
3. Sending Attatchments
First off, specify/import the library you need...
Next create the mail object....
Then set the mail object fields... (Your usual... To, From, Subject, Body)
For HTML email setting a single field will do...
If you want to add attatchments, do the following... the first argument is a stream and the second is a string for the filename.
Now after all that, you have only created your email message. You still need to send it out. So...
That's it. That's all there is to sending Emails!
1. Sending Text Email
2. Sending HTML Email
3. Sending Attatchments
First off, specify/import the library you need...
Using System.Net.Mail; (CodeBehind)
or <@ Import Namespace="System.Net.Mail" > (Inline)
Next create the mail object....
MailMessage mail = new MailMessage();
Then set the mail object fields... (Your usual... To, From, Subject, Body)
mail.From = new MailAddress("someone@gmail.com");
mail.To.Add("someone@gmail.com");
mail.Bcc.Add("someone@gmail.com");
mail.Subject = "The Subject Line";
mail.Body = "The Body String";
For HTML email setting a single field will do...
mail.IsBodyHtml = true;
If you want to add attatchments, do the following... the first argument is a stream and the second is a string for the filename.
mail.Attatchments.Add(new Attatchment(FileUpload.PostedFile.InputStream, "filename");
Now after all that, you have only created your email message. You still need to send it out. So...
SmtpClient client = new SmtpClient();
client.Send(mail);
That's it. That's all there is to sending Emails!
Comments