namespace Mail
{
public static class mail
{
public static void GetparaBySendmail(string mailaddress,string title,string file ,string content,string fromname,string pwd)
{
MailMessage mails = new MailMessage(); //创建邮件类
string fileone;
mails.From = new MailAddress(fromname, fromname, System.Text.Encoding.UTF8);//说明指定发件人的信息 收件的地址
mails.Subject = title;
fileone=file;
if (file!=String.Empty) //判断文件是否为空
{
AlternateView avfile = new AlternateView(fileone);
mails.AlternateViews.Add(avfile);
}
mails.BodyEncoding = System.Text.Encoding.UTF8;//文件编码
mails.IsBodyHtml = true;//是否为HTML格式
mails.Body = content; //发送的内容
mails.Priority = MailPriority.High;//优先级
SmtpClient smtp = new SmtpClient(); //发送邮件的协议
smtp.UseDefaultCredentials=true;
smtp.Credentials = new System.Net.NetworkCredential(fromname, pwd);//发送人的邮箱
string host = fromname.Substring(fromname.LastIndexOf("@")+1);
smtp.Host ="smtp."+ host;
string[] maillist= mailaddress.Split(';');
for (int i = 0; i < maillist.Length; i++)
{
mails.To.Add(maillist[i]);//收件人的邮箱
}
try
{
smtp.EnableSsl = true;//ssl加密
//smtp.Timeout = 1200;
smtp.Send(mails);
HttpContext.Current.Response.Write(" <script>alert('发送成功'); </script>");
}
catch (Exception e)
{
HttpContext.Current.Response.Write(" <script>alert('"+e.Message+"'); </script>");
}
}
}
}