邮件发送文档附件代码

                            var  mmsg = new System.Net.Mail.MailMessage();
                            mmsg.Subject = model.Title;//标题
                            mmsg.Body = "请查看附件,有疑问请联系管理员";//内容
                            mmsg.To.Add(account);//接收邮箱

                            var file = new FileStream(attachPath, FileMode.Open);
                            var ct = new ContentType();
                            //附件文件类型
                            ct.MediaType = MediaTypeNames.Text.Html;
                            //附件名称,可以是其它后缀名
                            ct.Name = model.Content;
                            mmsg.Attachments.Add(new System.Net.Mail.Attachment(file, ct));
                            //SMTP简单邮件协议
                            var sc = new System.Net.Mail.SmtpClient();
                            sc.Host = config["SmtpServer"];//主机地址
                            sc.Port = Convert.ToInt32(config["SmtpServerPort"]);//端口
                            //发送邮箱账号和密码
                            sc.Credentials = new System.Net.NetworkCredential(user.Email, user.EmailPwd);
                            //发送邮箱
                            mmsg.From = new MailAddress(user.Email);
                            sc.Send(mmsg);
                            //释放流资源
                            file.Close();
                            file.Dispose();

 

posted @ 2015-12-23 16:14  caichao  阅读(246)  评论(0编辑  收藏  举报