专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

c#发送邮件,可发送多个附件

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:47:22       共计:3667 浏览

1:创建SendMail类

2:调用方法

SendMail send = new SendMail("123456@qq.com", "123456@163.com", "邮件内容测试", "邮件标题测试", "mail20181224");
send.Attachments(@"D:\\工作\\abc.txt");
send.Attachments(@"D:\\工作\\123.txt");
send.Send();

---------------------------------------------------------------------------------------------------下面是类-------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;

/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string password;//发件人密码 
/// <summary> 
/// 处审核后类的实例 
/// </summary> 
/// <param name="To">收件人地址</param> 
/// <param name="From">发件人地址</param> 
/// <param name="Body">邮件正文</param> 
/// <param name="Title">邮件的主题</param> 
/// <param name="Password">发件人密码</param>
public SendMail(string To, string From, string Body, string Title, string Password)
{
mailMessage = new MailMessage();
mailMessage.To.Add(To);
mailMessage.From = new System.Net.Mail.MailAddress(From);
mailMessage.Subject = Title;
mailMessage.Body = Body;
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
this.password = Password;
}
/// <summary> 
/// 添加附件 
/// </summary> 
public void Attachments(string Path)
{
string[] path = Path.Split(',');
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件 
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期 
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期 
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期 
mailMessage.Attachments.Add(data);//添加到附件中 
}
}
/// <summary> 
/// 异步发送邮件 
/// </summary> 
/// <param name="CompletedMethod"></param> 
public void SendAsync(SendCompletedEventHandler CompletedMethod)
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = "smtp." + mailMessage.From.Host;
smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件 
smtpClient.SendAsync(mailMessage, mailMessage.Body);
}
}
/// <summary> 
/// 发送邮件 
/// </summary> 
public void Send()
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = "smtp." + mailMessage.From.Host;
smtpClient.Send(mailMessage);
}
}
}

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:asp.net(C#)定时自动发送邮件 | ·下一条:ffmpeg 视频转ts切片 生成m3u8视频播放列表

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有