//send(服务器邮箱地址,邮箱地址,邮箱密码,发送到那个邮箱的地址,用户名,邮件标题,内容,ref msg,编码方式,bool)
bool IsSend=NetMail.Send("smtp.126.com", "newdssddpostmail@ww.com", "qqqqq", email, "", title, ebody, ref msg, "utf-8", true);
string email = txtEmailAddress.Text;
string title = "emailTitle";
string ebody=@"<p style='margin-left:20px;'>First Name:"+txtFirstName.Text+"</p>";
ebody+="<p style='margin-left:20px;'>Last Name:"+txtLastName.Text+"</p>";
ebody+="<p style='margin-left:20px;'>Emaill Address:"+txtEmailAddress.Text+"</p>";
ebody+="<p style='margin-left:20px;'>Phone Number:"+txtPhoneNumber.Text+"</p>";
ebody+="<p style='margin-left:20px;'>I am interested to learn more about "+txtAbout.SelectedItem.ToString()+"</p>";
ebody+="<p style='margin-left:20px;'>Additional Comments:"+txtComment.Text+"</p>";
string msg = "";
//发送邮件NetMail类如下
bool IsSend = NetMail.Send("smtp.126.com", "newdssddpostmail@ww.com", "qqqq", "23232555@qq.com", "", title, ebody, ref msg, "utf-8", true);
if (IsSend) {
ClientScript.RegisterStartupScript(this.GetType(), "jsname", "alert('send Success!')", true); //发送邮件成功
}
using System;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace SendMail
{
/// <summary>
/// 通过System.Net.SmtpClient发送邮件
/// </summary>
public class NetMail
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="SmtpServer">邮箱服务器</param>
/// <param name="ServerAccount">邮箱帐号</param>
/// <param name="ServerPassword">邮箱密码</param>
/// <param name="Toemail">目标邮件地址</param>
/// <param name="ToUserName">目标用户名,可不填。</param>
/// <param name="Subject">邮件主题</param>
/// <param name="Body">邮件内容</param>
/// <param name="msg">返回信息</param>
/// <param name="Encode">邮件编码</param>
/// <param name="isHTML">是否启用HTML,true为启用。</param>
/// <returns></returns>
public static bool Send(string SmtpServer,string ServerAccount,string ServerPassword,string Toemail, string ToUserName, string Subject, string Body, ref string msg, string Encode, bool isHTML)
{
bool result = false;
SmtpClient client = new SmtpClient(SmtpServer);
client.Credentials = new NetworkCredential(ServerAccount, ServerPassword);
MailMessage mail = new MailMessage(ServerAccount, Toemail);
mail.SubjectEncoding = mail.BodyEncoding = Encoding.GetEncoding(Encode);
mail.IsBodyHtml = isHTML;
mail.Subject = Subject;
mail.Body = Body;
try
{
client.Send(mail);
result = true;
}
catch (Exception ex)
{
result = false;
msg = ex.Message;
}
return result;
}
}
}