首页 > 学院 > 开发设计 > 正文

smtp发送带附件的邮件(直接将string类型结果保存为附件)

2019-11-17 02:43:55
字体:
来源:转载
供稿:网友
smtp发送带附件的邮件(直接将string类型结果保存为附件)

该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好 MailMessage mmsg = new MailMessage(); mmsg.Subject = "邮件标题"; mmsg.Body = "邮件内容"; mmsg.To.Add("accept@QQ.com");//接收邮箱 byte[] bytes = System.Text.Encoding.Default.GetBytes (@"<table><tr><td width=150>1234567891234567 </td><td width=80>12345678</td></tr></table>"); MemoryStream ms = new MemoryStream(bytes); ContentType ct = new ContentType(); //附件文件类型 ct.MediaType = MediaTypeNames.Text.Html; //附件名称,可以是其它后缀名 ct.Name = "附件名称" + DateTime.Now.ToString() + ".html"; mmsg.Attachments.Add(new Attachment(ms, ct)); //SMTP简单邮件协议 System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(); sc.Host = "127.0.0.1";//主机地址 sc.Port = 25;//端口 //发送邮箱账号和密码 sc.Credentials = new System.Net.NetworkCredential("account", "passWord"); //发送邮箱 mmsg.From = new MailAddress("account@qq.com"); sc.Send(mmsg); //释放流资源 ms.Close(); ms.Dispose();


上一篇:Unity 入門

下一篇:小知识储备

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表