电子邮件程序问题

Email Program issues

我在一家公司工作,我被要求编写一些脚本来对目标进行自动复制,然后通过电子邮件发送主题为失败或成功的日志。经过大量研究后,我无法弄清楚为什么我的程序总是出错,而且它也以一种奇怪的方式中断。

该脚本由一个批处理脚本和一个用于发送电子邮件的 c# 应用程序组成。

RobocopyScript.cmd

@echo off


set robocopydestination=\[computer]\guest\TESTFOLDER\
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

EmailProgram.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 += "\r\n\r\n\r\n\r\n----------------\r\nLOG START:\r\n----------------";

            //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\[username]\Script\CompleteProjects\RobocopyEmail\completed'。

我绝对不是专家,但我在我的代码中没有看到任何地方调用异常给出的路径。有人可以帮我解决这个问题,因为我花了几个小时试图解决这个问题后被难住了。谢谢:)

变量中有空格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]

尝试

EmailProgram.exe "%mailfrom%" "%mailto%" "%successsubject%" "%successbody%" ...

相反。