电子邮件计划问题

时间:2018-12-30 00:02:40

标签: c# email batch-file robocopy

我在一家公司工作,被要求写一些脚本来对目的地进行机器人复制,然后通过电子邮件将主题失败或成功的日志发送给日志。经过大量的研究,我无法弄清为什么我的程序不断崩溃,并且它也以一种奇怪的方式崩溃。

该脚本由一个批处理脚本和一个用于发送电子邮件的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 \ [用户名] \ Script \ CompleteProjects \ RobocopyEmail \ completed'。

我绝对不是专家,但是我在代码中看不到调用异常给出的路径的任何地方。有人可以帮我解决这个问题,因为经过数小时的尝试后我陷入了困境。谢谢:)

1 个答案:

答案 0 :(得分:1)

变量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%” ...

相反。