如何用asp.net实现“给qq邮箱发送邮件”?
在标签中添加代码。
配置Web.config,在<configuration>标签中添加以下代码,(aaaaaaaaaa:表示发信人的邮箱号.bbbbbbbbbb:表示前面获取的授权码.端口号port:我测试25能发,而465不能发。
具体代码如下:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="nekorz <1018472917@qq.com>">
<network host="smtp.qq.com" userName="aaaaaaaaaa@qq.com" password="bbbbbbbbbb" port="25" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
添加发送邮件代码:
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Test Message";
myMessage.Body = "这是一封测试邮件.";
myMessage.To.Add(new MailAddress("aaaaaaaaaa@qq.com", "昵称"));
SmtpClient mySmtpClient = new SmtpClient(); try
{
mySmtpClient.Send(myMessage);
} catch (SmtpException ex)
{
}
2024-07-09 广告
前提是你的邮箱必须开通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