C#邮件发送(含附件)
2024-08-30 23:52:12
class SendEmail
{
static void Main(string[] args)
{
string from = "发件人@yingu.com";
string fromer = "赵海莹";
string to = "收件人@yingu.com";
string toer = "刘春喜";
string Subject = "逾期数据报表";
string file = string.Concat(System.AppDomain.CurrentDomain.BaseDirectory, "ReprotingTemp\\逾期订单列表.xls");
string Body = "逾期数据报表";
//企业邮箱smtp
string SMTPHost = "smtp.qiye.163.com";
string SMTPuser = "发件人@yingu.com";
string SMTPpass = "******(发件人邮箱密码)";
sendmail(from, fromer, to, toer, Subject, Body, file, SMTPHost, SMTPuser, SMTPpass);
}
/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">接受者邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param>
/// <returns></returns>
private static bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
{
////设置from和to地址
MailAddress from = new MailAddress(sfrom, sfromer);
MailAddress to = new MailAddress(sto, stoer); ////创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); //// 添加附件
if (sfile != "")
{
oMail.Attachments.Add(new Attachment(sfile));
} ////邮件标题
oMail.Subject = sSubject; ////邮件内容
oMail.Body = sBody; ////邮件格式
oMail.IsBodyHtml = false; ////邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); ////设置邮件的优先级为高
oMail.Priority = MailPriority.High; ////发送邮件
SmtpClient client = new SmtpClient();
////client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
//企业邮箱需设置端口,个人邮箱不需要
client.Port = ;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
//Response.Write(err.Message.ToString());
return false;
}
finally
{
////释放资源
oMail.Dispose();
} }
}
最新文章
- Java设计模式-访问者模式(Visitor)
- HDU 1044 Collect More Jewels(BFS+DFS)
- Cocos2d-x——Cocos2d-x 屏幕适配新解【转载】
- 由 OR 引起的死循环
- Oracle INTERVAL DAY TO SECOND数据类型
- IIS HTTP重定向到HTTPS
- Java中的局部变量表及使用jclasslib进行查看
- Automatic Generation of Animated GIFs from Video论文研读及实现
- USACO JAN14 奶牛冰壶运动 凸包+判定
- [原创]基于Zybo SDIO WiFi模块调试
- puppeteer 拦截页面请求
- 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)
- Python可迭代对象中的添加和删除(add,append,pop,remove,insert)
- C++ Exception机制
- Qt ------ QFileDialog
- JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理
- Django 设置media static
- 异常could not retrieve snapshot
- poj1273
- Django 2.0 学习(20):Django 中间件详解
热门文章
- Ubuntu下查看服务器cpu是否支持VT
- Python爬虫+颜值打分,5000+图片找到你的Mrs. Right
- Android 6.0权限分组
- Java反射机制实战——字段篇
- VC++函数只被调用一次
- 使用Spring框架的步骤
- javascript入门经典(第五版)-清华出版社之“经典”错误
- Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout
- NGS数据格式介绍
- chr()返回值是当前整数对应的 ASCII 字符。