-1

Been searching the Forums and while this question has been asked and answered my specific problem isn't among the fixes sadly.

I am working in a SCADA system so System.Web.Mail. commands are all I have available to use.

' are remarked out lines that I have tried already.

Here is what I have the mail sends fine but no attachment;

Dim Day as System.dateTime;
Dim sDay as String;
'Dim attachment as System.Web.Mail.MailAttachment;
'Dim attachment as string;

Day = new system.dateTime();
Day = now();
Day = system.dateTime.Parse(Day).AddDays(-1);
sDay = Day.day;

System.Web.Mail.SmtpMail.SmtpServer = "smtp.mdu.com";
System.Web.Mail.MailAttachment("G:\Test\" + me.IGCFilename + sday + ".txt");

'System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment("G:\Test\" + 
 me.IGCFilename + sday + ".txt");

System.Web.Mail.SmtpMail.Send
(
{from: } "DoNotReply@cngc.com;",
{to: } me.EmailTo,
{subject: } "Daily Gate Volumes sent to Williams",
{body: } "Gas Day Volumes"
);

me.EmailFileNow = false;
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
BrianS
  • 1
  • 2
  • Where are you adding the attachment to the email? – devlin carnate Sep 18 '19 at 20:34
  • Possible duplicate of [Adding an attachment to email using C#](https://stackoverflow.com/questions/5034503/adding-an-attachment-to-email-using-c-sharp) – devlin carnate Sep 18 '19 at 20:34
  • I believe the second system.web,,, should be adding attachment. – BrianS Sep 18 '19 at 20:38
  • I did try that post but it is using system.net.mail and my SCADA system did not uderstand it, – BrianS Sep 18 '19 at 20:39
  • Why is this question tagged with C#? – René Sep 18 '19 at 20:41
  • You appear to have Visual Basic syntax, not C#. Also you should not be on the mail library but rather the message. You should have a `MailMessage` that you are using to define your from, to, subject, body, and additional content. – Greg Sep 18 '19 at 20:44
  • As far as the programming language I was always told it was a hybrid of C# and VB if that is not correct I do apologize. The majority of the code we right is for control systems so mostly if then else statements. – BrianS Sep 18 '19 at 21:02
  • @Greg I do apologize I'm not a coder so I'm not sure what you mean by mail library versus message. – BrianS Sep 18 '19 at 21:06
  • It means that you should be creating a message using the mail library. If you look at the example in the question in the link I posted above, you will see how your code should look (if you're writing c#, and if you're not, you can easily use an online translator to convert it to VB). Also, it's possible that you have both VB and c# scripts implemented in your system, but it won't be in the same code block. – devlin carnate Sep 18 '19 at 21:40
  • Why is this question tagged VBScript? – user692942 Sep 18 '19 at 22:12
  • @devlin carnate Thank you for the response but again my system is a dumb scada system that does not understand commands like var, const, System.Net.Mail, that are in the linked post. I have tried modifying the linked post to make it work I have tried running it through only converter tools only to end up with multiple errors. The code I provided is what I could figure out using trial and error and it does work as far as sending the email, just not the attachment. I do not know maybe it cannot send attachments but one of the functions it says it has available is System.Web.Mail.MailAttachment. – BrianS Sep 18 '19 at 23:02
  • If your doing this in PowerShell lookup how to do mail in PowerShell with C#. – Greg Sep 19 '19 at 03:41
  • I have a hunch this is really what you want, PowerShell. https://stackoverflow.com/questions/3997303/how-to-attach-a-file-to-an-email-with-powershell – Greg Sep 19 '19 at 19:18

1 Answers1

0

Looks like the code is using the Send(String, String, String, String) method which sends a from, to, subject, body message.

To add a full email with attachments and all try:

Dim Day as System.dateTime;
Dim sDay as String;

Day = new system.dateTime();
Day = now();
Day = system.dateTime.Parse(Day).AddDays(-1);
sDay = Day.day;

MailAttachment att = System.Web.Mail.MailAttachment("G:\Test\" + me.IGCFilename + sday + ".txt");

emailMessage = new MailMessage();
emailMessage.From = "DoNotReply@cngc.com;";
emailMessage.To = "me.EmailTo";
emailMessage.Subject = "Daily Gate Volumes sent to Williams";
emailMessage.Body = "Gas Day Volumes";
emailMessage.Attachments.Add(att);

System.Web.Mail.SmtpMail.SmtpServer = "smtp.mdu.com";
System.Web.Mail.SmtpMail.Send(emailMessage);

me.EmailFileNow = false;

Without having a vb SCADA system I can't test it but the above code is .net SmtpMail code.

  • Thank you for that. I am attempting to get it to work but it is currently throwing an error during check that says first att is not expected. – BrianS Sep 19 '19 at 16:59
  • Can you post the full error? It is easier to understand what is going wrong with the actual error info. – ScottJamesSDF Sep 19 '19 at 22:39
  • With out any further feedback from you @BrianS we cannot help you solve this problem. If my response helped you solve your issue please mark it as the accepted answer. – ScottJamesSDF Sep 23 '19 at 13:07
  • It is still not working. As far as errors they are simply What I posted att not expected. in the code you provided. The errors are not robust enough to work out the issue so I have given up for now as we are in the middle of a SCADA upgrade and I'm focusing on that. This will be needed in the new system but for now it's low priority. I have tried Dim att as a string as a system variable as an attachment variable all with no success. – BrianS Sep 24 '19 at 16:08