C#发送邮件

一、先定义邮件类型

/// <summary>
/// 邮件
/// </summary>
public class EmailEntity
{
/// <summary>
/// 发送人
/// </summary>
public string mailSender { get; set; }

/// <summary>
/// 收件人
/// </summary>
public string[] mailToArray { get; set; }

/// <summary>
/// 抄送
/// </summary>
public string[] mailCcArray { get; set; }

/// <summary>
/// 标题
/// </summary>
public string mailSubject { get; set; }

/// <summary>
/// 正文
/// </summary>
public string mailBody { get; set; }

/// <summary>
/// 发件人密码
/// </summary>
public string mailPwd { get; set; }

/// <summary>
/// SMTP邮件服务器
/// </summary>
public string host { get; set; }

/// <summary>
/// 正文是否是html格式
/// </summary>
public bool isbodyHtml { get; set; }

/// <summary>
/// 附件
/// </summary>
public string[] attachmentsPath { get; set; }
/// <summary>
/// 是否发送成功
/// </summary>
/// <returns></returns>
public bool Send()
{
//使用指定的邮件地址初始化MailAddress实例
MailAddress maddr = new MailAddress(mailSender);
//初始化MailMessage实例
MailMessage myMail = new MailMessage();
//向收件人地址集合添加邮件地址
if (mailToArray != null)
{
for (int i = 0; i < mailToArray.Length; i++)
{
myMail.To.Add(mailToArray[i].ToString());
}
}

//向抄送收件人地址集合添加邮件地址
if (mailCcArray != null)
{
for (int i = 0; i < mailCcArray.Length; i++)
{
myMail.CC.Add(mailCcArray[i].ToString());
}
}
//发件人地址
myMail.From = maddr;

//电子邮件的标题
myMail.Subject = mailSubject;

//电子邮件的主题内容使用的编码
myMail.SubjectEncoding = Encoding.UTF8;

//电子邮件正文
myMail.Body = mailBody;

//电子邮件正文的编码
myMail.BodyEncoding = Encoding.Default;

myMail.Priority = MailPriority.High;

myMail.IsBodyHtml = isbodyHtml;

//在有附件的情况下添加附件
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in attachmentsPath)
{
attachFile = new Attachment(path);
myMail.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
///写日志
}

SmtpClient smtp = new SmtpClient();
//指定发件人的邮件地址和密码以验证发件人身份
smtp.Credentials = new System.Net.NetworkCredential(mailSender, mailPwd);

//QQ邮箱使用ssl加密
//smtp.EnableSsl = true;
smtp.EnableSsl = false;
//设置SMTP邮件服务器
smtp.Host = host;

try
{
//将邮件发送到SMTP邮件服务器
smtp.Send(myMail);
return true;

}
catch (System.Net.Mail.SmtpException ex)
{
///写日志
return false;
}

}
}

二、调用实例

///附件路径

string   path = @"C:\Users\Adam\Desktop\Excel\test201803";

string[] attachmentsPath = new string[] { path };///可以发送多个附件


EmailEntity email = new EmailEntity();
email.mailSender = "发送人邮箱地址";
email.mailPwd = "发送人邮箱密码";
email.mailSubject = "邮件标题";
email.mailBody = "邮件内容";
email.isbodyHtml = false; //是否是HTML
email.attachmentsPath = attachmentsPath;///附件地址
email.host = "smtp.mxhichina.com";//如果是QQ邮箱则:smtp:qq.com,依次类推 smtp.163.com 阿里云 smtp.mail.aliyun.com 阿里企业邮箱 smtp.mxhichina.com
email.mailToArray = new string[] { SendEmail };//接收者邮件集合
//email.mailCcArray = new string[] { "******@qq.com" };//抄送者邮件集合

if (email.Send())
{
Console.WriteLine("服务状态 : 发送邮件成功,请注意查看!");
}
else
{
Console.WriteLine("服务状态 : 发送邮件失败!");
}

转载于:https://www.cnblogs.com/LeiYang5237/p/8565382.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用C#编写的控制台程序,用于发送邮件: ```csharp using System; using System.Net; using System.Net.Mail; class Program { static void Main(string[] args) { // 设置邮件服务器 string smtpServer = args[0]; int smtpPort = 25; SmtpClient client = new SmtpClient(smtpServer, smtpPort); // 设置发件人 string from = args[1]; string displayName = args[2]; MailAddress fromAddress = new MailAddress(from, displayName); // 设置收件人 string to = args[3]; MailAddress toAddress = new MailAddress(to); // 创建邮件对象 MailMessage message = new MailMessage(fromAddress, toAddress); // 设置抄送和密送 if (args.Length > 4) { string[] cc = args[4].Split(','); foreach (string c in cc) { message.CC.Add(c); } } if (args.Length > 5) { string[] bcc = args[5].Split(','); foreach (string b in bcc) { message.Bcc.Add(b); } } // 设置主题和内容 message.Subject = args[6]; message.Body = args[7]; // 添加附件 if (args.Length > 8) { string[] attachments = args[8].Split(','); foreach (string attachment in attachments) { Attachment file = new Attachment(attachment); message.Attachments.Add(file); } } // 发送邮件 client.Send(message); Console.WriteLine("邮件发送成功!"); } } ``` 在命令行中输入以下命令即可发送邮件: ``` SendMail 邮件服务器IP 发件人 显示名称 收件人 抄送 密送 主题 内容 文件 ``` 其中,邮件服务器IP、发件人、显示名称、收件人、主题和内容是必填项,抄送、密送和文件是可选项。如果有多个收件人、抄送或密送,可以用逗号分隔。如果有多个文件,也可以用逗号分隔。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值