Creating Your Own Online E-mail System in ASP.NET 2.0 - Dissecting the Key Techniques: Sending Email
(Page 3 of 4 )
2. Sending emails
The whole process of sending email is rather complicated especially when the email contains attachments. Let's examine the detailed steps in terms of the three scenarios mentioned above, as follows:
1) Obtain the mail address of the sender, i.e. get the address the user has configured in the profile. The following gives the crucial code snippet associated with this:
string from =((WebMailProfile)HttpContext.Current.Application["WebMailProfile"]).Email;
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(from);
2) Add the addresses of the receivers. Since there may be more than one address receiving the e-mail, you need to get the address of every recipient from the string containing these addresses, and append them one by one to the To property of the MailMessage-mailMsg object:
string split = ";";
string[] toList = To.Text.Trim().Split(split.ToCharArray());
for(int i = 0; i < toList.Length; i++)
{
mailMsg.To.Add(toList[i].Trim());
}
3) Add the CopyTo (i.e. CC) addresses. There may be more than one of this kind of address; your approach should be similar to the one taken above:
string[] ccList = CC.Text.Trim().Split(split.ToCharArray());
for(int i = 0; i < ccList.Length; i++){
if(ccList[i].Trim().Length > 0) {
mailMsg.CC.Add(ccList[i].Trim());
}
}
4) Add the topic of the mail and give the appropriate coding algorithm:
mailMsg.Subject = Title.Text.Trim();
mailMsg.SubjectEncoding = Encoding.UTF8;
5) Append the body of the e-mail and also specify the coding methods:
mailMsg.Body = Body.Text;
mailMsg.BodyEncoding = Encoding.UTF8;
6) Specify the format of the email as HTML by default:
mailMsg.IsBodyHtml = HtmlCB.Checked;
7) Add the attachments. Here the reason and the operation are just like those of steps 2 or 3 above, so we simply list the relevant code below:
HttpFileCollection fileList = HttpContext.Current.Request.Files;
for(int i = 0; i < fileList.Count; i++)
{ ///adding each attachment
HttpPostedFile file = fileList[i];
if(file.FileName.Length <= 0 || file.ContentLength <= 0)
{break;}
Attachment attachment = new Attachment(file.FileName);
mailMsg.Attachments.Add(attachment);
nContain += file.ContentLength;
}
8) Start sending your e-mail. First, call the SendMail method of the Mail class (the wrapper of the mailing related operation for simplification). Second, use the SaveAsMail method of the Mail class to persist the e-mails sent into the Sender mailbox:
IMail mail = new Mail();
mail.SendMail(mailMsg);
///save the mails to send
int nMailID = mail.SaveAsMail(mailMsg.Subject,mailMsg.Body,from, To.Text.Trim(),CC.Text.Trim(),mailMsg.IsBodyHtml,
nContain,mailMsg.Attachments.Count > 0 ? true : false);
9) If there are attachments for the e-mails, upload them to the hard disk of the server, and finally store information for these attachments in the database. This function is accomplished through the following:
for(int i = 0; i < fileList.Count; i++)
{ ///add each attachment
HttpPostedFile file = fileList[i];
if(file.FileName.Length <= 0 || file.ContentLength <= 0)
{break;}
/// store these attachments into the hard disk
file.SaveAs(MapPath("MailAttachments/" + Path.GetFileName(file.FileName)));
/// save the attachments
mail.SaveAsMailAttachment(
Path.GetFileName(file.FileName),
"MailAttachments/" + Path.GetFileName(file.FileName),
file.ContentType,
file.ContentLength,
nMailID);
}
Next: Using SendMail >>
More ASP.NET Articles
More By Xianzhong Zhu