Send email with C# macro

  • Автор темы Автор темы darkdiver
  • Дата начала Дата начала

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 285
Реакции
2 732
Баллы
113
Sometimes you need send an email report and attach file with results, it is possible with this c# macro
JavaScript:
Развернуть Свернуть Копировать
// sender email
var fromEmailString = project.Variables["fromEmail"].Value;
// recipient
var toEmailString = project.Variables["toEmail"].Value;
// email login for authorization
var login = project.Variables["login"].Value;
// email password of the sender
var password = project.Variables["password"].Value;
// email server
var server = project.Variables["server"].Value;
// email port
int port;
int.TryParse(project.Variables["port"].Value, out port);
// enable SSL or not (for instance required for Gmail)
bool encryptConnection;
bool.TryParse(project.Variables["encryptedConnection"].Value, out encryptConnection);
// message
var messageText = project.Variables["messageBody"].Value;
// title
var messageSubject = project.Variables["messageSubject"].Value;
// attachment path, leave it empty in case you want to attach nothing
var fileToAttach = project.Variables["fileToAttach"].Value;

// format email headers
var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, fromEmailString);
var toAddress = new System.Net.Mail.MailAddress(toEmailString, toEmailString);
// create connecting to the server
var smtp = new System.Net.Mail.SmtpClient {
                            Host = server,
                            Port = port,
                            EnableSsl = encryptConnection,
                            DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new System.Net.NetworkCredential(login, password)
                        };
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                        Subject = messageSubject,
                                        Body = messageText,
                                        IsBodyHtml = false,
                                    };
// if there is attachment we add it
if (!string.IsNullOrEmpty(fileToAttach))
{
    var attach = new System.Net.Mail.Attachment(fileToAttach);
    message.Attachments.Add(attach);
}
smtp.Send(message);
message.Dispose();
 

Вложения

When testing this macro I get the following error: "Exception has been thrown by the target of an invocation."
I am using 5.0.2.
I have tried both secure ports 465 and unsecure ports 26 on my server, which are the recommended settings for my email provider.
Suggestions?
 
1) Test on latest build
2) Test with different email provider
 
I have same error . I try with gmail, ( have version 5.0.7) put for server smtp.gmail.com and for port 587 is this correct ?
 
I have same error . I try with gmail, ( have version 5.0.7) put for server smtp.gmail.com and for port 587 is this correct ?

Please check if you have all necessary variables in your project to get this code to work.
Because this code working just fine. I'm using exactly this project and everything is fine.

Download file attached and try with that, change just email login information there.

Cheers
 
  • Оценить
Реакции: aleksa77
Thanks, must all variables copies and then works fine .
 
Is this still working in current version?

I downloaded the file and tried 3 different mail providers, different ports for each, true/false SSL setting, and could not get it to work.
 
nevermind, worked on different pc
 
Please check you computer name should have only English letters in its' name. In other case this code will not work.
 
  • Оценить
Реакции: rostonix
Thanks, it works for me
 
Does this still work? I'm trying it with Gmail and ports 587 as well as 465 and neither of them work. My computer only has English letters, and it does not return anything in the codeReply variable
 
Does this still work? I'm trying it with Gmail and ports 587 as well as 465 and neither of them work. My computer only has English letters, and it does not return anything in the codeReply variable

This code wroks for me
Код:
Развернуть Свернуть Копировать
string TitleText = "";
string MessageText = "";
 
// Отправляем почту
var fromEmailString = project.Variables["fromEmail"].Value;
var toEmailString =  example@gmail.com;
var login = project.Variables["login"].Value;
var password = project.Variables["password"].Value;
var server = project.Variables["server"].Value;
int port;
int.TryParse(project.Variables["port"].Value, out port);

bool encryptConnection;
bool.TryParse(project.Variables["encryptedConnection"].Value, out encryptConnection);

var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
var toAddress = new System.Net.Mail.MailAddress(toEmailString, toEmailString);

// create connecting to the server
var smtp = new System.Net.Mail.SmtpClient {
                               Host = server,
                               Port = port,
                               EnableSsl = encryptConnection,
                               DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
                               UseDefaultCredentials = false,
                               Credentials = new System.Net.NetworkCredential(login, password)
};

// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                           Subject = TitleText,
                                           Body = MessageText,
                                           IsBodyHtml = false,
                                       };

smtp.Send(message);
message.Dispose();
 
I was wondering, how can you specify a "From" header using this method? I would like to do something like this: From: "Name" <example@gmail.com>

This way the recipient would see my name when they receive the email.

Thanks in advance!
 
look at the previous message
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
for you case it will be
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress("example@gmail.com", "Name");
 
  • Оценить
Реакции: Anfim777
look at the previous message
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
for you case it will be
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress("example@gmail.com", "Name");

Thanks so much!!! Got it to work :)
 
look at the previous message
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
for you case it will be
Код:
Развернуть Свернуть Копировать
var fromAddress = new System.Net.Mail.MailAddress("example@gmail.com", "Name");

Hey darkdiver, I have one more question, how can we specify a "Reply-to" header in this C# Code? For example if I want to send my email from tom_banks@gmail.com but want replies to go to tim_jones@gmail.com

Thanks so much in advance for the help!!!
 
you should use ReplyToList message property
C#:
Развернуть Свернуть Копировать
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                           Subject = TitleText,
                                           Body = MessageText,
                                           IsBodyHtml = false,
                                       };
message.ReplyToList.Add(new System.Net.Mail.MailAddress("tim_jones@gmail.com", "Tim Jones"));
 
  • Оценить
Реакции: Anfim777
you should use ReplyToList message property
C#:
Развернуть Свернуть Копировать
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                           Subject = TitleText,
                                           Body = MessageText,
                                           IsBodyHtml = false,
                                       };
message.ReplyToList.Add(new System.Net.Mail.MailAddress("tim_jones@gmail.com", "Tim Jones"));

Hi. How to make address substitution (from). Sending will be from the address test@gmail.com, and you will see a test111@gmail.com
Will it work or not? Thx for any answer. And sorry for my English :)
 
and how add BCC, CC
 
found a solution
 
Sometimes you need send an email report and attach file with results, it is possible with this c# macro
JavaScript:
Развернуть Свернуть Копировать
// sender email
var fromEmailString = project.Variables["fromEmail"].Value;
// recipient
var toEmailString = project.Variables["toEmail"].Value;
// email login for authorization
var login = project.Variables["login"].Value;
// email password of the sender
var password = project.Variables["password"].Value;
// email server
var server = project.Variables["server"].Value;
// email port
int port;
int.TryParse(project.Variables["port"].Value, out port);
// enable SSL or not (for instance required for Gmail)
bool encryptConnection;
bool.TryParse(project.Variables["encryptedConnection"].Value, out encryptConnection);
// message
var messageText = project.Variables["messageBody"].Value;
// title
var messageSubject = project.Variables["messageSubject"].Value;
// attachment path, leave it empty in case you want to attach nothing
var fileToAttach = project.Variables["fileToAttach"].Value;

// format email headers
var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, fromEmailString);
var toAddress = new System.Net.Mail.MailAddress(toEmailString, toEmailString);
// create connecting to the server
var smtp = new System.Net.Mail.SmtpClient {
                            Host = server,
                            Port = port,
                            EnableSsl = encryptConnection,
                            DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new System.Net.NetworkCredential(login, password)
                        };
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                        Subject = messageSubject,
                                        Body = messageText,
                                        IsBodyHtml = false,
                                    };
// if there is attachment we add it
if (!string.IsNullOrEmpty(fileToAttach))
{
    var attach = new System.Net.Mail.Attachment(fileToAttach);
    message.Attachments.Add(attach);
}
smtp.Send(message);
message.Dispose();
and how add BCC and CC
 
Something like this

Код:
Развернуть Свернуть Копировать
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                        Subject = messageSubject,
                                        Body = messageText,
                                        IsBodyHtml = false,
                                    };

var bcc = new System.Net.Mail.MailAddress("bcc@exmaple.com", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("cc@exmaple.com", "some name for CC");
message.CC.Add(cc);

// if there is attachment we add it
if (!string.IsNullOrEmpty(fileToAttach))
{
    var attach = new System.Net.Mail.Attachment(fileToAttach);
    message.Attachments.Add(attach);
}
 
  • Оценить
Реакции: VladislavNikishin
Something like this

Код:
Развернуть Свернуть Копировать
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                        Subject = messageSubject,
                                        Body = messageText,
                                        IsBodyHtml = false,
                                    };

var bcc = new System.Net.Mail.MailAddress("bcc@exmaple.com", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("cc@exmaple.com", "some name for CC");
message.CC.Add(cc);

// if there is attachment we add it
if (!string.IsNullOrEmpty(fileToAttach))
{
    var attach = new System.Net.Mail.Attachment(fileToAttach);
    message.Attachments.Add(attach);
}
How to do a few mails I write 1@mail.ru, 2@mail.ru, 3@mail.ru and sends only to the last email
Can I do something?
 
Hi,

I have an error since some days when i try to send an e-mail with your C# script.

"Executing action CSharp OwnCode: send email.id: 07020bcd-c6f0-4111-8d37-e30603c1f6e5 The specified string is not in the form required for an email address."

I tried with port 25, default 587.

By changing your e-mail address as well....

Thanks for your help
Jose
 
Something like this

Код:
Развернуть Свернуть Копировать
// create a message
var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
                                        Subject = messageSubject,
                                        Body = messageText,
                                        IsBodyHtml = false,
                                    };

var bcc = new System.Net.Mail.MailAddress("bcc@exmaple.com", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("cc@exmaple.com", "some name for CC");
message.CC.Add(cc);

// if there is attachment we add it
if (!string.IsNullOrEmpty(fileToAttach))
{
    var attach = new System.Net.Mail.Attachment(fileToAttach);
    message.Attachments.Add(attach);
}
how about multiple CC or BCC adresses?
 
Hi,

I don't have an CC or BBC addresses configured.

Thanks
 
how to send smtp mail via proxy?
 
Hello! Could someone please update the project, tried to use it but does not work. I tried to change ports, smtp, mail, but it doesn't work.

P.S. Beginner. Thanks!
 

Кто просматривает тему: (Всего: 0, Пользователи: 0, Гости: 0)