下面黄色代码为实现发送邮件
FileStream fs = new FileStream(Server.MapPath("../ImportUserIOExcel/productConsultAnwser.html"), FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
string strTemp = sr.ReadToEnd();
sr.Close();
fs.Close();
//if (queryTemp.spec_name1 == "" && queryTemp.spec_name2 == "")
//{
// product_name = queryTemp.brand_name + queryTemp.product_name;
//}
//else
//{
// product_name = queryTemp.brand_name + queryTemp.product_name + "(" + queryTemp.spec_name1 + " " + queryTemp.spec_name2 + ")";
//}
product_name = queryTemp.product_name;
strTemp = strTemp.Replace("{{$username$}}", userName);
strTemp = strTemp.Replace("{{$productName$}}", product_name);
strTemp = strTemp.Replace("{{$consultInfo$}}", consultInfo);
strTemp = strTemp.Replace("{{$consultAnwser$}}", answerInfo);
strTemp = strTemp.Replace("{{$productUrl$}}", productUrl);
sendmail(EmailFrom, FromName, userEmail, userName, EmailTile, strTemp, "", SmtpHost, Convert.ToInt32(SmtpPort), EmailUserName, EmailPassWord);
public bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, int sSMTPPort, string sSMTPuser, string sSMTPpass)
{
////设置from和to地址
MailAddress from = new MailAddress(sfrom, sfromer);
MailAddress to = new MailAddress(sto, stoer);
////创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to);
//// 添加附件
if (sfile != "")
{
oMail.Attachments.Add(new Attachment(sfile));
}
////邮件标题
oMail.Subject = sSubject;
////邮件内容
//oMail.Body = sBody;
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(sBody, null, "text/html");
oMail.AlternateViews.Add(htmlBody);
////邮件格式
oMail.IsBodyHtml = true;
////邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
////设置邮件的优先级为高
oMail.Priority = MailPriority.Normal;
////发送邮件
SmtpClient client = new SmtpClient();
////client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
client.Port = sSMTPPort;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception ex)
{
}
finally
{
////释放资源
oMail.Dispose();
}
}