当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用smtp发送带附件的邮件实现方法

C#使用smtp发送带附件的邮件实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#使用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();

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网