首页 > 编程语言 >c#发送邮件的简单封装类

c#发送邮件的简单封装类

时间:2024-02-22 11:12:53浏览次数:37  
标签:封装 string attachmentFilePath c# senderEmail attachment new message 邮件

 1   public class EmailSender
 2   {
 3       private string smtpServer;
 4       private int smtpPort;
 5       private string senderEmail;
 6       private string senderPassword;
 7       private string subjectPrefix;
 8       private string emailContext;
 9 
10       /// <summary>
11       /// Constructor for EmailSender class.
12       /// </summary>
13       /// <param name="smtpServer">SMTP server address.</param>
14       /// <param name="smtpPort">SMTP server port.</param>
15       /// <param name="senderEmail">Sender's email address.</param>
16       /// <param name="senderPassword">Sender's email password.</param>
17       /// <param name="subjectPrefix">Email subject prefix.</param>
18       /// <param name="emailContext">Email content.</param>
19       public EmailSender(string smtpServer, int smtpPort, string senderEmail, string senderPassword, string subjectPrefix, string emailContext)
20       {
21           this.smtpServer = smtpServer;
22           this.smtpPort = smtpPort;
23           this.senderEmail = senderEmail;
24           this.senderPassword = senderPassword;
25           this.subjectPrefix = subjectPrefix;
26           this.emailContext = emailContext;
27       }
28 
29       /// <summary>
30       /// Sends an email.
31       /// </summary>
32       /// <param name="recipientEmail">Recipient's email address.</param>
33       /// <param name="ccEmails">List of CC email addresses.</param>
34       /// <param name="attachmentFilePath">Attachment file path.</param>
35       public void SendMail(string recipientEmail, string[] ccEmails, string attachmentFilePath = "")
36       {
37           try
38           {
39               SmtpClient smtp = new SmtpClient(smtpServer, smtpPort);
40               smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
41               smtp.Credentials = new NetworkCredential(senderEmail, senderPassword);
42 
43               MailMessage message = new MailMessage();
44               message.From = new MailAddress(senderEmail);
45               message.To.Add(new MailAddress(recipientEmail));
46               message.Subject = $"{subjectPrefix} ";
47               message.Body = $"{emailContext}";
48               message.BodyEncoding = Encoding.UTF8;
49               message.IsBodyHtml = true;
50 
51               if (ccEmails != null && ccEmails.Length > 0)
52               {
53                   foreach (string ccEmail in ccEmails)
54                   {
55                       message.CC.Add(new MailAddress(ccEmail));
56                   }
57               }
58 
59               if (File.Exists(attachmentFilePath))
60               {
61                   // Add attachment
62                   Attachment attachment = new Attachment(attachmentFilePath, MediaTypeNames.Application.Octet);
63                   attachment.ContentDisposition.CreationDate = File.GetCreationTime(attachmentFilePath);
64                   attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(attachmentFilePath);
65                   attachment.ContentDisposition.ReadDate = File.GetCreationTime(attachmentFilePath);
66                   message.Attachments.Add(attachment);
67               }
68 
69               smtp.Send(message);
70               Console.WriteLine("Email sent successfully!");
71           }
72           catch (Exception ex)
73           {
74               Console.WriteLine(ex.Message);
75           }
76       }
77   }
EmailSender

源码地址:https://github.com/yycb1994

标签:封装,string,attachmentFilePath,c#,senderEmail,attachment,new,message,邮件
From: https://www.cnblogs.com/INetIMVC/p/18026885

相关文章

  • Object方法 — Object.entries()
    Object方法—Object.entries()Object.entries()方法是JavaScript中的一个静态方法,用于返回一个给定对象自身可枚举属性的键值对数组。该方法接受一个对象作为参数,并将该对象的可枚举属性转换为一个二维数组,其中每个子数组包含两个元素:属性的键和属性的值。返回的数组中的......
  • Invicti v24.2.0 for Windows - 企业应用安全测试
    Invictiv24.2.0forWindows-企业应用安全测试InvictiStandard20Feb2024v24.2.0.43677请访问原文链接:https://sysin.org/blog/invicti/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgInvicti是一种自动化但完全可配置的Web应用程序安全扫描程序,使您能够......
  • Spring Kafka AckMode介绍
     原文链接:https://blog.csdn.net/qq1309664161/article/details/116994341一:AckMode介绍kafka消费端在读取数据后,会向Kafka服务端提交偏移量,来记录消费端读取数据的位置。提交偏移量分为手动提交和自动提交,为了保证数据读取的安全性,我们一般设置成手动提交偏移量。在Springb......
  • saltstack的使用
    1.安装https://docs.saltproject.io/salt/install-guide/en/latest/topics/overview.html1.1linux使用Bootstrapinstallation安装#下载文件bootstrap-salt.shcurl-obootstrap-salt.sh-Lhttps://bootstrap.saltproject.io#添加权限chmod+xbootstrap-salt.sh#安装#F......
  • blocks 单调栈、单调队列题解
    blocks题解:1、题面:2、分析:题意大概就是说,找一段最长的区间,并且这段区间的平均值>=k,那么我们可以对他的每一个值减去k,最终求和>=0即可。那我们需要对每个可能的左端点和右端点进行考虑,并以此让他们进行配对,看他们之间的区间和是否非负。那么我们先定住一个右端点,再依次考虑......
  • 分享个我自己封装的Datatable拓展
    废话不多说,直接上代码1publicstaticclassDataTableExtensions2{3///<summary>4///DetermineswhethertheDataTableisnullorempty.5///</summary>6///<paramname="dt">TheDataTabletocheck.<......
  • 05-JavaScript基础语法
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>JS-基础语法</title></head><body></body><script>//输出语句//1.alert()弹出警告框aler......
  • Qt 颜色对话框QColorDialog弹出时应用程序输出栏出现QWindowsWindow::setGeometry: Un
    引言在项目中点击按钮,弹出颜色选择对话框,但同时应用程序会在应用程序输出一栏中显示QWindowsWindow::setGeometry:Unabletosetgeometry180x30+345+311(frame:202x86+334+266)onQWidgetWindow/"QColorDialogClassWindow"on"\\.\DISPLAY1".Resultinggeometry:5......
  • Android无线调试--VSCode也能用
    只针对android11版本及以上版本官方文档https://developer.android.google.cn/studio/run/device?hl=zh-cn#wireless官方文档讲的是使用AndroidStudio开发工具进行开发时的无线连接如下: 点进去之后会有两个选项,一个是扫码,一个是使用配对码 拿出手机,打开开发者选项--》......
  • Blocks—单调栈
    思路:题目意思是求平均数>=k的最长序列先求出a[i]-k的前缀和就是求sum[i]-sum[j]>=0的最大i-j当j<=k<=isum[j]<=sum[k]j<=k<=isum[j]<=sum[k]更新i的时候,k就不如j优所以处理出来一个单调上升的数组(stak),那答案就在里面选倒序更新,单调栈一直减减就好因为如果用stak[top]更新了i,因......