I want to get receipient & sender Email address from front-end!
I have created a 'Custom Email Sending' save action at this location:
"Sitecore/System/Modules/Settings/Save Actions/" & added it on a form's Submit buttons's Save Action.
In 'Custom Email Sending' Action in Content tab in 'Data' section I have declared 'Assembly: .Helper, Class:.Helper.CustomEmail'
and in 'Submit' Section in 'Parameter' field i have declared:
Host:smtp.gmail.com, Port:587, Login:example@gmail.com, Password:*, IsBodyHtml:true, enableSSL:true
and in 'Editor' fieldsection I have declared 'Editor: control:SendMail.Editor'.
But on click of 'Custom Email Sending' action in form I didn't entered value in To,CC,BCC etc.
And In form I have taken three fields 'To', 'From', 'Message'. All I want is that
- "I want to get receipient & sender address from front-end"
This is my code in CustomEmail.cs class:
public class CustomEmail : ISaveAction, ISubmit
{
public void Execute(Sitecore.Data.ID formid, AdaptedResultList fields, params object[] data)
{
NameValueCollection formData = new NameValueCollection();
foreach (AdaptedControlResult acr in fields)
{
formData[acr.FieldName] = acr.Value;
}
try
{
// get field values of form
string emailTo = formData["To"];
string emailFrom = formData["From"];
string personalMsg = formData["Message"];
bool result = false;
if (!string.IsNullOrEmpty(emailFrom) && !string.IsNullOrEmpty(emailTo))
{
// send mail
result = SendEmail(emailTo, emailFrom, personalMsg);
}
// if mail sending fail then show error massege
if (result == false)
{
Sitecore.Diagnostics.Log.Error("Some error occcured while sending email", this);
}
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.Message.ToString(), this);
}
}
#region SendEmail function
/// <summary>
/// send mail
/// </summary>
/// <param name="To"></param>
/// <param name="From"></param>
/// <param name="Message"></param>
public static bool SendEmail(string To, string From, string Message)
{
bool result = true;
try
{
MailMessage mailMsg = new MailMessage();
MailAddress mailFrom = new MailAddress(From);
mailMsg.From = mailFrom;
try
{
MailAddress mailTo = new MailAddress(To);
mailMsg.To.Add(mailTo);
}
catch
{
}
mailMsg.Body = Message;
mailMsg.IsBodyHtml = true;
var smtp = new SmtpClient(Sitecore.Configuration.Settings.MailServer, Sitecore.Configuration.Settings.MailServerPort);
smtp.EnableSsl = true;
smtp.Send(mailMsg);
}
catch
{
result = false;
}
return result;
}
#endregion
public void Submit(Sitecore.Data.ID formid, AdaptedResultList fields)
{
Execute(formid, fields);
}
#region IOnLoad Members
public void OnLoad(bool isPostback, RenderFormArgs args)
{
}
#endregion
}
In web.config I added code:
<setting name="MailServer" value="smtp.gmail.com" />
<setting name="MailServerUserName" value="***@gmail.com" />
<setting name="MailServerPassword" value="***" />
<setting name="MailServerPort" value="587" />
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
I got this Exception:
- The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication
Required. Learn more at
Please resolve this problem.