62,068
社区成员
发帖
与我相关
我的任务
分享
//StrTo:收件人邮箱地址,toDisplayName:收件人名,strFrom:发件人邮箱地址, fromDisplayName:发件人名
//StrBody:郵件內容,strSubject:郵件主題,host:smtp主机名或者ip地址,count:登陆邮箱的用户名
//password:登陆邮箱的密码,path:附件文件的路径
public bool SendEmail_f(string StrTo, string toDisplayName, string strFrom, string fromDisplayName, string StrBody, string strSubject, string host,string count,string password,string path)
{
MailAddress fromEmail = new MailAddress(strFrom, fromDisplayName);
MailAddress toEmail = new MailAddress(StrTo, toDisplayName);
MailMessage mail = new MailMessage(fromEmail, toEmail);
mail.Body = StrBody;
mail.Subject = strSubject;
mail.IsBodyHtml = true;//html方式发送
mail.BodyEncoding = Encoding.UTF8;//编码方式
mail.Priority = System.Net.Mail.MailPriority.Normal;//优先权为正常
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = host;
//smtpClient.Host = "smtp.qq.com";
smtpClient.Port = 25;//发邮件的端口,smtp
smtpClient.UseDefaultCredentials = false;//凭证设为无
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//投递方式网网络
smtpClient.Credentials = new System.Net.NetworkCredential(count, password);//你的用户名和密码
//path为附件路径
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)
{
Response.Write(e.Message + " " + e.InnerException);
Response.End();
return false;
}
return true;
}