发送电子邮件
public class SimpleEmailHelper
{
private string _SmtpAdd;
private string _UserID;
private string _UserPsw;
public SimpleEmailHelper(string smtpAddress, string userID, string userPsw)
{
_SmtpAdd = smtpAddress;
_UserID = userID;
_UserPsw = userPsw;
}
public bool Send(string from, string to, string subject, string message,string cc)
{
return Send(from, from, to, to, subject, message,cc);
}
public bool Send(string from, string fromDisplay, string sendTo, string sendToDisplay,string subject, string message,string cc)
{
bool ret = true;
SmtpClient client = new SmtpClient();
client.Host = _SmtpAdd;//邮件服务器 比如 网易的是 smtp.163.COM
client.Port = 25;//端口号,也可不写
client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式
client.Credentials = new NetworkCredential(_UserID, _UserPsw);//用户名和密码
MailMessage myMessage = new MailMessage();
myMessage.Priority = MailPriority.Normal;//优先级
myMessage.From = new MailAddress(from, fromDisplay, Encoding.GetEncoding("gb2312"));
myMessage.To.Add(sendTo);
if (cc != "")
{
myMessage.CC.Add(cc);
}
myMessage.Subject = subject;//邮件主题
myMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
myMessage.IsBodyHtml = true;
myMessage.BodyEncoding = Encoding.GetEncoding("gb2312");
myMessage.Body = message;//正文
myMessage.Attachments.Add(new Attachment(@"C:\Users\lando\Desktop\Flex问题集结号.txt"));//加入附件。。。
client.Send(myMessage);//开始发送。
return ret;
}
}
页面调用:
SQ.FrameWork.SimpleEmailHelper emailHelper = new SQ.FrameWork.SimpleEmailHelper(stmpServerIpAddress, userId, psw); emailHelper.Send(from, distEmailAddress, TextBoxTopic.Text.Trim(),TextBoxContent.Text.Trim(),txtCCCleint.Text); ShowMessage("邮件发送成功。");