在.NET中使用SMTP发送邮件
摘要
- 简介
- .NET的SMTP类
- 使用CDO组件发送邮件
- 使用Socket撰写邮件发送程序
- 总结
- 更多的信息
using System.Web.Mail; public void sendMail() { try{System.Web.Mail.MailMessage myMail=new MailMessage();myMail.From = "myaccount@test.com";myMail.To = "myaccount@test.com";myMail.Subject = "MailTest";myMail.Priority = MailPriority.Low;myMail.BodyFormat = MailFormat.Text;myMail.Body = "Test";SmtpMail.SmtpServer="smarthost"; //your smtp server hereSmtpMail.Send(myMail); } catch(Exception e){throw e; } }
public void CDOsendMail() {try{ CDO.Message oMsg = new CDO.Message();oMsg.From = "myaccount@test.com";oMsg.To = "myaccount@test.com";oMsg.Subject = "MailTest";oMsg.HTMLBody = "<html><body>Test</body></html>";CDO.IConfiguration iConfg = oMsg.Configuration;ADODB.Fields oFields = iConfg.Fields;oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2; oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="myaccount@test.com"; //sender mail oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="myaccount@test.com"; //email account oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username"; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password"; oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1; //value=0 代表Anonymous验证方式(不需要验证) //value=1 代表Basic验证方式(使用basic (clear-text) authentication. //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.) //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express) oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com";oFields.Update();oMsg.BodyPart.Charset="gb2312";oMsg.HTMLBodyPart.Charset="gb2312"; oMsg.Send();oMsg = null;} catch (Exception e){throw e;} }
220 Welcome to coremail System(With Anti-Spam) 2.1 EHLO 263.NET 250-192.168.30.29 250-PIPELINING 250-SIZE 10240000 250-ETRN 250-AUTH LOGIN 250 8BITMIME AUTH LOGIN 334 VXNlcm5hbWU6 bXlhY2NvdW50 334 UGFzc3dvcmQ6 bXlwYXNzd29yZA== 235 Authentication successful MAIL FROM:myaccount@263.NET 250 Ok RCPT TO:myaccount@263.NET 250 Ok Data 354 End data with <CR><LF>.<CR><LF> This is a testing email. haha. . 250 Ok: queued as AC5291D6406C4 QUIT 221 Bye
public void SendMail(MailMessage msg) { NetworkStream nwstream = GetConnection();WriteToStream(ref nwstream, "EHLO " + smtpHost + "\r\n");string welcomeMsg = ReadFromStream(ref nwstream);// implement HELO command if EHLO is unrecognized.if (IsUnknownCommand(welcomeMsg)){WriteToStream(ref nwstream, "HELO " + smtpHost + "\r\n");}CheckForError(welcomeMsg, ReplyConstants.OK); // Authentication is used if the u/p are suppliedAuthLogin(ref nwstream);WriteToStream(ref nwstream, "MAIL FROM: <" + msg.From.Address + ">\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK);SendRecipientList(ref nwstream, msg.To);SendRecipientList(ref nwstream, msg.CC);SendRecipientList(ref nwstream, msg.BCC);WriteToStream(ref nwstream, "DATA\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.START_INPUT);if (msg.ReplyTo.Name != null && msg.ReplyTo.Name.Length != 0){ WriteToStream(ref nwstream, "Reply-To: \"" + msg.ReplyTo.Name + "\" <" + msg.ReplyTo.Address + ">\r\n"); }else{ WriteToStream(ref nwstream, "Reply-To: <" + msg.ReplyTo.Address + ">\r\n"); }if (msg.From.Name != null && msg.From.Name.Length != 0){ WriteToStream(ref nwstream, "From: \"" + msg.From.Name + "\" <" + msg.From.Address + ">\r\n"); }else{ WriteToStream(ref nwstream, "From: <" + msg.From.Address + ">\r\n"); }WriteToStream(ref nwstream, "To: " + CreateAddressList(msg.To) + "\r\n");if (msg.CC.Count != 0){ WriteToStream(ref nwstream, "CC: " + CreateAddressList(msg.CC) + "\r\n"); }WriteToStream(ref nwstream, "Subject: " + msg.Subject + "\r\n");if (msg.Priority != null){ WriteToStream(ref nwstream, "X-Priority: " + msg.Priority + "\r\n"); }if (msg.Headers.Count > 0){SendHeaders(ref nwstream, msg);}if (msg.Attachments.Count > 0 || msg.HtmlBody != null){SendMessageBody(ref nwstream, msg);}else{WriteToStream(ref nwstream, msg.Body + "\r\n"); }WriteToStream(ref nwstream, "\r\n.\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK);WriteToStream(ref nwstream, "QUIT\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.QUIT);CloseConnection(); }private bool AuthLogin(ref NetworkStream nwstream) { if (username != null && username.Length > 0 && password != null && password.Length > 0) {WriteToStream(ref nwstream, "AUTH LOGIN\r\n");if (AuthImplemented(ReadFromStream(ref nwstream))){WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.ASCII.GetBytes(this.username.ToCharArray())) + "\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.SERVER_CHALLENGE);WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.ASCII.GetBytes(this.password.ToCharArray())) + "\r\n");CheckForError(ReadFromStream(ref nwstream), ReplyConstants.AUTH_SUCCESSFUL);return true;} } return false; }
在.NET中使用SMTP发送邮件相关推荐
- Lumen / Laravel 使用网易邮箱 SMTP 发送邮件
Laravel 是目前最流行的PHP框架,而Lumen 是 Laravel 的精简版,主要用于接口开发. Laravel 邮件发送服务基于 Symfony 组件 Swift Mailer. 本文记录了 ...
- Lumen / Laravel 5.5 使用网易邮箱 SMTP 发送邮件
2019独角兽企业重金招聘Python工程师标准>>> Laravel 是目前最流行的PHP框架,而Lumen 是 Laravel 的精简版,主要用于接口开发. Laravel 邮件 ...
- Spring Boot中使用JavaMailSender发送邮件
相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...
- C#中的SMTP配置Outlook.Com SMTP主机
如果你想以编程方式使用 Outlook.com或Gmail帐户作为 SMTP主机 发送电子邮件,也有为了得到这一切工作的几件事情要注意. 使用基本的System.Net.Mail库, ...
- python通过代理发送邮件_Python实现SMTP发送邮件详细教程
简介 Python发送邮件的教程本人在网站搜索的时候搜索出来了一大堆,但是都是说了一大堆原理然后就推出了实现代码,我测试用给出的代码进行发送邮件时都不成功,后面找了很久才找到原因,这都是没有一个详细的 ...
- php邮件发送tp,在Thinkphp3.2 中使用PHPMailer 发送邮件
phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考. phpmailer发送邮件功能很强大,今天真 ...
- foxmail 怎么把邮件格式默认为html_Python SMTP发送邮件-smtplib模块
在进入正题之前,我们需要对一些基本内容有所了解:常用的电子邮件协议有SMTP.POP3.IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别通过TCP端口25.110和143建立连接. Pyt ...
- php mail laravel,Laravel框架实现的使用smtp发送邮件功能示例
本文实例讲述了Laravel框架实现的使用smtp发送邮件功能.分享给大家供大家参考,具体如下: 1..env文件中配置 MAIL_DRIVER=smtp MAIL_HOST=smtp.邮箱后缀 MA ...
- play for scala 通过网易smtp发送邮件
最近用play来做一个小项目,里面用到了发送邮件的功能.这里我将这部分抽出来分享,毕竟目前来看paly于scala方面的中文资料在网上还是毕竟少,希望我的这篇文章能为有需要的人提供一点思路. 下面写下 ...
最新文章
- mod php是什么意思,mod函数是什么意思
- leetcode 218. 天际线问题
- git tag怎么使用_GIT中tag使用,打版本必备
- 转载子龙山人 cocos2d-x学习资源汇总(持续更新。。。)
- 用asp记录论坛用户密码(dvbbs,leadbbs等)
- 史上最全的自动驾驶研究报告(上)
- 有限域的Lagrange插值分析ZUC流密码S盒的代数结构
- 阿里巴巴未来十年使命、愿景和价值观
- 移动政务中的小程序技术
- 同时展示多张图片的无缝轮播
- 读《聪明人用方格笔记本》有图了有内容了
- win10固态硬盘分区 整数_电脑硬盘分区大小多少才合理【详细介绍】
- Android图表年度最强总结,一篇文章从入门到精通!
- PE文件解析(1):Dos头与NT头
- 部落卫队 (回溯搜索)
- Easyweb包括iframe中的页面跳转
- Argument of type ‘‘ is not assignable to parameter of type ‘never‘.
- 正则表达式解析器的编写
- 计算机考研复试面试常问问题 计算机网络篇(上)
- mysql的一些性能数据
热门文章
- Java ByteArrayOutputStream reset()方法及示例
- python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?
- 百善计算机学习,党建引领学做合一,志愿服务助力乡村振兴——计算机工程学院开展“百善孝为先”主题宣传文化墙墙绘涂鸦活动...
- UVA 10891——Game of Sum
- Mysql5.7后的password加密和md5
- linux网路编程之TCP状态转换及端口复用
- C++ i/o类库中 常用流类
- 再写单链表(不带头单链表)
- 网络基础: 浅析应用层一
- 安卓开发入门到精通!免费Android高级工程师学习资源,系列篇