C# 发送邮件
2021-06-11 15:06
标签:app 注册 edm 编写 oct sync eth ram def 一、简单发送邮件 MailMessage m = new MailMessage("发件邮箱@163.com", "收件邮箱@163.com", "标题", "内容"); SmtpClient client = new SmtpClient(); client.Host = "发件服务器可以从相关网站查看例如163的是:smtp.163.com"; 二、发送带附件的、发送给多人的邮件。 1. 编写用户发送邮件的类:【此类参考网络两处资源组合而成】 class SendMail public void AddTo(string to) 2. 主程序: SendMail m = new SendMail("收件邮箱地址", "发件邮箱地址", "内容", "标题", "授权密码"); m.Attachments("d:/文件名.docx"); C# 发送邮件 标签:app 注册 edm 编写 oct sync eth ram def 原文地址:https://www.cnblogs.com/-liuming-/p/10552272.html
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("发件邮箱@163.com", "授权密码");
client.Send(m);
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string password;//发件人密码
///
/// 处审核后类的实例
///
/// 收件人地址
/// 发件人地址
/// 邮件正文
/// 邮件的主题
/// 发件人密码
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;
}
{
mailMessage.To.Add(to);
}
///
/// 添加附件
///
public void Attachments(string Path)
{
string[] path = Path.Split(‘,‘);
Attachment data;
ContentDisposition disposition;
for (int i = 0; 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);//添加到附件中
}
}
///
/// 异步发送邮件
///
///
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);
}
}
///
/// 发送邮件
///
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);
}
}
}
m.AddTo("其他收件地址");
m.Send();