Send email with C# macro

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 284
Благодарностей
2 728
Баллы
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();
 

Вложения

qmg

Client
Регистрация
12.06.2011
Сообщения
15
Благодарностей
7
Баллы
3
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?
 

rostonix

Известная личность
Регистрация
23.12.2011
Сообщения
29 067
Благодарностей
5 705
Баллы
113
1) Test on latest build
2) Test with different email provider
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
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 ?
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 769
Благодарностей
1 179
Баллы
113
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

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
Thanks, must all variables copies and then works fine .
 

kumquat

Client
Регистрация
09.06.2012
Сообщения
10
Благодарностей
0
Баллы
1
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.
 

kumquat

Client
Регистрация
09.06.2012
Сообщения
10
Благодарностей
0
Баллы
1
nevermind, worked on different pc
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 284
Благодарностей
2 728
Баллы
113
Please check you computer name should have only English letters in its' name. In other case this code will not work.
 
  • Спасибо
Реакции: rostonix

Yuri

Client
Регистрация
28.11.2014
Сообщения
129
Благодарностей
24
Баллы
18
Thanks, it works for me
 

Reflixez88

Новичок
Регистрация
14.01.2017
Сообщения
13
Благодарностей
1
Баллы
3
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
 

Yuri

Client
Регистрация
28.11.2014
Сообщения
129
Благодарностей
24
Баллы
18
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 =  [email protected];
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();
 

Reflixez88

Новичок
Регистрация
14.01.2017
Сообщения
13
Благодарностей
1
Баллы
3
I was wondering, how can you specify a "From" header using this method? I would like to do something like this: From: "Name" <[email protected]>

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

Thanks in advance!
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 284
Благодарностей
2 728
Баллы
113
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("[email protected]", "Name");
 
  • Спасибо
Реакции: Anfim777

Reflixez88

Новичок
Регистрация
14.01.2017
Сообщения
13
Благодарностей
1
Баллы
3

Reflixez88

Новичок
Регистрация
14.01.2017
Сообщения
13
Благодарностей
1
Баллы
3
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("[email protected]", "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 [email protected] but want replies to go to [email protected]

Thanks so much in advance for the help!!!
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 284
Благодарностей
2 728
Баллы
113
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("[email protected]", "Tim Jones"));
 
  • Спасибо
Реакции: Anfim777

Gizmond

Client
Регистрация
18.02.2017
Сообщения
263
Благодарностей
51
Баллы
28
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("[email protected]", "Tim Jones"));
Hi. How to make address substitution (from). Sending will be from the address [email protected], and you will see a [email protected]
Will it work or not? Thx for any answer. And sorry for my English :-)
 

Gizmond

Client
Регистрация
18.02.2017
Сообщения
263
Благодарностей
51
Баллы
28
and how add BCC, CC
 

Gizmond

Client
Регистрация
18.02.2017
Сообщения
263
Благодарностей
51
Баллы
28
found a solution
 
Регистрация
27.08.2018
Сообщения
29
Благодарностей
3
Баллы
3
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
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 284
Благодарностей
2 728
Баллы
113
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("[email protected]", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("[email protected]", "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
Регистрация
27.08.2018
Сообщения
29
Благодарностей
3
Баллы
3
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("[email protected]", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("[email protected]", "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 [email protected], [email protected], [email protected] and sends only to the last email
Can I do something?
 
Регистрация
27.08.2018
Сообщения
29
Благодарностей
3
Баллы
3
udp
Made by myself
 

antitrust56

Client
Регистрация
21.08.2012
Сообщения
17
Благодарностей
1
Баллы
3
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
 

dariesto

Client
Регистрация
03.11.2011
Сообщения
124
Благодарностей
9
Баллы
18
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("[email protected]", "some name for BCC");
message.Bcc.Add(bcc);

var cc = new System.Net.Mail.MailAddress("[email protected]", "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?
 

antitrust56

Client
Регистрация
21.08.2012
Сообщения
17
Благодарностей
1
Баллы
3
Hi,

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

Thanks
 
Регистрация
10.07.2017
Сообщения
29
Благодарностей
1
Баллы
3
Work fine. Thanks for this share.
 

srameld

Новичок
Регистрация
05.05.2020
Сообщения
4
Благодарностей
0
Баллы
1
how to send smtp mail via proxy?
 

DoGo

Client
Регистрация
27.11.2020
Сообщения
30
Благодарностей
6
Баллы
8
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!
 

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