Link to home
Start Free TrialLog in
Avatar of Thomas Stockbruegger
Thomas StockbrueggerFlag for Germany

asked on

Need some help with mailto

Hello,
How can I enclose the file that I want to send with this email, and also
how can I enclose a subject?

File:     C:/user/test.txt
Subject: this is a test


str_Email=”info@somebody.com”

ShellExecute(NULL,_T("open"),"mailto:"+str_EMail,NULL,NULL,SW_NORMAL);

Thank you very much for your help.
Best regards from
Thomas
Avatar of sarabande
sarabande
Flag of Luxembourg image

try

mailto:user@example.com?subject="Some Subject" &body="Some Content" ?Attach="c:\temp\test.txt"

see https://msdn.microsoft.com/en-us/library/aa767737.aspx for more Information.

Sara
Avatar of Thomas Stockbruegger

ASKER

Hi Sara,
thank you for your post. How can I enclose this code to ShellExecute?

ShellExecute(NULL,_T("open"),"mailto:user@example.com?subject="Some Subject" &body="Some Content" ?Attach="c:\temp\test.txt");
will not work.
Thank you.
Thomas
you may try a std::stringstream and concatenate like

#include <sstream>
....
std::string quoted_string(const std::string & str) { std::string s = "\""; s += str; s += "\""; return s; }


std::string stremail = "user@example.com";
std::string strsubject = "some subject";
std::string strbody = "some text";
std::string strattach= "c:\\temp\\test.txt"; 
std::ostringstream oss;
oss << "mailto:" 
      << stremail 
      << "?subject="<<quoted_string(strsubject)
      <<" &Body="<< quoted_string(strbody)
     << " ?Attach="<< quoted_string(strattach);

ShellExecute(NULL, "open", oss.str().c_str, NULL, NULL, SW_SHOW);

Open in new window


if your mfc project uses wide characters (called UNICODE by MS) you would use wstring and wostringstream like


#include <sstream>
....
std::wstring quoted_string(const std::wstring & str) { std::wstring s = L"\""; s += str; s += L"\""; return s; }


std::wstring stremail = L"user@example.com";
std::wstring strsubject = L"some subject";
std::wstring strbody = L"some text";
std::wstring strattach= L"c:\\temp\\test.txt"; 
std::wostringstream oss;
oss << L"mailto:" 
      << stremail 
      << L"?subject="<<quoted_string(strsubject)
      <<L" &Body="<< quoted_string(strbody)
     << L" ?Attach="<< quoted_string(strattach);

ShellExecute(NULL, L"open", oss.str().c_str, NULL, NULL, SW_SHOW);

Open in new window


you also could use CString and _T macro which automatically would do the Switch between ansi and wide characters.

if you get compile errors post the errors and the code lines the Errors refer to.

Sara
Hi Sara, did you mean this?

// EMAILDlg.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "EMAIL.h"
#include "EMAILDlg.h"
#include ".\emaildlg.h"
#include <sstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
.
.
.
.
//##########################################################################
void CEMAILDlg::OnBnClickedButton_Email()
{    
          
 

std::string quoted_string(const std::string & str) { std::string s = "\""; s += str; s += "\""; return s; }


std::string stremail = "user@example.com";
std::string strsubject = "some subject";
std::string strbody = "some text";
std::string strattach= "c:\\temp\\test.txt";
std::ostringstream oss;
oss << "mailto:"
      << stremail
      << "?subject="<<quoted_string(strsubject)
      <<" &Body="<< quoted_string(strbody)
     << " ?Attach="<< quoted_string(strattach);

ShellExecute(NULL, "open", oss.str().c_str, NULL, NULL, SW_SHOW);



}
//##########################################################################

I will get this error:

c:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Visual Studio Projects\EMAIL\EMAILDlg.cpp(192) : error C2601: 'quoted_string': Lokale Funktionsdefinitionen sind unzulässig
c:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Visual Studio Projects\EMAIL\EMAILDlg.cpp(206) : error C2664: 'ShellExecuteA': Konvertierung des Parameters 3 von 'const char *(void) const' in 'LPCSTR' nicht möglich
        Es gibt keinen Kontext, in dem diese Konvertierung möglich
quoted_string is a helper function which should be placed above your function

std::string quoted_string(const std::string & str) { std::string s = "\""; s += str; s += "\""; return s; }

void CEMAILDlg::OnBnClickedButton_Email()
{
    ...

Open in new window


you also can put it into the CEMAILDlg class as a member function (best directly as inline in the class definition). don't forget to #include <string> in the header file if you do so.


the ShellExecute call has to be changed to

ShellExecute(NULL, "open", oss.str().c_str(), NULL, NULL, SW_SHOW);

cstr is a function of std::string, which returns a const pointer to char what is equivalent to LPCSTR.

Sara
Hi Sara, I will get this result:
will not enclose the file.


User generated image



User generated image
// EMAILDlg.h : Headerdatei
#pragma once
#include <string>
#include <sstream>
.
.
.

// CEMAILDlg Dialogfeld
class CEMAILDlg : public CDialog
{
// Konstruktion
public:
      CEMAILDlg(CWnd* pParent = NULL);      // Standardkonstruktor

// Dialogfelddaten
      enum { IDD = IDD_EMAIL_DIALOG };

      protected:
      virtual void DoDataExchange(CDataExchange* pDX);      // DDX/DDV-Unterstützung

.
.
.
.
.




// EMAILDlg.cpp : Implementierungsdatei
#include "stdafx.h"
#include "EMAIL.h"
#include "EMAILDlg.h"
#include ".\emaildlg.h"
#include <sstream>
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
.
.
.
.
.
std::string quoted_string(const std::string & str) { std::string s = "\""; s += str; s += "\""; return s; }

//#####################################################################################
void CEMAILDlg::OnBnClickedButtonEmail()
{    

std::string stremail = "user@example.com";
std::string strsubject = "some subject";
std::string strbody = "some text";
std::string strattach= "o:\\Verwaltung\\1111\\1.doc";
std::ostringstream oss;
oss << "mailto:"
      << stremail
      << "?subject="<<quoted_string(strsubject)
      <<" &Body="<< quoted_string(strbody)
     << " ?Attach="<< quoted_string(strattach);

ShellExecute(NULL, "open", oss.str().c_str(), NULL, NULL, SW_SHOW);



}
//#####################################################################################
you may put the line with ?Attach= directly after ?subject line and above the &Body= line.

i will try to find some more samples.

Sara
Hi Sara, this is the result....no attachment.

std::ostringstream oss;
oss << "mailto:"
      << stremail
      << "?subject="<<quoted_string(strsubject)
   << " ?Attach="<< quoted_string(strattach)
      <<" &Body="<< quoted_string(strbody);

User generated image
ok. can you try


<< "?attachment=\"\"o:\\.....\"\""


 both before body and after body?

see the doubled quotes around the file path.

if that worked you could use

quoted_string(quoted_string(strattach)) to get the doubled "" into the c++ string.

i searched a long time and some people said they have got it working but no solution which was confirmed by others.

Sara
User generated image

Hi Sara, same result ... (both ways) will not work.


std::string stremail = "user@example.com";
std::string strsubject = "some subject";
std::string strbody = "some text";
std::string strattach= "O:\\Verwaltung\\1111\\1.doc";
std::ostringstream oss;
oss << "mailto:"
      << stremail
      << "?subject="<<quoted_string(strsubject)
        << "?attachment=\"\"O:\\Verwaltung\\1111\\1.doc\"\""
      <<" &Body="<< quoted_string(strbody);
     

ShellExecute(NULL, "open", oss.str().c_str(), NULL, NULL, SW_SHOW);

when I put the code behind Body the text="?attachment=\"\"O:\\Verwaltung\\1111\\1.doc\"\"" will show up after "some text"
in the specs they say that only the first parameter was prefixed by a ?

the other parameters are prefixed by &

Sara
Hi Sara, both ways will not work. No attachment.

std::string stremail = "user@example.com";
std::string strsubject = "some subject";
std::string strbody = "some text";
std::string strattach= "O:\\Verwaltung\\1111\\1.doc";
std::ostringstream oss;
oss << "mailto:"
     << stremail
       << "?subject="<<quoted_string(strsubject)
        << "&attachment=\"\"O:\\Verwaltung\\1111\\1.doc\"\""
       <<" &Body="<< quoted_string(strbody);



         << stremail
      << "?subject="<<quoted_string(strsubject)
        << " &Body="<< quoted_string(strbody)
      <<"&attachment=\"\"O:\\Verwaltung\\1111\\1.doc\"\"";



ShellExecute(NULL, "open", oss.str().c_str(), NULL, NULL, SW_SHOW);
User generated image
Sara, even if this is not working with the file attachament...can you let me know how I can use multi-line text with strbody
f.e.
std::string strbody = "With kindest regards\r\nThomas"; = will not work =With kindest regardsThomas

should look like on the email

With kindest regards
Thomas
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Sara, even if we get no luck with the attachment.
Have a nice day.
Best regards
Thomas
you might get more success with attachments when using a html mail request rather than mailto. i saw examples of html requests when looking for mailto examples. unfortunately i can't help you with html as it is not in my expertise. but i am pretty sure that there are plenty of samples around.

another way could be to use an smtp client for example like the one i found at www.port25.com/Free-Trial 

Sara