抓取网络文件的URL地址作附件发送
最近公司的一个项目, 把网站的前后台分离到两个不同的服务器上(后台Server在公司内部网,前台Server在互联网了).
前后台用户(分别称呼:客户,公司员工)所有的上传的文件必须要存放在前台的Server上,这样客户才能无障碍访问文件.
那,客户登陆前台提出咨询问题之后,公司员工回复问题解决方法,解决方法包含附件.提交保存后要给前台用户发送一个mail,将回复的内容和附件发送到客户的邮箱.
通常发送带附件的邮件的方式为(使用System.Net.Mail):
首先添加引用:
这里因为附件被上传到了前台Server上,所以取得附件的方法需要改变了.
首先添加引用:
下面代码:
补:
前后台用户(分别称呼:客户,公司员工)所有的上传的文件必须要存放在前台的Server上,这样客户才能无障碍访问文件.
那,客户登陆前台提出咨询问题之后,公司员工回复问题解决方法,解决方法包含附件.提交保存后要给前台用户发送一个mail,将回复的内容和附件发送到客户的邮箱.
通常发送带附件的邮件的方式为(使用System.Net.Mail):
首先添加引用:
using System.Net.Mail;
再编写发送mail代码: 1 SmtpClient SmtpMail = new SmtpClient("127.0.0.1"); //127.0.0.1为发送邮件服务器IP.
2 MailMessage sendMail = new MailMessage();
3 sendMail.Subject = "测试发送带附件的邮件"; //邮件的主题
4 sendMail.From = new MailAddress("sender@mail.com"); //发件人
5 string strToUser = "receiver@mail.com"; //收件人
6 string[] temp;
7 if (strToUser != null && strToUser.Trim() != "")
8 {
9 temp = strToUser.Split();
10 for (int i = 0; i < temp.Length; i++)
11 {
12 sendMail.To.Add(new MailAddress(temp[i]));
13 }
14 }
15 sendMail.Body = "请注意附件."; //邮件内容
16 sendMail.SubjectEncoding = System.Text.Encoding.UTF8; //主题内容编码.
17 sendMail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码.
18 sendMail.IsBodyHtml = true; //邮件内容支持HTML
19
20 string strPath = "E:\\foo.txt"; //要发送附件
21 if (strPath != null && strPath.Length != 0)
22 {
23 try
24 {
25 Attachment ma = new Attachment(strPath);
26 sendMail.Attachments.Add(ma);
27 }
28 catch
29 { }
30 }
31 SmtpMail.Send(sendMail);
2 MailMessage sendMail = new MailMessage();
3 sendMail.Subject = "测试发送带附件的邮件"; //邮件的主题
4 sendMail.From = new MailAddress("sender@mail.com"); //发件人
5 string strToUser = "receiver@mail.com"; //收件人
6 string[] temp;
7 if (strToUser != null && strToUser.Trim() != "")
8 {
9 temp = strToUser.Split();
10 for (int i = 0; i < temp.Length; i++)
11 {
12 sendMail.To.Add(new MailAddress(temp[i]));
13 }
14 }
15 sendMail.Body = "请注意附件."; //邮件内容
16 sendMail.SubjectEncoding = System.Text.Encoding.UTF8; //主题内容编码.
17 sendMail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码.
18 sendMail.IsBodyHtml = true; //邮件内容支持HTML
19
20 string strPath = "E:\\foo.txt"; //要发送附件
21 if (strPath != null && strPath.Length != 0)
22 {
23 try
24 {
25 Attachment ma = new Attachment(strPath);
26 sendMail.Attachments.Add(ma);
27 }
28 catch
29 { }
30 }
31 SmtpMail.Send(sendMail);
这里因为附件被上传到了前台Server上,所以取得附件的方法需要改变了.
首先添加引用:
1using System.Net.Mail;
2using System.Net;
3using System.Net.Mime;
4using System.IO;
2using System.Net;
3using System.Net.Mime;
4using System.IO;
下面代码:
1 SmtpClient SmtpMail = new SmtpClient("10.98.0.50"); //10.98.0.50为发送邮件服务器IP.
2 MailMessage sendMail = new MailMessage();
3 sendMail.Subject = "测试发送带附件的邮件"; //邮件的主题
4 sendMail.From = new MailAddress("Sender@mail.com"); //发件人
5 string strToUser = "Receiver@mail.com"; //收件人
6 string[] temp;
7 if (strToUser != null && strToUser.Trim() != "")
8 {
9 temp = strToUser.Split();
10 for (int i = 0; i < temp.Length; i++)
11 {
12 sendMail.To.Add(new MailAddress(temp[i])); //CC.Add() 添加抄送,Bcc.Add() 添加暗送
13 }
14 }
15 sendMail.Body = "请注意附件."; //邮件内容
16 sendMail.SubjectEncoding = System.Text.Encoding.UTF8; //主题内容编码.
17 sendMail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码.
18 sendMail.IsBodyHtml = true; //邮件内容支持HTML
19
20 string strPath = "http://10.98.0.18/fore/UploadFiles/File/128593509120029362699.doc"; //要发送附件的网址
21 string strFileName = strPath.Substring(strPath.LastIndexOf("/") + 1); ; //文件名
22
23 WebClient wc = new WebClient();
24 MemoryStream m = new MemoryStream();
25
26 if (strFileName != "" && strFileName != null)
27 {
28 try
29 {
30 byte[] fs = wc.DownloadData(strPath);
31 m = new MemoryStream(fs); //将文件抓取下来,存于内存中.
32
33 Attachment ma = new Attachment(m, strFileName);
34 sendMail.Attachments.Add(ma);
35 fs = null;
36 }
37 catch
38 { }
39 }
40
41 SmtpMail.Send(sendMail);
42 sendMail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定
43 m.Dispose();
44 wc.Dispose();
2 MailMessage sendMail = new MailMessage();
3 sendMail.Subject = "测试发送带附件的邮件"; //邮件的主题
4 sendMail.From = new MailAddress("Sender@mail.com"); //发件人
5 string strToUser = "Receiver@mail.com"; //收件人
6 string[] temp;
7 if (strToUser != null && strToUser.Trim() != "")
8 {
9 temp = strToUser.Split();
10 for (int i = 0; i < temp.Length; i++)
11 {
12 sendMail.To.Add(new MailAddress(temp[i])); //CC.Add() 添加抄送,Bcc.Add() 添加暗送
13 }
14 }
15 sendMail.Body = "请注意附件."; //邮件内容
16 sendMail.SubjectEncoding = System.Text.Encoding.UTF8; //主题内容编码.
17 sendMail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码.
18 sendMail.IsBodyHtml = true; //邮件内容支持HTML
19
20 string strPath = "http://10.98.0.18/fore/UploadFiles/File/128593509120029362699.doc"; //要发送附件的网址
21 string strFileName = strPath.Substring(strPath.LastIndexOf("/") + 1); ; //文件名
22
23 WebClient wc = new WebClient();
24 MemoryStream m = new MemoryStream();
25
26 if (strFileName != "" && strFileName != null)
27 {
28 try
29 {
30 byte[] fs = wc.DownloadData(strPath);
31 m = new MemoryStream(fs); //将文件抓取下来,存于内存中.
32
33 Attachment ma = new Attachment(m, strFileName);
34 sendMail.Attachments.Add(ma);
35 fs = null;
36 }
37 catch
38 { }
39 }
40
41 SmtpMail.Send(sendMail);
42 sendMail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定
43 m.Dispose();
44 wc.Dispose();
补:
SmtpClient 请确保发送服务器地址正确,否则可能出现“由于目标机器积极拒绝,无法连接。”的错误;同时请确保端口正确,否则可能出现“远程主机强迫关闭了一个现有的连接”的错误。
现在的 SMTP 服务器,几乎都要求验证,通过 NetworkCredential 来发送用户名和密码。
SmtpClient smtp = new SmtpClient("SMTP 服务器地址");
smtp.Credentials = new NetworkCredential("登录名", "密码"); //SMTP 验证
smtp.Credentials = new NetworkCredential("登录名", "密码"); //SMTP 验证