C USES the MailAddress class to send the instance code for html formatted mail
- 2020-05-26 09:59:34
- OfStack
1. First, the namespace using System.Net.Mail is introduced;
2. The function of the sent mail is encapsulated into one class, which contains the basic functions of the sent mail: recipient (more than one person), cc (more than one person), sender, subject, message body, attachment, etc. The encapsulated Email class is as follows:
public class Email
{
/// <summary>
/// The sender
/// </summary>
public string mailFrom { get; set; }
/// <summary>
/// The recipient
/// </summary>
public string[] mailToArray { get; set; }
/// <summary>
/// cc
/// </summary>
public string[] mailCcArray { get; set; }
/// <summary>
/// The title
/// </summary>
public string mailSubject { get; set; }
/// <summary>
/// The body of the
/// </summary>
public string mailBody { get; set; }
/// <summary>
/// Sender password
/// </summary>
public string mailPwd { get; set; }
/// <summary>
/// SMTP Mail server
/// </summary>
public string host { get; set; }
/// <summary>
/// Whether the text is html format
/// </summary>
public bool isbodyHtml { get; set; }
/// <summary>
/// The attachment
/// </summary>
public string[] attachmentsPath { get; set; }
public bool Send()
{
// Class with the specified mail address MailAddress The instance
MailAddress maddr = new MailAddress(mailFrom);
// Initialize the MailMessage The instance
MailMessage myMail = new MailMessage();
// Add a mail address to the collection of recipient addresses
if (mailToArray != null)
{
for (int i = 0; i < mailToArray.Length; i++)
{
myMail.To.Add(mailToArray[i].ToString());
}
}
// Add a mail address to the cc recipient address collection
if (mailCcArray != null)
{
for (int i = 0; i < mailCcArray.Length; i++)
{
myMail.CC.Add(mailCcArray[i].ToString());
}
}
// Sender address
myMail.From = maddr;
// The subject line of the email
myMail.Subject = mailSubject;
// The encoding used for the subject content of the email
myMail.SubjectEncoding = Encoding.UTF8;
// Email body
myMail.Body = mailBody;
// The encoding of the email body
myMail.BodyEncoding = Encoding.Default;
myMail.Priority = MailPriority.High;
myMail.IsBodyHtml = isbodyHtml;
// Add attachments where there are attachments
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in attachmentsPath)
{
attachFile = new Attachment(path);
myMail.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
throw new Exception(" There was an error when adding an attachment :" + err);
}
SmtpClient smtp = new SmtpClient();
// Specify the sender's email address and password to verify the sender's identity
smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
// Set up the SMTP Mail server
smtp.Host = host;
try
{
// Send the email to SMTP Mail server
smtp.Send(myMail);
return true;
}
catch (System.Net.Mail.SmtpException ex)
{
return false;
}
}
}
3. The page calls the class that sent the email
protected void Send_Click(object sender, EventArgs e)
{
Email email = new Email();
email.mailFrom = " The email address of the sender ";
email.mailPwd = " The password of the sender's mailbox ";
email.mailSubject = " Email subject ";
email.mailBody = " Email content ";
email.isbodyHtml = true; // Whether it is HTML
email.host = "smtp.126.com";// If it is QQ Email is: smtp:qq.com, So on
email.mailToArray = new string[] { "******@qq.com","12345678@qq.com"};// Recipient mail collection
email.mailCcArray = new string[] { "******@qq.com" };// Cc message collection
if (email.Send())
{
Response.Write("<script type='text/javascript'>alert(' Send successfully! ');history.go(-1)</script>");// The prompt will return to the current page if it is sent successfully.
}
else
{
Response.Write("<script type='text/javascript'>alert(' Send failed! ');history.go(-1)</script>");
}
}