用system.web.mail 发送邮件及附件

try
            {
                MailMessage message = new MailMessage();
                // 发送方
                message.From = "[email protected]";
                // 接收方
                message.To = "[email protected]";
  //主题
                message.Subject = "Send Using Web Mail";


                // 指定为HTML格式的内容
                message.BodyFormat = MailFormat.Html;


                // HTML内容  邮件内容
                message.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>";


                //添加附件
                // 指定附件路径.
                String sFile = @"D:\down\dd.txt";
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);


                message.Attachments.Add(oAttch);


                // 指定SMTP服务器地址
                SmtpMail.SmtpServer = "smtp.163.com";
                //验证 
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //登陆名 
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");
                //登陆密码 
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "********"); 
                SmtpMail.Send(message);


                message = null;
                oAttch = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                Console.Read();
            }

你可能感兴趣的:(用system.web.mail 发送邮件及附件)