0

Ok so I am tasked with setting up a submit action class to send an email. I setup everything like below and it gets all the way through till the send email and then throws an error

"Unable to read data from the transport connection: net_io_connectionclosed"

I did some research and it says I need to use port 587 which I am as you can see below. However I havent added anything to the webconfig or the sitecore.config to set SMTP up. Is there something else needed before an email can be sent?

        MailMessage mail = new MailMessage();
        mail.To.Add("mjones@myemail.com");
        mail.From = new MailAddress("mjones@myemail.com");
        mail.Subject = "We hit the code";
        string Body = "Nothing in the body as of yet!";
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;
        smtp.Send(mail);

Thanks in advance for any help.

2
0

Create a patch config and add the following setting and update the values

 <!--  MAIL SERVER
SMTP server used for sending mails by the Sitecore server
Is used by MainUtil.SendMail()
Default value: ""
-->
<setting name="MailServer" value="#{MailServer}#" patch:instead="setting[@name='MailServer']"/>

<!--  MAIL SERVER PORT
If the SMTP server requires a custom port number, enter the value in this setting.
The default value is: 25
-->
<setting name="MailServerPort" value="#{MailServerPort}#" patch:instead="setting[@name='MailServerPort']"/>

<!--  MAIL SERVER USER
If the SMTP server requires login, enter the user name in this setting
-->
<setting name="MailServerUserName" value="#{MailServerUserName}#" patch:instead="setting[@name='MailServerUserName']"/>
<setting name="MailServerPassword" value="#{MailServerPassword}#" patch:instead="setting[@name='MailServerPassword']"/>

once you set this patch config, do the following in your send email method

var mailMessage = new MailMessage
    {
        From = new MailAddress(fromEmail),
        Subject = "Subject",
        Body = message,
        IsBodyHtml = isHtml
    };
    
MainUtil.SendMail(mailMessage);

MailUtil is a library by Sitecore which uses the config settings to send email. you can use this method in your end email action.

by using the MainUtil, you can remove the code

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Send(mail);

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.