1.先把 smtp 的發信程式準備好
隨便搜尋一下 C# Csharp 的 程式
http://blog.sina.com.cn/s/blog_46175deb0100az4y.html
mail.cs
================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace TMail1
{
class MainProgram
{
static void Main(string[] args)
{
// 建立MailMessage類實例
MailMessage mail = new MailMessage();
// 收件者
mail.To.Add(new MailAddress("
[email protected]"));
// 發件者
mail.From = new MailAddress("
[email protected]");
// 郵件主題
mail.Subject = "test";
// 郵件內容
mail.Body = "test!";
// 附加檔案 ps:我自己(bx2aa)加上附檔
mail.Attachments.Add(new System.Net.Mail.Attachment("test.rar"));
// 建立SmtpClient類實例
SmtpClient mailer = new SmtpClient();
// 設定Smtp主機
mailer.Host = "smtp.pczone.com.tw";
mailer.Port = 25;
// 設定 SMTP 伺服器 帳號/密碼, 發件者 E-mail, 密碼
mailer.Credentials = new NetworkCredential("account","password");
try
{
// 發信
mailer.Send(mail);
System.Console.WriteLine("發送成功!");
}
catch (Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
}
}
}
================================================
請自行查詢 Framework 版本修改 紅字部份
csc 在 C:\WINDOWS\Microsoft.NET\Framework\v???\
C:\WINDOWS\Microsoft.NET\Framework\v???\csc mail.cs
產成 mail.exe
直接執行 mail 就送出去.
好開始整理出檔案.
2.整理出全部檔案名稱:
ps:需要 UnixUtils 且 PATH 指到 UnixUtils 目錄
list.txt 內容為目錄名稱
======================
用戶資料夾\A\工作報告\
用戶資料夾\B\工作報告\
用戶資料夾\C\工作報告\
用戶資料夾\D\工作報告\
======================
for /F %i in (list.txt) do echo copy %i>> file.txt && dir %i | sort | grep -v "<DIR>" | grep -v " \.\." | grep -v " \." | tail -n1 | cut -c 41- >> file.txt && echo. >>file.txt
ps:把前一天的檔案當成是"日期最新"的檔案, 所以用 sort 排序抓最後一行
把 file.txt 整理一下為 copy pathfilename
sed -e :a -e "$!N;s/ \n \(.*\)/\1 /;ta" -e "P;D" file.txt > sendfile.txt
把 檔案 copy 到 c:\temp
複製到 c:\temp
for /F "tokens=1,* delims= " %i in (sendfile.txt) do %i %j c:\temp
3.把 c:\temp\ 裏的檔案壓縮起來
"C:\Program Files\WinRAR\winrar.exe" a test c:\temp\*.*
執行 mail.exe 就發出去了.
以上花了三四個小時, 確實能正常執行.
PS: 要放到批次檔裡 %那些 %i 要變為 %%i %%j
如果報告名稱固定的話就直接寫死
report.bat
cop c:\用戶資料夾\A\工作報告\report.doc c:\temp\張三.doc
cop c:\用戶資料夾\B\工作報告\report.doc c:\temp\李四.doc
cop c:\用戶資料夾\C\工作報告\report.doc c:\temp\王五.doc
"C:\Program Files\WinRAR\winrar.exe" a test c:\temp\*.*
mail.exe
ps:注意防毒軟體有沒有把 port :25 檔住
有的話會出現
System.Net.Mail.SmtpException: 傳送郵件失敗。 ---> System.Net.WebException: 無法連接至遠端伺服器 ---
> System.Net.Sockets.SocketException: 連線已被您主機上的軟體中止。 ???.???.???.???:25
於 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
於 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
於 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, So
cket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
Exception& exception)
--- 內部例外狀況堆疊追蹤的結尾 ---
於 System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async,
IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
於 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsy
ncDelegate asyncCallback)
於 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
於 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallbac
k, Int32 creationTimeout)
於 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
於 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
於 System.Net.Mail.SmtpClient.GetConnection()
於 System.Net.Mail.SmtpClient.Send(MailMessage message)
--- 內部例外狀況堆疊追蹤的結尾 ---
於 System.Net.Mail.SmtpClient.Send(MailMessage message)
於 TMail1.MainProgram.Main(String[] args)