日期:2014-05-18 浏览次数:21047 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="mail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>mail to users</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.Net.Mail; public partial class mail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //SendMail(发件者, 收件者, 主旨, 内容, 主机,发件者昵称, 密码 ,附件) SendMail("sunjie@yyhj.com.cn", "lilei.luo@yyhj.com.cn", "主旨", "邮件内容测试", "exhj.yyhj.com.cn", "孙节", "yyhj", ""); } public void SendMail(string send, string recieve, string subject, string mailbody, string host, string uname, string pwd, string strFileName) { //生成一个 使用SMTP发送邮件的客户端对象 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); //生成一个主机IP //client.Port = 25; //587, 465, 995 client.Host = host; //表示不以当前登录用户的默认凭据进行身份验证 client.UseDefaultCredentials =true ; //包含用户名和密码 if (uname != "") { client.Credentials = new System.Net.NetworkCredential(uname, pwd); } //指定如何发送电子邮件。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; //通过本机SMTP服务器传送该邮件, //其实使用该项的话就可以随意设定“主机,发件者昵称, 密码”,因为你的IIS服务器已经设定好了。而且公司内部发邮件是不需要验证的。 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, uname, System.Text.Encoding.UTF8); message.Subject = subject; message.Body = mailbody; //定义邮件正文,主题的编码方式 message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8"); message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //获取或设置一个值,该值指示电子邮件正文是否为 HTML。 message.IsBodyHtml = false; //指定邮件优先级 message.Priority = System.Net.Mail.MailPriority.High; //添加附件 //System.Net.Mail.Attachment data = new Attachment(@"E:\9527\tubu\PA260445.JPG", System.Net.Mime.MediaTypeNames.Application.Octet); if (strFileName != "" && strFileName != null) { Attachment data = new Attachment(strFileName); message.Attachments.Add(data); } try