邮件发送 EMailHelper
引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
EMailHepler类:
public static class EmailHelper
{
/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="fromDisplayName">发件邮箱显示名</param>
/// <param name="sto">收件邮箱</param>
/// <param name="sSubject">主题</param>
/// <param name="sBody">内容</param>
/// <param name="sfiles">附件路径,绝对路径</param>
/// <param name="ccto">抄送地址</param>
/// <param name="isBodyHtml">邮件是否支持html格式</param>
/// <returns></returns>
public static bool Sendmail(string fromDisplayName, string sto, string sSubject, string sBody,
string[] sfiles = null, string[] ccto = null, bool isBodyHtml = false)
{
////设置from和to地址
MailAddress from = new MailAddress(EmailSection.Instance.SMTPuser, fromDisplayName);
MailAddress to = new MailAddress(sto); ////创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); //// 添加附件
if (sfiles != null)
{
foreach (var item in sfiles)
{
oMail.Attachments.Add(new Attachment(item));
}
} //添加抄送用户
if (ccto != null)
{
foreach (var item in ccto)
{
oMail.CC.Add(new MailAddress(item));
}
}
////邮件标题
oMail.Subject = sSubject; ////邮件内容
oMail.Body = sBody; ////邮件格式
oMail.IsBodyHtml = isBodyHtml; ////邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); ////设置邮件的优先级为高
oMail.Priority = MailPriority.Normal; ////发送邮件
SmtpClient client = new SmtpClient();
////client.UseDefaultCredentials = false;
client.Host = EmailSection.Instance.SMTPHost;
client.Credentials = new NetworkCredential(EmailSection.Instance.SMTPuser, EmailSection.Instance.SMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
return false;
}
finally
{
////释放资源
oMail.Dispose();
} }
}
EmailSection配置相关信息:
public class EmailSection : ConfigurationSection
{
private static EmailSection _instance = null; public static EmailSection Instance
{
get
{
if (_instance == null)
{
_instance = (EmailSection)ConfigurationManager.GetSection(typeof(EmailSection).Name);
}
return _instance;
}
}
[ConfigurationProperty("SMTPHost", IsRequired = true)]
public string SMTPHost
{
get
{ return (String)this["SMTPHost"]; }
set
{ this["SMTPHost"] = value; }
}
[ConfigurationProperty("SMTPuser", IsRequired = true)]
public string SMTPuser
{
get
{ return (String)this["SMTPuser"]; }
set
{ this["SMTPuser"] = value; }
}
[ConfigurationProperty("SMTPpass", IsRequired = true)]
public string SMTPpass
{
get
{ return (String)this["SMTPpass"]; }
set
{ this["SMTPpass"] = value; }
}
}
配置文件配置:
<EmailSection SMTPHost = "smtp.exmail.qq.com" SMTPuser = "developer@laiyihuo.com" SMTPpass = "laiyihuo_1"></EmailSection>
邮件发送 EMailHelper的更多相关文章
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- J2EE 邮件发送那些事儿
距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...
- 结合ABP源码实现邮件发送功能
1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...
- SSH项目里面 忘记密码的邮件发送功能
package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...
- [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作
这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...
- java spring 邮件发送
开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...
- Java邮件发送与接收原理
一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...
- c#实现邮件发送链接激活
2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...
- .Net(C#)最简单的邮件发送案例
一.序言 刚开始接触邮件发送功能的时候,在网上找的资料都挺复杂的!对于新手入门有点难(至少对于本人来说,第一次接触的时候确实不容易).这里就写一段简单的邮箱发送代码,备忘,也给新手一个参考(相关类的字 ...
随机推荐
- 若后台的Activity被系统回收...
你后台的Activity被系统回收怎么办?如果后台的Activity由于某种原因被系统回收了,如何在被系统回收之前保存当前状态? 除了在栈顶的Activity,其他的Activity都有可能在内存不足 ...
- V9 二次开发技术篇之 模型数据库
应V9粉丝的建议,本人今天讲一下 MVC中的M 数据库模型 首先 在 phpcms\model 建一个模型文件test_model.class.php <?phpdefined('IN_PHP ...
- DEDE常见的错误(转)
1:dedecms文章录入的时候,如何控制文章重复. 在dede/article_add.php里面,加入该程序就OK了 if($cfg_check_title == 'Y'){ ...
- mysql中的JOIN用法总结
join是mysql中一个基础的关键词,一般在多表连接查询中使用,这里做一下总结 1.JOIN的语法格式 table_references: table_reference [, table_refe ...
- LNMP笔记:域名重定向、读写权限、显示WP主题、北京时间
边写边记,以后还会用到的. 将 xxx.com 重定向到 www.xxx.com 1.打开 /usr/local/nginx/conf/vhost/你网站的域名.com.conf 2.查看原有的 se ...
- 如何使用service命令来管理nginx
如何使用service命令来管理nginx??? 如: service nginx start service nginx restart service nginx stop service ngi ...
- Javascript OrderBy
要在js 实现orderBy基本知识就是 array.sortarray.sort(function(a,b){ a 表示 row 0 b 表示 row 1 它会loop多次你可以比较 if(a &g ...
- hdu 1543 Paint the Wall
http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...
- op编译信赖的库
Table of known prerequisites and their corresponding packages Here's a table with the package name f ...
- 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET
今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...