111,131
社区成员
发帖
与我相关
我的任务
分享
public void SendMail(string fromAddr, string toAddr, string bcc, string cc, string subject, string body)
{
if ((fromAddr == null) || (fromAddr == ""))
{
fromAddr = "sender@contoso.com";
}
if ((toAddr == null) || (toAddr == ""))
{
throw new Exception("To address should not be empty.");
}
//replace ';' with ','
string toAddress = toAddr.Replace(";", ",");
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage(fromAddr, toAddress);
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}
// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient(this.Host, this.Port);
mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//Credential
System.Net.NetworkCredential oCredential = new System.Net.NetworkCredential(Username, Password);
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.Credentials = oCredential;
// Send the mail message
mSmtpClient.Send(mMailMessage);
}