Send email with C# macro

Discussion in 'Snippets' started by darkdiver, Jul 25, 2013.

  1. darkdiver

    darkdiver Administrator Staff Member

    Joined:
    Jan 13, 2009
    Messages:
    2,258
    Likes Received:
    2,618
    Sometimes you need send an email report and attach file with results, it is possible with this c# macro
    Code (javascript):
    1.  
    2. // sender email
    3. var fromEmailString = project.Variables["fromEmail"].Value;
    4. // recipient
    5. var toEmailString = project.Variables["toEmail"].Value;
    6. // email login for authorization
    7. var login = project.Variables["login"].Value;
    8. // email password of the sender
    9. var password = project.Variables["password"].Value;
    10. // email server
    11. var server = project.Variables["server"].Value;
    12. // email port
    13. int port;
    14. int.TryParse(project.Variables["port"].Value, out port);
    15. // enable SSL or not (for instance required for Gmail)
    16. bool encryptConnection;
    17. bool.TryParse(project.Variables["encryptedConnection"].Value, out encryptConnection);
    18. // message
    19. var messageText = project.Variables["messageBody"].Value;
    20. // title
    21. var messageSubject = project.Variables["messageSubject"].Value;
    22. // attachment path, leave it empty in case you want to attach nothing
    23. var fileToAttach = project.Variables["fileToAttach"].Value;
    24.  
    25. // format email headers
    26. var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, fromEmailString);
    27. var toAddress = new System.Net.Mail.MailAddress(toEmailString, toEmailString);
    28. // create connecting to the server
    29. var smtp = new System.Net.Mail.SmtpClient {
    30.                             Host = server,
    31.                             Port = port,
    32.                             EnableSsl = encryptConnection,
    33.                             DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    34.                             UseDefaultCredentials = false,
    35.                             Credentials = new System.Net.NetworkCredential(login, password)
    36.                         };
    37. // create a message
    38. var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
    39.                                         Subject = messageSubject,
    40.                                         Body = messageText,
    41.                                         IsBodyHtml = false,
    42.                                     };
    43. // if there is attachment we add it
    44. if (!string.IsNullOrEmpty(fileToAttach))
    45. {
    46.     var attach = new System.Net.Mail.Attachment(fileToAttach);
    47.     message.Attachments.Add(attach);
    48. }
    49. smtp.Send(message);
    50. message.Dispose();
    51.  
     

    Attached Files:

    popobawa, lupo, qmg and 2 others like this.
  2. qmg

    qmg Client

    Joined:
    Jun 12, 2011
    Messages:
    16
    Likes Received:
    7
    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?
     
  3. rostonix

    rostonix Administrator Staff Member

    Joined:
    Dec 23, 2011
    Messages:
    29,050
    Likes Received:
    5,576
    1) Test on latest build
    2) Test with different email provider
     
  4. aleksa77

    aleksa77 Client

    Joined:
    Sep 30, 2011
    Messages:
    616
    Likes Received:
    42
    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 ?
     
  5. lokiys

    lokiys Moderator

    Joined:
    Feb 1, 2012
    Messages:
    3,610
    Likes Received:
    812
    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 likes this.
  6. aleksa77

    aleksa77 Client

    Joined:
    Sep 30, 2011
    Messages:
    616
    Likes Received:
    42
    Thanks, must all variables copies and then works fine .
     
  7. kumquat

    kumquat Client

    Joined:
    Jun 9, 2012
    Messages:
    10
    Likes Received:
    0
    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.
     
  8. kumquat

    kumquat Client

    Joined:
    Jun 9, 2012
    Messages:
    10
    Likes Received:
    0
    nevermind, worked on different pc
     
  9. darkdiver

    darkdiver Administrator Staff Member

    Joined:
    Jan 13, 2009
    Messages:
    2,258
    Likes Received:
    2,618
    Please check you computer name should have only English letters in its' name. In other case this code will not work.
     
    rostonix likes this.
  10. Yuri

    Yuri Client

    Joined:
    Nov 28, 2014
    Messages:
    129
    Likes Received:
    24
    Thanks, it works for me
     
  11. Reflixez88

    Reflixez88 Новичок

    Joined:
    Jan 14, 2017
    Messages:
    13
    Likes Received:
    1
    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
     
  12. Yuri

    Yuri Client

    Joined:
    Nov 28, 2014
    Messages:
    129
    Likes Received:
    24
    This code wroks for me
    Code (text):
    1. string TitleText = "";
    2. string MessageText = "";
    3.  
    4. // Отправляем почту
    5. var fromEmailString = project.Variables["fromEmail"].Value;
    6. var toEmailString =  [email protected];
    7. var login = project.Variables["login"].Value;
    8. var password = project.Variables["password"].Value;
    9. var server = project.Variables["server"].Value;
    10. int port;
    11. int.TryParse(project.Variables["port"].Value, out port);
    12.  
    13. bool encryptConnection;
    14. bool.TryParse(project.Variables["encryptedConnection"].Value, out encryptConnection);
    15.  
    16. var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
    17. var toAddress = new System.Net.Mail.MailAddress(toEmailString, toEmailString);
    18.  
    19. // create connecting to the server
    20. var smtp = new System.Net.Mail.SmtpClient {
    21.                                Host = server,
    22.                                Port = port,
    23.                                EnableSsl = encryptConnection,
    24.                                DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    25.                                UseDefaultCredentials = false,
    26.                                Credentials = new System.Net.NetworkCredential(login, password)
    27. };
    28.  
    29. // create a message
    30. var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
    31.                                            Subject = TitleText,
    32.                                            Body = MessageText,
    33.                                            IsBodyHtml = false,
    34.                                        };
    35.  
    36. smtp.Send(message);
    37. message.Dispose();
     
  13. Reflixez88

    Reflixez88 Новичок

    Joined:
    Jan 14, 2017
    Messages:
    13
    Likes Received:
    1
    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!
     
  14. darkdiver

    darkdiver Administrator Staff Member

    Joined:
    Jan 13, 2009
    Messages:
    2,258
    Likes Received:
    2,618
    look at the previous message
    Code (c#):
    1. var fromAddress = new System.Net.Mail.MailAddress(fromEmailString, "Alex");
    for you case it will be
    Code (c#):
    1. var fromAddress = new System.Net.Mail.MailAddress("[email protected]", "Name");
     
  15. Reflixez88

    Reflixez88 Новичок

    Joined:
    Jan 14, 2017
    Messages:
    13
    Likes Received:
    1
    Thanks so much!!! Got it to work :-)
     
  16. Reflixez88

    Reflixez88 Новичок

    Joined:
    Jan 14, 2017
    Messages:
    13
    Likes Received:
    1
    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!!!
     
  17. darkdiver

    darkdiver Administrator Staff Member

    Joined:
    Jan 13, 2009
    Messages:
    2,258
    Likes Received:
    2,618
    you should use ReplyToList message property
    Code (csharp):
    1. // create a message
    2. var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
    3.                                            Subject = TitleText,
    4.                                            Body = MessageText,
    5.                                            IsBodyHtml = false,
    6.                                        };
    7. message.ReplyToList.Add(new System.Net.Mail.MailAddress("[email protected]", "Tim Jones"));
     
  18. Gizmond

    Gizmond Client

    Joined:
    Feb 18, 2017
    Messages:
    133
    Likes Received:
    16
    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 :-)
     
  19. Gizmond

    Gizmond Client

    Joined:
    Feb 18, 2017
    Messages:
    133
    Likes Received:
    16
    and how add BCC, CC
     
  20. Gizmond

    Gizmond Client

    Joined:
    Feb 18, 2017
    Messages:
    133
    Likes Received:
    16
    found a solution
     
  21. VladislavNikishin

    VladislavNikishin Новичок

    Joined:
    Aug 27, 2018
    Messages:
    9
    Likes Received:
    2
    and how add BCC and CC
     
  22. darkdiver

    darkdiver Administrator Staff Member

    Joined:
    Jan 13, 2009
    Messages:
    2,258
    Likes Received:
    2,618
    Something like this

    Code (C#):
    1. // create a message
    2. var message = new System.Net.Mail.MailMessage(fromAddress, toAddress) {
    3.                                         Subject = messageSubject,
    4.                                         Body = messageText,
    5.                                         IsBodyHtml = false,
    6.                                     };
    7.  
    8. var bcc = new System.Net.Mail.MailAddress("[email protected]", "some name for BCC");
    9. message.Bcc.Add(bcc);
    10.  
    11. var cc = new System.Net.Mail.MailAddress("[email protected]", "some name for CC");
    12. message.CC.Add(cc);
    13.  
    14. // if there is attachment we add it
    15. if (!string.IsNullOrEmpty(fileToAttach))
    16. {
    17.     var attach = new System.Net.Mail.Attachment(fileToAttach);
    18.     message.Attachments.Add(attach);
    19. }
     
    VladislavNikishin likes this.
  23. VladislavNikishin

    VladislavNikishin Новичок

    Joined:
    Aug 27, 2018
    Messages:
    9
    Likes Received:
    2
    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?
     
  24. VladislavNikishin

    VladislavNikishin Новичок

    Joined:
    Aug 27, 2018
    Messages:
    9
    Likes Received:
    2
    udp
    Made by myself
     
  25. antitrust56

    antitrust56 Client

    Joined:
    Aug 21, 2012
    Messages:
    17
    Likes Received:
    1
    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
     
  26. dariesto

    dariesto Client

    Joined:
    Nov 3, 2011
    Messages:
    115
    Likes Received:
    8
    how about multiple CC or BCC adresses?
     
  27. antitrust56

    antitrust56 Client

    Joined:
    Aug 21, 2012
    Messages:
    17
    Likes Received:
    1
    Hi,

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

    Thanks
     

Пользователи просматривающие тему (Пользователей: 0, Гостей: 0)