How to send Email from my application?
-
I have a Pdf fle in my application. I want to send this file by email when user press send via email button in my app. It will send a Email Notification like that.
Do you have any idea to do that?
Thank you! -
@Long-Vu-Ngoc You can send email from a Qt program by:
- Communicate directly with an SMTP server to send the email. There are a few Qt SMTP client libraries (under LGPL) that can be found with a simple search.
- Automating an existing mail client (e.g. Outlook)
- Execute a child process to send an email. For example, mailx on Linux, a Powershell script, or blat on Windows.
Your template HTML mail looks like you are intending that recipients click a download button to fetch the file from some external server. That is not the same as attaching the PDF to an email.
-
@Long-Vu-Ngoc said in How to send Email from my application?:
But I don't know how to embedded the html to body of email
Provide it as string to "body" parameter:
Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
-
-
@Long-Vu-Ngoc
And what is your question? Does something not work? Does it send email? Does email arrive but is not right? You don't say, so how should we know?The HTML you have posted is incorrect/malformed, so I would not expect it to work. Make it correct before you try to send it.
-
@JonB Hi
I want to use Qt to launch Outlook and attach html file in the body of email
As I commented, I use QProcess to run Outlook.exe and use command to attach html file in body of the emailOutlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
As @jsulm suggeset, I paste my html code in body. Sorry I don't know about html, I have just learned it.
Outlook already launch but the body email don't show html file. -
@Long-Vu-Ngoc
I still don't understand whether you are saying that if you send non-HTML, like you show here...&body="HERE YOUR HTML"
, that works? But if you send where you showedbody = "<!DOCTYPE html>" ...
that does not work? It arrives but is incorrect? It does not arrive? I don't know what you are saying.If you are trying to send the HTML body you show in
body = "<!DOCTYPE html>" ...
then you need to start by correcting it. Where did you get that string from, it is not right? It has a<head>
start tag but no</head>
matching end tag, so it is invalid HTML, and I don't know what Outlook will do with it. -
@Long-Vu-Ngoc
At least your new example is correct HTML now. I don't know where you copied the previous HTML you had from.
In Outlook you have a setting as to whether to display emails as plain text or HTML. Have you set that to allow it to show as HTML? -
@Long-Vu-Ngoc
That is actually the setting for whether to send messages in HTML. I thought there was one for whether to display in HTML received messages, but maybe not.I have never used a command line like
Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
It is possible that this
body=...
argument automatically means send as text not HTML, I don't know.On the message received in the inbox, can you display the source and see what it looks like. I think you right-click in an empty area of the body when showing a message and select View Source. Let's see whether it looks like it has sent any HTML.
-
@JonB said in How to send Email from my application?:
It is possible that this body=... argument automatically means send as text not HTML, I don't know.
You said right. I check source and it is Plain text,
Here is source: </head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoPlainText"><html><head></head><body>Hello World<h1>Hello World</h1><p>Hello World</p></body></html><o:p></o:p></p>
</div>
<br>
<span style="font-size: 8pt; font-family: Arial, sans-serif, serif, EmojiFont; color: rgb(115, 115, 115);"><i>***********************************************************************
<br> -
@Long-Vu-Ngoc
Yeah. I did have a Google around a looked quite a bit, not one example of this command line being used to send HTML (as opposed to plain) text, and not one person asking.IIRC, when you type into Outlook UI for rich text it does something like sends a message which has both HTML and plain text versions embedded in it, as "alternatives". For whatever reason I'm thinking you can't make it send as HTML from command-line
body=...
argument. -
@Long-Vu-Ngoc
If you really wanted to do this, given we can't find how to do it you would need to use VBA. Google if you don't know this. Qt hasQAxObject
as entry point to this. It's going to be a lot more code. Or you may be able to run Powershell as external command passing it parameters, that allows you to access this level.I would be looking at sending via SMTP instead, as @ChrisW67 mentioned above. But that too is some work, and you need to connect to an SMTP server available to you.
-
The mailto: syntax used in the Outlook command line allows only for plain text messages. That syntax predates HTML/rich text email by a long margin.
To send an HTML body via Outlook you could use a variation of the Powershell script found here. Something like:
# File blah.ps1 param ( [string]$to = "default@default.com", [string]$cc = "default@default.com", [string]$subject = "default subject", [string]$body = "body content", [switch]$html = $false ) $ol = New-Object -comObject Outlook.Application $mail = $ol.CreateItem(0) $mail.To = $to $mail.Cc = $cc $mail.Subject = $subject if($html) { $mail.HTMLBody = $body } else { $mail.Body = $body } $mail.Send
Launch Powershell using QProcess in much the same sort of way. IMHO all emails should contain a usable text-only part along with any rich text format. Outlook's interface seems to actively fight this (setting HTMLBody mangles Body).
Edit: If you are going to use Powershell then you should look at Send-MailMessage. This avoids Outlook client altogether, but requires an SMTP server (which may be a Microsoft mail server).
You may have to deal with restrictions on running Powershell scripts.
Have a look at Qutlook example to see how you might automate the application more directly.