【问题标题】:Email Program issues电子邮件程序问题
【发布时间】:2018-12-30 00:02:39
【问题描述】:

我在一家公司工作,我被要求编写一些脚本来对目的地进行 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\[username]\Script\CompleteProjects\RobocopyEmail\completed'。

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

【问题讨论】:

  • 日志文件的路径是什么?
  • 个人 id 放弃 robocopy 并只使用 System.IO,并降低复杂性,但这可能不是您当前的问题
  • 日志文件只是脚本和可执行文件所在的目录,我尝试创建路径 C:\Logs\log.log 以及相同的结果

标签: c# email batch-file robocopy


【解决方案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%" ...

改为。

【讨论】:

  • 好吧,这就像一记耳光。非常感谢,引用了几句,它就像一个魅力。
猜你喜欢
  • 2016-09-22
  • 2019-05-18
  • 2011-08-13
  • 1970-01-01
  • 2012-05-14
  • 2023-03-19
  • 1970-01-01
  • 2018-05-10
  • 2015-12-22
相关资源
最近更新 更多