MailMessage mm = new MailMessage();
1 MailMessage mm = new MailMessage(); 2 mm.Subject = txt_title.Text; 3 mm.Body = HiddenField1.Value; 4 mm.BodyEncoding = mm.SubjectEncoding = System.Text.Encoding.UTF8; 5 mm.From = new MailAddress("xxxxxxxx@qq.com");//发件人 6 mm.IsBodyHtml = true; 7 mm.ReplyToList.Add(new MailAddress("xxxxxxx@qq.com"));//回复给发件人的地址 8 mm.To.Add(new MailAddress("xxxxxxxx@qq.com"));//收件人 9 Attachment att = new Attachment(Server.MapPath("upload/" + filename)); 10 mm.Attachments.Add(att);//添加附件 11 SmtpClient client = new SmtpClient(); 12 client.Host = "smtp.qq.com";//服务器名称 13 client.DeliveryMethod = SmtpDeliveryMethod.Network; 14 client.Credentials = new System.Net.NetworkCredential("xxxxxxx3@qq.com","xxxxxxx");//对发件人进行验证(账号和密码) 15 try 16 { 17 client.Send(mm); 18 JavaScriptHelper.Alert("发送成功", this.Page); 19 client.Dispose(); 20 } 21 catch(Exception ex) { 22 JavaScriptHelper.Alert("发送失败",this.Page); 23 throw ex; 24 }
MM的属性
Subject //邮件主题
SubjectEncoding//邮件标题编码格式
例如MM.SubjectEncoding=System.Text.Encoding.Utf8;
Body//邮件内容
BodyEncoding//邮件内容正文编码
例如同上
IsBodyHtml//正文是否可以使用HTML格式
From //发件人邮箱地址
例如MM.From=new MailAddress("发件人邮箱地址");
To//可以是一个收件人地址也可是多个收件人地址
例如群发多个收件人:MM.To.Add(new MailAddress("收件人地址"));
ReplyTo//回复地址与发件人地址是一致的
例如MM.ReplyTo=new MailAddress("发贱人邮箱地址");
Attachments//邮件附件
在这不得不说,如何添加邮件附件
Attachment att = new Attachment(Server.MapPath("upload/" + filename));//创建附件对象 mm.Attachments.Add(att);//添加附件
而对简单邮件传送协议对象属性暂解
创建简单邮件传送协议对象:
SmtpClient client = new SmtpCli
电子邮件通过网络发送
client.DeliveryMethod = SmtpDeliveryMethod.Network;
设置通信服务器,此处发件人所使用的SMTP服务器 smtp.163.com等
client.Host = "smtp.163.com";//smtp.126.com smtp.QQ.com,
用于验证发件人身份凭证
client.Credentials = new System.Net.NetworkCredential("发件人邮箱地址", "发件人邮箱密码");
发送邮件
client.Send(mm);
更多可参考:http://www.cnblogs.com/dlwang2002/archive/2007/10/18/928875.html#top