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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 19:29   1544   0
try
            {
                MailMessage message = new MailMessage();
                // 发送方
                message.From = "xxx@163.com";
                // 接收方
                message.To = "xxx@163.com";
  //主题
                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", "lwbpeter@163.com");
                //登陆密码 
                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();
            }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP