Antoine Pelletier
Antoine Pelletier

Reputation: 3316

ASP.Net C# - Open default Email client with new Email having body filled with informations

As the title says, using C# code-behind of a ASP.Net project, I would like to open the user's default mail client with a message's body already filled with certain information.

I was able to directly send a message with information in it:

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;
}

I was also able to open the user's default email client :

string email = op.CourrielOperateur1;
ClientScript.RegisterStartupScript(this.GetType(), "mailto", "parent.location='mailto:" + email + "'", true);

But now... I would like to open the client just like the second example but the body must be already filled with a default text...

Any ideas would be appreciated.

Upvotes: 1

Views: 7549

Answers (2)

cpeterson
cpeterson

Reputation: 63

Here is a working example: ASP Page:

<tr>
<td>
<asp:Label ID="lblEmail" runat="server" Text="Email: "></asp:Label>
</td>
<td>
<asp:hyperlink id="lnkEmail1" runat="server" navigateurl="mailto:[email protected]?subject=MessageTitle&body;=MessageContent" target="" text="" xmlns:asp="#unknown"/>
</td>
</tr>

C#:

    //call this in !ispostback in page_load
    protected void BuildEmailAsLink()
        {
        //Email is different because we have to make the link clickable.
        //navigateurl="mailto:[email protected]?subject=MessageTitle&body=MessageContent"

        var email = lnkEmail1.Text;

        //Only need to load the email address once on pageload. otherwise reuse it from the UI.
        if (!string.IsNullOrEmpty(email) && (email.Length > 1) && email.Contains("@"))
        {
            //continue;
        }
        else
        {
            email = lnkEmail1.Text.Trim();
        }

        if (string.IsNullOrEmpty(email) == false)
        {
            lnkEmail1.NavigateUrl = lnkEmail1.NavigateUrl.Replace("[email protected]", email);
            lnkEmail1.NavigateUrl = lnkEmail1.NavigateUrl.Replace("MessageTitle", "Reaching Out");
            lnkEmail1.NavigateUrl = lnkEmail1.NavigateUrl.Replace("MessageContent",
                string.IsNullOrEmpty(lblFirstName.Text.Trim()) == false
                    ? string.Format("Hi {0},", lblFirstName.Text.Trim())
                    : "Hi,");

            lnkEmail1.Text = email;
            lnkEmail1.Visible = true;
        }
    }

Upvotes: 0

P. Roe
P. Roe

Reputation: 2117

C# can't do this directly. In your case you need to use c# combined with JS to get this working.

See this Answer

As described by RFC 6068, mailto allows you to specify subject and body, as well as cc fields. For example:

mailto:[email protected]?subject=Subject&body=message%20goes%20here

User doesn't need to click a link if you force it to be opened with JavaScript

window.location.href =
"mailto:[email protected]?subject=Subject&body=message%20goes%20here";

Be aware that there is no single, standard way in which browsers/email clients handle mailto links (e.g. subject and body fields may be discarded without a warning). Also there is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of mailto links.

Upvotes: 7

Related Questions