1个回答
展开全部
前提是你的邮箱必须开通smtp了
#region 显示邮件发送是否成功
protected void Page_Load(object sender, EventArgs e)
{
bool bResult = SendEDMEmailBy47Server("收件人邮箱如:1204472077@qq.com", "发件人邮箱如:hu_hujun@163.com", "内容", "标题", "登录邮箱如:hu_hujun@163.com", "密码", Server.MapPath("js/jquery.js"));
//this.hidBool.Value = bResult.ToString();
if (bResult)
{
this.Response.Write("发送成功!");
}
else {
this.Response.Write("发送失败!");
}
}
#endregion
#region 发送邮件方法 支持带附件
/// <summary>
/// 发送edm邮件,可以附加附件
/// </summary>
/// <param name="StrTo">收件人地址</param>
/// <param name="strFrom">发件人地址</param>
/// <param name="StrBody">邮件内容</param>
/// <param name="strSubject">邮件主题</param>
/// // <param name="name">发件人地址</param>
/// // <param name="pwd">发件人密码</param>
/// <param name="path">附件路径</param>
/// <returns>返回是否发送成功</returns>
public static bool SendEDMEmailBy47Server(string StrTo, string strFrom, string StrBody, string strSubject,string name,string pwd,string path)
{
string sHos = string.Empty;//根据发送邮箱地址获取你的smtp主机
if (name.Contains("@163"))
{
sHos = "smtp.163.com";
}
else if (name.Contains("@126"))
{
sHos = "smtp.126.com";
}
else if (name.Contains("@qq"))
{
sHos = "smtp.qq.com";
}
else if (name.Contains("@sohu"))
{
sHos = "smtp.sohu.com";
}
else if (name.Contains("@sina"))
{
sHos = "smtp.sina.com";
}
else if (name.Contains("@tom"))
{
sHos = "smtp.tom.com.cn";
}
else if (name.Contains("@foxmail"))
{
sHos = "smtp.foxmail.com";
}
else if (name.Contains("@139"))
{
sHos = "smtp.139.com";
}
System.Net.Mail.MailAddress fromEmail = new System.Net.Mail.MailAddress(strFrom);//发件人的地址
System.Net.Mail.MailAddress toEmail = new System.Net.Mail.MailAddress(StrTo);//收件人地址
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);
mail.Body = StrBody;
mail.Subject = strSubject;
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.Normal;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = sHos;//这里设置成你的smtp主机名或者ip地址
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(name, pwd);//你的用户名和密码
//以下是邮件附件部分,path一般是从FileUpload控件中取~
FileInfo fi;
if (path != "")
{
fi = new FileInfo(path);
if (fi.Exists)
{
System.Net.Mail.Attachment firstatt = new System.Net.Mail.Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposion = new System.Net.Mime.ContentDisposition();
disposion.CreationDate = System.IO.File.GetCreationTime(path);
disposion.ModificationDate = System.IO.File.GetLastWriteTime(path);
disposion.ReadDate = System.IO.File.GetLastAccessTime(path);
mail.Attachments.Add(firstatt);
}
}
try
{
smtpClient.Send(mail);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
#endregion
#region 显示邮件发送是否成功
protected void Page_Load(object sender, EventArgs e)
{
bool bResult = SendEDMEmailBy47Server("收件人邮箱如:1204472077@qq.com", "发件人邮箱如:hu_hujun@163.com", "内容", "标题", "登录邮箱如:hu_hujun@163.com", "密码", Server.MapPath("js/jquery.js"));
//this.hidBool.Value = bResult.ToString();
if (bResult)
{
this.Response.Write("发送成功!");
}
else {
this.Response.Write("发送失败!");
}
}
#endregion
#region 发送邮件方法 支持带附件
/// <summary>
/// 发送edm邮件,可以附加附件
/// </summary>
/// <param name="StrTo">收件人地址</param>
/// <param name="strFrom">发件人地址</param>
/// <param name="StrBody">邮件内容</param>
/// <param name="strSubject">邮件主题</param>
/// // <param name="name">发件人地址</param>
/// // <param name="pwd">发件人密码</param>
/// <param name="path">附件路径</param>
/// <returns>返回是否发送成功</returns>
public static bool SendEDMEmailBy47Server(string StrTo, string strFrom, string StrBody, string strSubject,string name,string pwd,string path)
{
string sHos = string.Empty;//根据发送邮箱地址获取你的smtp主机
if (name.Contains("@163"))
{
sHos = "smtp.163.com";
}
else if (name.Contains("@126"))
{
sHos = "smtp.126.com";
}
else if (name.Contains("@qq"))
{
sHos = "smtp.qq.com";
}
else if (name.Contains("@sohu"))
{
sHos = "smtp.sohu.com";
}
else if (name.Contains("@sina"))
{
sHos = "smtp.sina.com";
}
else if (name.Contains("@tom"))
{
sHos = "smtp.tom.com.cn";
}
else if (name.Contains("@foxmail"))
{
sHos = "smtp.foxmail.com";
}
else if (name.Contains("@139"))
{
sHos = "smtp.139.com";
}
System.Net.Mail.MailAddress fromEmail = new System.Net.Mail.MailAddress(strFrom);//发件人的地址
System.Net.Mail.MailAddress toEmail = new System.Net.Mail.MailAddress(StrTo);//收件人地址
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);
mail.Body = StrBody;
mail.Subject = strSubject;
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.Normal;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = sHos;//这里设置成你的smtp主机名或者ip地址
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(name, pwd);//你的用户名和密码
//以下是邮件附件部分,path一般是从FileUpload控件中取~
FileInfo fi;
if (path != "")
{
fi = new FileInfo(path);
if (fi.Exists)
{
System.Net.Mail.Attachment firstatt = new System.Net.Mail.Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposion = new System.Net.Mime.ContentDisposition();
disposion.CreationDate = System.IO.File.GetCreationTime(path);
disposion.ModificationDate = System.IO.File.GetLastWriteTime(path);
disposion.ReadDate = System.IO.File.GetLastAccessTime(path);
mail.Attachments.Add(firstatt);
}
}
try
{
smtpClient.Send(mail);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
#endregion
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询