class SendMail
{
public bool sendMail(string mailFrom, string mailTo, string mailSubject, string smptHost, string userName, string password, bool isHtmlBody, string content)
{
bool flag = true;
try
{
MailMessage mail = new MailMessage();
mail.Subject = mailSubject;
mail.From = new MailAddress(mailFrom);
mail.To.Add(new MailAddress(mailTo));
mail.IsBodyHtml = isHtmlBody;
mail.Body = content;
Attachment attach = new Attachment(@"D:\data\1.txt"); //发送附件
mail.Attachments.Add(attach);
SmtpClient mailClient = new SmtpClient();
mailClient.Host = smptHost;
mailClient.Port = 25;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = new System.Net.NetworkCredential(userName, password);
mailClient.EnableSsl = true;
mailClient.Send(mail);
}
catch (Exception e)
{
flag = false;
e.Message.ToString();
}
return flag;
}
}
调用
bool sent = sm.sendMail("发送的邮箱", "接收邮箱", ".Net Sent Mail.", "邮件服务器IP", "用户名(不需要@后面的部分)", "密码", true, "this mail is sent by .Net Program");