- Регистрация
- 13.01.2009
- Сообщения
- 2 285
- Благодарностей
- 2 730
- Баллы
- 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();
Вложения
-
17,5 КБ Просмотры: 1 068