C#-工具-EmailSender-发送邮件
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
namespace WebHelper
{
public class EmailSender
{
/// <param name="url"></param>
/// <param name="paramData"></param>
/// <param name="headerDic"></param>
/// <returns></returns>
public static string HttpPost(string url, string paramData, Dictionary<string, string> headerDic = null)
{
string result = string.Empty;
try
{
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
if (headerDic != null && headerDic.Count > 0)
{
foreach (var item in headerDic)
{
wbRequest.Headers.Add(item.Key, item.Value);
}
}
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch (Exception ex)
{ }
return result;
}
private static String AssemblePostData(String sendEmailTitle, String errDetail)
{
Dictionary<string, string> Info = new Dictionary<string, string>();
Info.Add("time", new DateTime().ToString("yyyy-MM-dd HH:mm-ss"));
Info.Add("ua", "Android");
Info.Add("message", errDetail);
Info.Add("website", sendEmailTitle);
return BuildPostQuery(Info);
}
/// <summary>
/// 组装普通文本请求参数。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
public static string BuildPostQuery(IDictionary<string, string> parameters)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 发送邮件(自定义发送配置)
/// </summary>
/// <param name="reEmailPath">收件人邮箱地址</param>
/// <param name="reEmailName">收件人名称</param>
/// <param name="sendEmailTitle">发件标题</param>
/// <param name="sendEmailContent">发件内容</param>
/// <param name="sendEmailUser">发件人账户</param>
/// <param name="sendEmailPwd">发件人密码</param>
/// <param name="sendEmailName">发件人名称</param>
/// <param name="sendEmailPort">端口</param>
/// <param name="sendEmailHost">发送SMTP地址</param>
/// <returns></returns>
public static bool SendEmailMsg(string reEmailPath, string reEmailName, string sendEmailTitle, string sendEmailContent, string sendEmailUser, string sendEmailPwd, string sendEmailName, string sendEmailPort, string sendEmailHost)
{
SmtpClient client = new SmtpClient();
//设置为发送认证消息
client.UseDefaultCredentials = true;
client.EnableSsl = true;
//string Email = EmailUser.Split('@')[0];
//获取或设置用于验证发件人身份的凭据。
client.Credentials = new System.Net.NetworkCredential(sendEmailUser, sendEmailPwd);
//经过ssl(安全套接层)加密,163邮箱SSL协议端口号为465/994,关闭SSL时端口为25,
//qq邮箱SSL协议端口号为465或587,关闭SSL时端口同样为25,不过用SSL加密后发送邮件都失败,具体原因不知
//client.EnableSsl = true;
client.Port = Convert.ToInt32(sendEmailPort); //端口号
client.Host = sendEmailHost; //获取或设置用于 SMTP 事务的主机的名称或 IP 地址
try
{
client.Send(InitMailMessage(reEmailPath, reEmailName, sendEmailTitle, sendEmailContent, sendEmailUser, sendEmailName));
return true;
}
catch (System.Net.Mail.SmtpException ex)
{
return false;
}
}
/// <summary>
/// 初始化信件相关信息
/// </summary>
/// <param name="reEmailPath">收件人</param>
/// <param name="reEmailName">收件人名称</param>
/// <param name="sendEmailTitle">发送标题</param>
/// <param name="sendEmailContent">发送内容</param>
/// <param name="sendEmailUser">发件人</param>
/// <param name="sendEmailName">发件人名称</param>
/// <returns></returns>
public static MailMessage InitMailMessage(string reEmailPath, string reEmailName, string sendEmailTitle, string sendEmailContent, string sendEmailUser, string sendEmailName)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(sendEmailUser, sendEmailName); //发件人
if (reEmailPath != "")
{
MailAddress mailAdd = new MailAddress(reEmailPath, reEmailName); //收件人
mail.To.Add(mailAdd); //收件人
}
else
{
mail.To.Add(reEmailPath); //收件人
}
mail.Subject = sendEmailTitle; //主题
mail.Body = sendEmailContent; //内容
//邮件主题和正文的编码格式
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true; //邮件正文允许html编码
mail.Priority = MailPriority.Normal; //优先级
//密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址
//mail.Bcc.Add("qq@qq.com");
//抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁
//mail.CC.Add("qq@qq.com");
//mail.Attachments.Add(new Attachment("D:\\1.doc")); //添加附件
return mail;
}
}
}
正文到此结束