码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-26 16:19:32      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:http   ar   os   sp   文件   on   bs   html   ad   

该方式直接保存为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();
 

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

标签:http   ar   os   sp   文件   on   bs   html   ad   

原文地址:http://www.cnblogs.com/gc2013/p/4123145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!