我想得到收据&发件人来自前端的电子邮件地址!
我在此位置创建了“自定义电子邮件发送”保存操作: “Sitecore / System / Modules / Settings / Save Actions /”&将其添加到表单的“提交”按钮的“保存操作”中。
在“自定义电子邮件发送”中“数据”部分的“内容”标签中的操作我已声明“汇编: .Helper,类: .Helper.CustomEmail'
并在“参数”字段的“提交”部分中声明: 主持人:smtp.gmail.com,端口:587,登录:example@gmail.com,密码: * ,IsBodyHtml:true,enableSSL:true
并在'编辑'字段中我已声明'编辑:控制:SendMail.Editor'。
但是点击表单中的“自定义电子邮件发送”操作,我没有在“收件人”,“抄送”,“BCC”等中输入值。
在形式上我采取了三个字段'To','From','Message'。我只想要那个
这是我在CustomEmail.cs类中的代码:
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
}
在web.config中我添加了代码:
<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>
我有这个例外:
请解决此问题。
答案 0 :(得分:2)
在我看来,您需要向SmtpClient添加凭据。
var smtp = new SmtpClient(Sitecore.Configuration.Settings.MailServer, Sitecore.Configuration.Settings.MailServerPort);
smtp.Credentials = new System.Net.NetworkCredential("username", "password")
smtp.EnableSsl = true;
smtp.Send(mailMsg);