asp.net 发送邮件

  

   string file = Server.MapPath("email_test.txt");

            System.Web.Mail.MailMessage mes = new System.Web.Mail.MailMessage();

            mes.To = "发送邮箱";
            mes.From = "接收邮箱";

          

            MailAttachment mailAttachment = new MailAttachment(file);

            mes.Attachments.Add(mailAttachment);
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//基本权限 
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "发送邮箱名");
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "发送邮箱密码");
            mes.BodyEncoding = System.Text.Encoding.UTF8;

            mes.Body = "邮件 正文 email content";

            //以下可以选择文本呈现格式

            mes.BodyFormat = MailFormat.Html;

            mes.Subject = "邮件发送测试";

            SmtpMail.SmtpServer = "smtp.163.com";
            SmtpMail.Send(mes);

            Response.Write("发送成功!");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

//以下是利服务器pop3协议发送邮件

 try
        {

            string file = Server.MapPath("email_test.txt");

            MailAddress from = new MailAddress("zhengpengfei0791@163.com");//实例一个邮件发送地址
            MailAddress to = new MailAddress("solomon_msn@live.cn");//实例一个邮件接收地址

            MailMessage mes = new MailMessage(from, to);


            Attachment myAttachment = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);

            System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;

            disposition.CreationDate = System.IO.File.GetCreationTime(file);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

            mes.Attachments.Add(myAttachment);
            mes.IsBodyHtml = true;

            mes.Subject = "邮件测试";
            mes.Subject = "邮件正文 Email Content<img src='";

            SmtpClient client = new SmtpClient();
            client.Host = Request.UserHostAddress;
            client.Port = 25;

            client.Credentials = new System.Net.NetworkCredential("toEmail", "test");

            client.Send(mes);

            Response.Write("邮件发送成功!");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

posted @ 2011-05-04 21:56  solomon_Blog  阅读(229)  评论(0编辑  收藏  举报