locked
Sending email with javascript RRS feed

  • Question

  • User-809753952 posted

    I need to send email from client side and I am using this code. 

    function TriggerOutlook(emailTo, emailSub, emailBody) { 
                window.open("mailto:" + emailTo + "?body=" + emailBody + "&subject=" + emailSub);
            }
    

    I am calling this from code behind as:

       Dim emailTo As String = lblReqBy.Text
            Dim emailSubject As String = " Question regarding your stock request dated " & lblReqOn.Text 
            Dim source As String = lblSource.Text.ToLower
            Dim Nr As String = lblNr.Text
            Dim emailBody As String = "" ' 
            emailBody = "https://stockreq.xxx.xxx/stockreqtest.aspx?" & "src=" & source & "%26no=" & Nr
            ClientScript.RegisterStartupScript(Me.[GetType](), "Out", "TriggerOutlook('" & emailTo & "','" & emailSubject & "' ,'" & emailBody & "'  )", True)
    
    

    It works well.  But I need to show another text (like Open Stock) instead of the hyperlink in the outlook body. How can I achieve it?

    Thursday, May 13, 2021 12:17 PM

All replies

  • User475983607 posted

    mnmhemaj

    It works well.  But I need to show another text (like Open Stock) instead of the hyperlink in the outlook body. How can I achieve it?

    I'm not sure what you asking.  Rather than using a link to open the user's email client, you can take advantage of the SmtpClient or MailKit to send SMTP messages from the server.  The application will need an SMTP service and account.  

    https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-5.0

    https://www.nuget.org/packages/MailKit/

    There are JavaScript libraries that send SMPT messages.  You'll be faced with the same configuration as sending from the web server.  Your application will need an SMTP service to send the emails.  Plus it is not very secure since anyone can view the code.

    https://www.geeksforgeeks.org/how-to-send-an-email-from-javascript/

    Thursday, May 13, 2021 12:32 PM
  • User-809753952 posted

    Thanks mgebhard for your reply. I want to open Client Outlook. That is why I am not using SMTP.

    When the email client is opened, the body text is :  https://stockreq.xxxx.xxx/stockreqtest.aspx?src=rdp&no=100062

    But I want the body text as:   Open Link

    Thursday, May 13, 2021 1:37 PM
  • User-474980206 posted

    Unfortunately the body parameter only supports text, not html and you can not create a link.

    Thursday, May 13, 2021 2:40 PM
  • User-809753952 posted

    Hi! I can show the link "  https://stockreq.xxxx.xxx/stockreqtest.aspx?src=rdp&no=100062" in the email body.

    But  I want to show a text "Open link" and when you click on the text,  it should be open the link. 

    How will you write the body string for this?

    I write 

    emailBody="<html><a href=https://stockreq.xxx.xxx/stockreqtest.aspx?" & "src=" & source & "%26no=" & Nr " > Open Link</a></html>"

    and then I open outlook as

     window.open("mailto:" + emailTo + "?body=" + emailBody + "&subject=" + emailSub);

    This writes the whole text  <html><a href=https://stockreq.xxx.xxx/stockreqtest.aspx?" & "src=" & source & "%26no=" & Nr " > Open Link</a></html> in the body. What is wrong the syntax?

    Thursday, May 13, 2021 3:48 PM
  • User1535942433 posted

    Hi mnmhemaj,

    As far as I think,there is not possible.

    The special <hfname> "body" indicates that the associated <hfvalue> is the body of the message. The "body" field value is intended to contain the content for the first text/plain body part of the message. The "body" pseudo header field is primarily intended for the generation of short text messages for automatic processing (such as "subscribe" messages for mailing lists), not for general MIME bodies.

    More details,you could refer to below article:

    https://stackoverflow.com/questions/5620324/mailto-link-with-html-body

    Best regards,

    Yijing Sun

    Friday, May 14, 2021 5:33 AM
  • User753101303 posted

    Hi,

    Check F12 Console but as pointed already it's likely that mailto doesn't support having an HTML body for safety reason. The alternate option I see would be from the server side:
    to send the mail to the sender which can then update and forward the message
    - or
    to serve an EML file https://en.wikipedia.org/wiki/Email#Filename_extensions

    Outlook should be able to open this file and the user can then update and hit the send button.

    Friday, May 14, 2021 9:52 AM