C#邮件发送开发经本人测试通过
先准备以下工作
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 = "邮件格式不正确";
}
下载文件地址密码需要的留言
地址
https://files.cnblogs.com/files/RebornC/MyDll3.zip