C#中怎么实现Email邮件发送功能
C# 中怎么实现Email邮件发送功能 ,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联公司是专业的竹溪网站建设公司,竹溪接单;提供成都做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行竹溪网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
namespace CalslNum.Helper
{
///
///发送邮件类
///
public class MailService
{
///
/// 发送邮件程序调用方法 SendMail("abc@126.com", "某某人", "cba@126.com", "你好", "我测试下邮件", "邮箱登录名", "邮箱密码", "smtp.126.com", true,);
///
/// 发送人邮件地址
/// 发送人显示名称
/// 发送给谁(邮件地址)
/// 标题
/// 内容
/// 邮件登录名
/// 邮件密码
/// 邮件服务器 smtp服务器地址
/// 是否是HTML格式的邮件
/// send ok
public static bool SendMail(string from, string fromname, string to, string subject, string body, string server, string username, string password, bool IsHtml)
{
//邮件发送类
MailMessage mail = new MailMessage();
try
{
//是谁发送的邮件
mail.From = new MailAddress(from, fromname);
//发送给谁
mail.To.Add(to);
//标题
mail.Subject = subject;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = IsHtml;
//邮件服务器和端口
SmtpClient smtp = new SmtpClient(server, 25);
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//发件人身份验证,否则163 发不了
smtp.UseDefaultCredentials = true;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(username, password);
//超时时间
smtp.EnableSsl = false;
smtp.Timeout = 10000;
smtp.Send(mail);
return true;
}
catch (Exception)
{
return false;
}
finally
{
mail.Dispose();
}
}
//读取指定URL地址的HTML,用来以后发送网页用
public static string ScreenScrapeHtml(string url)
{
//读取stream并且对于中文页面防止乱码
StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8);
string str = reader.ReadToEnd();
reader.Close();
return str;
}
//发送plaintxt
public static bool SendText(string from, string fromname, string to, string subject, string body, string server, string username, string password)
{
return SendMail(from, fromname, to, subject, body, server, username, password, false);
}
//发送HTML内容
public static bool SendHtml(string from, string fromname, string to, string subject, string body, string server, string username, string password)
{
return SendMail(from, fromname, to, subject, body, server, username, password, true);
}
//发送制定网页
public static bool SendWebUrl(string from, string fromname, string to, string subject, string server, string username, string password, string url)
{
//发送制定网页
return SendHtml(from, fromname, to, subject, ScreenScrapeHtml(url), server, username, password);
}
//默认发送格式
public static bool SendEmailDefault(string ToEmail,string f_username,string f_pass,string f_times)
{
StringBuilder MailContent = new StringBuilder();
MailContent.Append("亲爱的×××会员:
");
MailContent.Append(" 您好!你于");
MailContent.Append(DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss"));
MailContent.Append("通过×××管理中心审请找回密码。
");
MailContent.Append(" 为了安全起见,请用户点击以下链接重设个人密码:
");
string url = "http://www.×××.×××/SignIn/Rest?u=" + f_username + "&s=" + f_pass + "&t=" + f_times; 114 MailContent.Append("" + url + "
"); 115 MailContent.Append(" (如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可。)"); 116 return SendHtml(ConfigurationManager.AppSettings["EmailName"].ToString(), "会员管理中心", ToEmail, "×××找回密码", MailContent.ToString(), ConfigurationManager.AppSettings["EmailService"].ToString(), ConfigurationManager.AppSettings["EmailName"].ToString(), ConfigurationManager.AppSettings["EmailPass"].ToString()); //这是从webconfig中自己配置的。 117 } 118 } 119 }webconfig配置信息
//说明:这里面的"EmailService"得与你自己设置邮箱的smtp/POP3/...服务要相同, 大部分是根据@后面的进行配置。我是用163邮箱配置的。 可以根据自己需要自己配置。
后台调用的方法
public ActionResult SendEmail(string EmailName)
{
EmailName = Helper.FI_DesTools.DesDecrypt(EmailName);
if (!Regex.IsMatch(EmailName, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{
return Content("0");
}
string f_username = "";
string f_pass = "";
string f_times = Helper.FI_DesTools.DesEncrypt(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
List list = (from a in users where a.emailaddress == EmailName select a).ToList();
if (list.Count > 0)
{
f_username = Helper.FI_DesTools.DesEncrypt(list[0].×××);
f_pass = Helper.FI_DesTools.DesEncrypt(list[0].×××);
bool flag = Helper.MailService.SendEmailDefault(EmailName, “×××”,“×××”, “×××”); //这里面的参数根据自己需求自己定,最好进行加密
if (flag)
{
return Content("true");
}
else
{
return Content("false");
}
}
else {
return Content("false");
}
} 发送完邮件效果图如下:

关于C# 中怎么实现Email邮件发送功能 问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
本文题目:C#中怎么实现Email邮件发送功能
当前路径:http://www.jxjierui.cn/article/gcijsh.html


咨询
建站咨询
