一、首先需要配置web.config文件:
<system.net> <mailSettings> <smtp from="Emailname">
<!--这里的host是你本人的邮箱所在服务器,如新浪就会是smtp.sina.com,
userName为你的邮箱名称,password为你的邮箱密码--> <network host="smtp.163.com" userName="Emailname" password="Emailpassword" port="25" defaultCredentials="false"/> </smtp> </mailSettings> </system.net>
二、然后编写发送邮件的函数:
//// <summary> /// 邮件发送方法(带附件) /// </summary> /// <param name="mailto">收件人地址。如:[email protected]</param> /// <param name="mailsubject">邮件标题</param> /// <param name="mailbody">邮件正文</param> /// <param name="mailFrom">邮件发送人地址。如:[email protected]</param> /// <param name="list">附件路径</param> /// <returns></returns> public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list) { try { //邮件发送人地址 System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom); System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如[email protected] System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to); mail.Subject = mailsubject; mail.Body = mailbody; //以下设置服务器 System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient(); //以下为增加附件 int count = list.Count; for (int i = 0; i < count; i++) { System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list.ToString()); mail.Attachments.Add(data); } mySmth.Send(mail); mail.Dispose(); return true; } catch { return false; } }
|
请发表评论