我有一個簡單一點的。 //page_load事件調用 if (!Page.IsPostBack) { CreateCopyMessage("m1"); } } public static void CreateCopyMessage(string server)//string server { MailAddress from = new MailAddres ("[email protected]", ""); MailAddress to = new MailAddress("[email protected]", ""); MailMessage message = new MailMessage(from, to); message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "MY GOD!我的表"; message.Body = @"這是發送郵件的測試,成功!";
MailAddress copy = new MailAddress("[email protected]"); message.CC.Add(copy); SmtpClient client = new SmtpClient(server); //SmtpClient client = new SmtpClient(); client.Credentials = CredentialCache.DefaultNetworkCredentials; //Console.WriteLine("Sending an e-mail message to {0} by using the SMTP host {1}.", // to.Address, client.Host); client.Send(message); }
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="From">发送人邮箱地址</param>
/// <param name="To">接收人邮箱地址</param>
/// <param name="Subject">邮件主题</param>
/// <param name="body">邮件内容</param>
public void SendMail(string From, string To, string Subject, string body)
{
try
{
MailMessage mailMsg = new MailMessage(); //生成电子邮件类
From = "[email protected]";
//To = "[email protected]";
mailMsg.From = new MailAddress(From); //发送人
if (System.Configuration.ConfigurationSettings.AppSettings["TestEmail"] != null)
{
mailMsg.To.Add(new MailAddress("[email protected]"));
}
else
{
mailMsg.To.Add(new MailAddress(To));
}
//mailMsg.To.Add(new MailAddress(To)); //接收人
// mailMsg.To.Add(new MailAddress("[email protected]"));
mailMsg.Subject = Subject; //主题
mailMsg.Body = body; //内容
mailMsg.IsBodyHtml = true; //设置邮件为HTML形式
mailMsg.Priority = MailPriority.High; //设置邮件优先级为高
SmtpClient smtpC = new SmtpClient("localhost"); //成生SMTP传输类,发送邮件
smtpC.UseDefaultCredentials = false;
smtpC.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationSettings.AppSettings["ServiceMail"].ToString(), System.Configuration.ConfigurationSettings.AppSettings["MailPassWord"].ToString());
smtpC.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpC.Send( mailMsg ); //发送
}
catch (System.Exception E)
{
throw (E); //抛出异常
}
}
//page_load事件調用
if (!Page.IsPostBack)
{
CreateCopyMessage("m1");
}
} public static void CreateCopyMessage(string server)//string server
{
MailAddress from = new MailAddres ("[email protected]", "");
MailAddress to = new MailAddress("[email protected]", "");
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "MY GOD!我的表";
message.Body = @"這是發送郵件的測試,成功!";
MailAddress copy = new MailAddress("[email protected]");
message.CC.Add(copy);
SmtpClient client = new SmtpClient(server);
//SmtpClient client = new SmtpClient();
client.Credentials = CredentialCache.DefaultNetworkCredentials;
//Console.WriteLine("Sending an e-mail message to {0} by using the SMTP host {1}.",
// to.Address, client.Host);
client.Send(message);
}