小功能,备忘
MailMessage objMailMessage = new MailMessage();//引入System.Net
objMailMessage.From = new MailAddress("**@sina.com");//源邮件地址 (发件人地址)
objMailMessage.To.Add(new MailAddress("**@163.com"));//目的邮件地址(收件人地址)。可以有多个收件人,用逗号隔开
objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;//文本字符编码
objMailMessage.Subject = "This is test";//邮件标题
StringBuilder Builder = new StringBuilder();//构建邮件文本内容,内容都采用标签形式
Builder.Append("Hi,Pippo<br/><br/> This is testing Email.");//文字
Builder.Append("<a href='http://www.baidu.com'>请单击链接</a>");//网址链接
Builder.Append("<image src='http://www.zhituad.com/photo2/00/87/68/91b1OOOPIC4e.jpg'></image>");//图片
objMailMessage.Body = Builder.ToString();
objMailMessage.IsBodyHtml = true;//可以以HTML方式查看邮件内容
</pre><pre name="code" class="csharp"><span style="white-space:pre"> </span> string file=@"";//附件路径
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(file, MediaTypeNames.Application.Octet);
<span style="white-space:pre"> </span> ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
objMailMessage.Attachments.Add(data);//添加附件
SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.Host = "smtp.sina.com";//smtp.sina.com,smtp.qq.com;指定发送邮件服务器(Smtp服务器,根据发件人所使用的邮箱进行配置)
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new System.Net.NetworkCredential("**.com", "**");指定发件人的邮箱的密码与用户名(网站管理员。)
//objSmtpClient.EnableSsl = true;//SMTP 服务器要求安全连接需要设置此属性
try
{
objSmtpClient.Send(objMailMessage);
MessageBox.Show("ok");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义