我在一家公司工作,我被要求编写一些脚本来执行目的地的机器人副本,然后通过电子邮件发送日志,主题是失败或成功。经过相当多的研究,我无法弄清楚为什么我的程序总是中断,而且它也以一种奇怪的方式中断。
该脚本由批处理脚本和用于发送电子邮件的 c# 应用程序组成。
RobocopyScript.cmd
@echo off
set robocopydestination=\[computer]guestTESTFOLDER
set robocopysource=C:Users[username]Script
set robocopylogoutput=log.log
set mailfrom=[email address]
set mailto=[email address]
set successsubject=Robocopy Success
set successbody=Robocopy successfully completed its backup.
set failuresubject=Robocopy Failure
set failurebody=Robocopy failed to complete its backup
set networkcredentialusername=[email address]
set networkcredentialpassword=[password]
set logfile=log.log
robocopy %robocopysource% %robocopydestination% /e /z /tee /log:%robocopylogoutput%
if errorlevel 1 goto success
if errorlevel 0 goto success
goto fail
:fail
EmailProgram.exe %mailfrom% %mailto% %failuresubject% %failurebody% %networkcredentialusername% %networkcredentialpassword% %logfile%
exit
:success
EmailProgram.exe %mailfrom% %mailto% %successsubject% %successbody% %networkcredentialusername% %networkcredentialpassword% %logfile%
exit
电子邮件程序.csx
using System;
using System.Net.Mail;
namespace EmailProgram
{
class MainClass
{
public static void Main(string[] args)
{ // from To Subject Body
MailMessage mail = new MailMessage(args[0], args[1], args[2], args[3]);
SmtpClient client = new SmtpClient();
//set up the smtp client
client.Port = 25;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com"; //ncu ncp
client.Credentials = new System.Net.NetworkCredential(args[4], args[5]);
//add 4 lines of space between the body message and the log information for readability
mail.Body += "rnrnrnrn----------------rnLOG START:rn----------------";
//break up the log file into its lines, then write the lines to the email message body
//log file path
string[] logLines = System.IO.File.ReadAllLines(args[6]);
foreach(string s in logLines)
{
mail.Body += s;
}
//send the email, if the email fails to send it simply will close this application
try
{
client.Send(mail);
}
catch(Exception e)
{
System.Console.WriteLine(e);
}
}
}
}
在运行批处理脚本时,robocopy 成功完成,然后在尝试运行 EmailProgram 时.exe它给我:
未处理的异常:System.IO.FileNotFoundException:找不到文件"C:\Users\[用户名]\Script\CompleteProjects\RobocopyEmail\complete'。
我绝对不是专家,但我在我的代码中看不到调用异常给出的路径的地方。有人可以帮我解决这个问题吗,因为我在试图弄清楚这个问题几个小时后被难住了。谢谢:)
空格successsubject
等。
当你打电话给EmailProgram.exe %mailfrom% %mailto% %successsubject% %successbody% ...
时,你真的发出了电话
EmailProgram.exe apa@bepa.com qux@foo.bar Robocopy Success Robocopy successfully completed its backup...
因此,completed
在电子邮件发送器中变得args[6]
。
尝试
电子邮件程序.exe "%mailfrom%" "%mailto%" "%successsubject%" "%successbody%" ...
相反。