邮件发送
方法一:使用System.Web.Mail命名空#region 发送邮件:此方法失败
protected void SendFailed() { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = "test@ gmail.com"; mail.To = " test@ gmail.com "; mail.Subject = "For Test"; mail.Priority = System.Web.Mail.MailPriority.Normal; mail.BodyEncoding = Encoding.Default; mail.BodyFormat = MailFormat.Html; mail.Body = "this is a Email!<input type='button' value='ok'/>"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test"); //set your username here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); //set your password here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); SmtpMail.SmtpServer = "smtp.gmail.com"; SmtpMail.Send(mail); } #endregion间(此方法我测试没有成功过)
方法二:使用System.Net.Mail命名空间(此方法测试成功)
我使用的gmail的邮箱,以及他提供免费smtp服务,之前试了好几个邮箱都不成功。Gmail的smtp服务必须经过ssl加密,才可以验证成功。
#region 发送邮件:此方法可行 protected void SendSuccess() { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new MailAddress("test@gmail.com", "someone");//必须是提供smtp服务的邮件服务器 message.To.Add(new MailAddress("test@yahoo.com.cn")); message.Subject = "测试邮件" ; message.CC.Add(new MailAddress("test@126.com")); message.Bcc.Add(new MailAddress("test@126.com")); message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; message.Body = "邮件发送测试"; message.Priority = System.Net.Mail.MailPriority.High; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); // 587;//Gmail使用的端口 client.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password"); //这里是申请的邮箱和密码 client.EnableSsl = true; //必须经过ssl加密 try { client.Send(message); Response.Write("邮件已经成功发送到" + message.To.ToString() + "<br>"); } catch (Exception ee) { Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ ); } } #endregion
邮件接收
我使用的是LumiSoft.Net这个开源的项目,也是从一个网友哪里看到的下载地址,然后自己看了下代码,写了个简单的接收方法。首先将代码中relrease目录下的dll文件引用到项目中。
using LumiSoft.Net.POP3.Client; using LumiSoft.Net.Mail; …… public IList<Mail_Message> ReceiveMail() { IList<Mail_Message> mailList = new List<Mail_Message>(); using (POP3_Client client = new POP3_Client()) { client.Connect("pop.gmail.com",995,true); client.Authenticate("zw.seaman", "zw_seaman", false); POP3_ClientMessageCollection coll = client.Messages; for (int i = 0; i < coll.Count; i++) { POP3_ClientMessage message = coll[i]; Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte()); mailList.Add(mm); } } return mailList; } protected void Page_Load(object sender, EventArgs e) { IList<Mail_Message> mailList = new ZMail.Mail().ReceiveMail(); foreach (Mail_Message mail in mailList) { StringBuilder sb = new StringBuilder(); sb.Append(mail.From.ToString()).Append(" 发送给 "); sb.Append(mail.To.ToString()).Append("<br/>") ; sb.Append(mail.Subject).Append("<br/>"); sb.Append(mail.BodyHtmlText).Append("<hr/>"); Response.Write(sb.ToString()); } }
以上就是C# 邮件发送和接收实现代码的内容,更多相关内容请关注PHP中文网(www.php.cn)!

要开始C#.NET开发,你需要:1.了解C#的基础知识和.NET框架的核心概念;2.掌握变量、数据类型、控制结构、函数和类的基本概念;3.学习C#的高级特性,如LINQ和异步编程;4.熟悉常见错误的调试技巧和性能优化方法。通过这些步骤,你可以逐步深入C#.NET的世界,并编写高效的应用程序。

C#和.NET的关系是密不可分的,但它们不是一回事。C#是一门编程语言,而.NET是一个开发平台。C#用于编写代码,编译成.NET的中间语言(IL),由.NET运行时(CLR)执行。

C#.NET依然重要,因为它提供了强大的工具和库,支持多种应用开发。1)C#结合.NET框架,使开发高效便捷。2)C#的类型安全和垃圾回收机制增强了其优势。3).NET提供跨平台运行环境和丰富的API,提升了开发灵活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通过不断的更新和优化,适应了新兴技术的需求。1)C#9.0和.NET5引入了记录类型和性能优化。2).NETCore增强了云原生和容器化支持。3)ASP.NETCore与现代Web技术集成。4)ML.NET支持机器学习和人工智能。5)异步编程和最佳实践提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1
好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版