先准備以下工作
1.先開通郵箱我以QQ郵箱為例
2.開通
POP3/SMTP服務 (
如何使用 Foxmail 等軟件收發郵件?)
已開啟 |
關閉
獲取授權碼
3.C#開發了先寫一個CS文件
public class Email
{
public string MailSend(string mialaddress,string title, string mailReceive, string content)
{
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("發送者郵箱", "發送者名稱");
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.Subject = title;//標題
mail.To.Add(mialaddress);//收地址
mail.Body = content;//內容
mail.From = from;
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Host = "smtp.qq.com";//郵箱SMtp地址QQ
client.Credentials = new System.Net.NetworkCredential("發送者郵箱地址", "授權碼");
try
{
client.Send(mail);//發送
return "成功發送";
}
catch (Exception err)
{
return "發送失敗"+err.Message.ToString();
}
}
}
4.在調用這個CS
if (Regex.IsMatch(mialaddress.Text, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase) == true)//正則表達式郵箱的格式判斷
{
Email email = new Email();//類的實例化
string XX = email.MailSend(mialaddress.Text, title.Text, mailReceive.Text, content.Text);//參數的傳遞
Response.Write("<script language = javascript>alert('"+XX+"');</script>");
}
else
{
Label5.Visible=true;
Label5.Text = "郵件格式不正確";
}
下載文件地址密碼需要的留言
地址
http://files.cnblogs.com/files/RebornC/MyDll3.zip
