Sitecore.MainUtil.SendMail(System.Net.Mail.MailMessage message)
This method is using the System.Net.Mail.SmtpClient to send the email which what most of us will actually implement on custom code. So why not using it... Here is the Method:
////// Sends the mail. /// /// The message. public static void SendMail(System.Net.Mail.MailMessage message) { string mailServer = Settings.MailServer; System.Net.Mail.SmtpClient smtpClient; if (string.IsNullOrEmpty(mailServer)) { smtpClient = new System.Net.Mail.SmtpClient(); } else { int mailServerPort = Settings.MailServerPort; if (mailServerPort > 0) { smtpClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort); } else { smtpClient = new System.Net.Mail.SmtpClient(mailServer); } } string mailServerUserName = Settings.MailServerUserName; if (mailServerUserName.Length > 0) { string mailServerPassword = Settings.MailServerPassword; System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(mailServerUserName, mailServerPassword); smtpClient.Credentials = credentials; } smtpClient.Send(message); }
So you should be able to use it straight away as per the following:
var myMessage = new MailMessage(from, to, subject, body); Sitecore.MainUtil.SendMail(myMessage);
RE-EDITED the post: obviously you need to setup your SMTP in the web.config for this to work:
< !-- MAIL SERVER SMTP server used for sending mails by the Sitecore server Is used by MainUtil.SendMail() Default value: "" --> < setting name="MailServer" value="" /> < !-- MAIL SERVER USER If the SMTP server requires login, enter the user name in this setting --> < setting name="MailServerUserName" value="" /> < !-- MAIL SERVER PASSWORD If the SMTP server requires login, enter the password in this setting --> < setting name="MailServerPassword" value="" /> < !-- 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="25" />
I hope that will help...
This comment has been removed by the author.
ReplyDeleteIs there a way to check that mail is sent successfully?
ReplyDeleteThanks in Advance.