zoukankan      html  css  js  c++  java
  • 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();
     

  • 相关阅读:
    安卓-登陆页面的实现
    异常
    实用类
    Hashset
    Map
    LinkedList
    arraylist
    继承
    字符串相关代码
    数组代码
  • 原文地址:https://www.cnblogs.com/gc2013/p/4123145.html
Copyright © 2011-2022 走看看