Page 1 of 2

C sharp script help

Posted: 24 Oct 2012, 16:05
by hgoodsir
hi all,

I have been looking on line and found a c script for emailing via gmail a file but not sure how to integrate into scene, so I can email snap shots using the script.

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

Re: C sharp script help

Posted: 31 Oct 2012, 20:51
by Chris
Hi Hashim,

I'm still in the early stage of C# but ive been having a go at this :)

I think i found the post that you referenced your code from and i couldn't get it to work in visual studio, so i picked up another bit of code and had a go with that ;)
I have successfully sent email from visual studio with this code so have transferred it in to Ventuz to do some testing.

after some hours of compiling errors in Ventuz i have now got a code that is returning zero compile errors in the script node (getting there ;) )

But.....and its a big but.....i still haven't been able to send a test email from Ventuz......

So I too am now wondering if anyone can point out where this script is going wrong in order to achieve said task :)
See attached file :)

If theres any prob with attachment, heres the code so far.....
added to Validate()......

inputs added to script node:
strings: YourEmail, SendToEmail, EmailPassword, Subject, EmailBody
Bool: Update

Outputs added to script node:
Strings: Exception, MessageSent

using System.Net;
using System.Net.Mail;
using System.Web;

{
string SendersAddress = this.YourEmail;
string ReceiversAddress = this.SendToEmail;
string SendersPassword = this.EmailPassword;
string subject = this.Subject;
string body = this.EmailBody;

try

{
SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.Gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(SendersAddress, SendersPassword);
smtp.Timeout = 30000;


MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);

smtp.Send(message);
this.MessageSent = ("Message Sent Successfully");

}
catch (Exception ex)
{
this.Exception = (ex.Message);
}

changed = true;

}

hopefully we can get this sorted :) ;)

Cheers
Chris

p.s didn't get attachement working either, which i know is what you want the script for.......so any ideas on that would be awesome :)

Re: C sharp script help

Posted: 31 Oct 2012, 20:53
by Chris
oooops.....forgot file :roll:

Re: C sharp script help

Posted: 01 Nov 2012, 16:36
by Daniel Willer
It works fine for me just paste your code in a method created via the CustomModel.


Cherio!

Re: C sharp script help

Posted: 01 Nov 2012, 20:40
by Chris
It works! excellent! cheers Daniel (got your email ;) )

now i will have a look at the attach file option and come back with a working answer....hopefully ;)


chris

Re: C sharp script help

Posted: 02 Nov 2012, 06:43
by Chris
add this just before .......smtp.send(message);

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@"C:\Users\the file URL goes here");
message.Attachments.Add(attachment);

:)

solved.....:)

Chris

Re: C sharp script help

Posted: 02 Nov 2012, 14:51
by hgoodsir
This is excellent Chris thanks for this :)

Re: C sharp script help

Posted: 02 Nov 2012, 18:38
by joysprod
Nice bit of scripting which as a new C# user I could just about understand.

Does your render stall when sending the email? How can this be avoided?

Regards

Peter

Re: C sharp script help

Posted: 07 Nov 2012, 15:56
by joysprod
Answering my own question...

Simple really, use sendasync to send the mail!

Peter

Re: C sharp script help

Posted: 09 Nov 2012, 00:16
by hgoodsir
cool iv'e tried to understand the sendasync, looking at link
http://msdn.microsoft.com/en-us/library ... async.aspx
but i have no idea how to add to script any help :)