就像标题所说的那样,使用ASP.Net项目背后的C#代码,我想打开用户的默认邮件客户端,其邮件正文中已经填充了某些信息。
我能够直接发送包含信息的消息:
private static bool EnvoieCourriel(string adrCourriel, string corps, string objet, string envoyeur, Attachment atache)
{
SmtpClient smtp = new SmtpClient();
MailMessage msg = new MailMessage
{
From = new MailAddress(envoyeur),
Subject = objet,
Body = corps,
IsBodyHtml = true
};
if (atache != null)
msg.Attachments.Add(atache);
try
{
msg.To.Add(adrCourriel);
smtp.Send(msg);
}
catch
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我还能够打开用户的默认电子邮件客户端:
string email = op.CourrielOperateur1;
ClientScript.RegisterStartupScript(this.GetType(), "mailto", "parent.location='mailto:" + email + "'", true);
Run Code Online (Sandbox Code Playgroud)
但是现在...我想像第二个示例一样打开客户端,但是主体必须已经填充了默认文本...
任何想法,将不胜感激。
C#无法直接执行此操作。在您的情况下,您需要将c#与JS结合使用才能正常工作。
如RFC 6068所述,mailto允许您指定主题和正文以及cc字段。例如:
mailto:username@example.com?subject=Subject&body=message%20goes%20here
Run Code Online (Sandbox Code Playgroud)
如果您强制使用JavaScript打开链接,则用户无需单击链接
window.location.href =
"mailto:user@example.com?subject=Subject&body=message%20goes%20here";
Run Code Online (Sandbox Code Playgroud)
请注意,浏览器/电子邮件客户端没有简单的标准方式来处理mailto链接(例如,主题和正文字段可能会被丢弃而不会发出警告)。此外,还有弹出窗口和广告拦截器,防病毒软件等可能会默默阻止强制打开mailto链接的风险。