Send Email from Customization

Good morning group!

I created a Subform that basically asks a Yes or No question (i.e. do you want to send an email), if the user clicks Yes I need to send it. I know that I can have my customization trigger a transaction that will then trigger a BPM to send the email, but my question is: can’t I just put the “send email” code in the Customization Script instead? Has anyone done something like this? I’m struggling to identify the assemblies I need.

If using Outlook, see Microsoft.Office.Interop.Outlook

Mark W.

You’ll need to add a reference to ICE (I think it’s Ice.Core? That or Ice.Framework), but then you should be able to just go:

using Ice.Core;
using Ice.Lib.Framework;
using Ice.Lib.Customization;

var mailer = this.GetMailer(async:true);
var message = new Ice.Mail.SmtpMail();

message.SetFrom("example@example.com");
message.SetTo("example@example.com");
message.SetCC("example@example.com");
message.SetSubject("Subject");
message.SetBody("Email Body Text");

message.IsBodyHtml = true;
mailer.Send(message);

That will allow also you to add HTML formatting so your alerts look more professional.

1 Like

You sure can, but BPM is much easier

I can share some office 365 custom mailer code too if that would help.

For some reason you dont have your epicor mailer set up, you can do the old fashioned way:

NetworkCredential basicCredential = 
    new NetworkCredential("uremail@domain.com", urpassword); 
MailAddress fromAddress = new MailAddress("WhoIsSender@domain.com"); 
smtpClient.Host = "mail.yourSMTP.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.EnableSsl = true;
MailMessage message = new MailMessage();
//-------> THIS IS WHAT YOU WANT!  ---->message.Attachments.Add(new Attachment(PathToAttachment));
message.From = fromAddress;
message.Subject = "Testing customiztion from E10";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>This was created from within EPICOR..</h1><BR>On the receipt entry screen";
message.To.Add("receipient1@domain.com"); 
try
{
    smtpClient.Send(message);
}
catch(Exception ex)
{
    //Error, could not send the message
 //   Response.Write(ex.Message);
MessageBox.Show(ex.Message);
}

2 Likes

That is what I use, works as expected.

using System.Text;
using System.Net.Mail;

SmtpClient client = new SmtpClient(“your mail server”);
MailMessage mess = new MailMessage(from, To);
mess.IsBodyHtml = true;
mess.Subject = Subject;
sb.AppendLine();
sb.AppendLine(Body);
sb.AppendLine();

    sb.AppendLine();sb.AppendLine();

   
    mess.Body = sb.ToString();
    mess.Priority = MailPriority.High;

    client.Send(mess);

Pierre

Thanks for the input fellas! I’ll digest everything and choose which direction I’ll go. I appreciate all the help.

Have a good one.

I mixed up your question with another. The other solutions are cleaner. The only time you’d want to use the Outlook version is if you want to track the email in your Sent folder.

Mark W.

Quick update. I just went the BPM route. Had customization trigger it using CallContextBPMData

Hello Aaron,
Would you have an example using the office 365 mailer code ?
Thanks in advance,

Regards,
John

1 Like

Sure thing:

SmtpClient client = new SmtpClient("smtp.office365.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("YourSendingEmailAddress@domain.com", "yourPassword", "domain.com");//this is the account you'll send from
client.Port = 587;
client.EnableSsl = true;

MailAddress from = new MailAddress("fromemail@domain.com");//If you enable send-as on the sending account, you can make the "from" dynamic
MailAddress to = new MailAddress("toemail@domain.com"); 
MailAddress cc = new MailAddress("ccemail@domain.com");

MailMessage message = new MailMessage(from, to);
message.CC.Add(cc);

message.Body = txtEmailMsg.Text.ToString();
message.Subject = "Subject Goes Here";
client.SendAsync(message, userState);

Thanks Aaron, I’ll give this a try.

Cheers, John