TT419.CN代码备份:发送邮件(带图片)的部分核心代码
string fromAddress =txt_email.Text;
string toAddress = txt_to.Text.Replace(";",",");
string title = "Your friend " + txt_firstname.Text + " " + txt_lastname.Text + "(" + txt_email.Text + ") sends you a e-card ";
string path = Server.MapPath(@"images/ECARD_land.gif"); // my logo is placed in images folder
MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
//mailMessage.To.Add(toAddress);
mailMessage.Subject = title;
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "companylogo";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body style=' color:red; text-align:center; font-size:12px; font-family:Verdana'><br><b>" + title + "</b><br><a href=' calendar2009_land.jpg'><img src='cid:companylogo' border=0/></a><br></body></html>", null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
try
{
mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient client = client = new SmtpClient();
client.Host = System.Web.Configuration.WebConfigurationManager.AppSettings["smtpsvr"].ToString();
client.Credentials = new System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings["username"].ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings["pwd"].ToString());
client.Send(mailMessage);
Page.RegisterClientScriptBlock("on", "<script>alert('发送成功')</script>");
}
catch
{
Page.RegisterClientScriptBlock("sr", "<script>alert('发送失败,请确保收件人地址正确')</script>");
}