how do I use mailto with attachment?
Hey all... can someone tell me how this is done? My message will open but does not contain the attachment...
Dim
Mypath
AsString =
"c:\Myfolder\"'Get user email info (FromMe) message mail from
Dim MyEmailAsString =My.Forms.frmMain.GetUserName.ToString &"@some.com"
Dim FromMe = MyEmail
Dim mAsNew System.Net.Mail.MailMessage(MyEmail, MyEmail)
'
'Attach PDF Document
Dim AttachMeAsString = Mypath & frmMain.txtProposalNum.Text &"-" & frmMain.ComboRevision.Text &"some.pdf"
Dim CustomerAttachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(AttachMe)
'
'set default email message info
m.Subject ="Current Message"
m.Body ="This is a test"
m.Attachments.Add(CustomerAttach)
m.IsBodyHtml =True
'
'set SMTP client to local with default user credentials
Dim SmtpClientAs System.Net.Mail.SmtpClient =New System.Net.Mail.SmtpClient()
SmtpClient.UseDefaultCredentials =True
SmtpClient.Host ="localhost"
'
'Start by telling the control that we are composing an e-mail
Dim MsgBuilderAsNew System.Text.StringBuilder
MsgBuilder.Append("mailto:" & FromMe &"")
MsgBuilder.Append("&subject=this is test subject")
MsgBuilder.Append("&body=this is test body")
MsgBuilder.Append("&attachment=" & AttachMe &"")
ExecuteFile(MsgBuilder.ToString)
'
' function to open email message -
'
PrivateFunction ExecuteFile(ByVal FileNameAsString)AsBoolean
Dim myProcessAsNew Process
myProcess.StartInfo.FileName = FileName
myProcess.StartInfo.UseShellExecute =True
myProcess.StartInfo.RedirectStandardOutput =False
myProcess.Start()
myProcess.Dispose()
EndFunction
Thanks in advance,
BillB
[4115 byte] By [
billb59] at [2008-1-10]
nobugz wrote: |
| Don't use the mailto protocol. Contact an SMTP server directly with the SmtpClient class. | |
This is an occassionally connected client... So where is the open commmand in the SmtpClient class? I've tried SMTPClient class with no success... by providing an OpenEmail function I was able to open the email message but without the specified attachment....
'open email message for send using SMTPClient Class ?
SmtpClient.Send(MyEmail, "", m.Subject, m.Body)
This does not pop up the email message without a function... Can you provide an example?
If you're suggesting this...
Dim
[to] As
String
= "myemail@some.com"
Dim
From As
String
= FromMe.ToStringDim
message As
MailMessage = New
MailMessage(From, [to])message.Subject =
"Using the new SMTP client."
message.Body =
"Using this new feature, you can send an e-mail message from an application very easily."
message.Attachments.Add(CustomerAttach)
'
Dim
client As
SmtpClient = New
SmtpClient("localhost"
) '<- this works using your default "mail.some.com"' Credentials are necessary if the server requires the client
' to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials =
True
client.Send(message)
The above code does not work either without an additional form and textboxes that contains the necessary params... and besides it does not hit the mark of my original post of how do I just pop up an email message with the PDF document attached!
Someone has had to do this before with success.... any other ideas 
Thanks for your posts,
billb
Fun.... not sure you get what i'm saying here (I'm beginning to guess what "popup an email message" means)... it seems to me I've already got that part working fine without the Microsoft.Office.Interop.Outlook namespace and this should just be a simple attachment is what I'm talkin 'bout!
Someone must have this working out there... I know I can't possibly be the only one who needs this functionality.
Please don't take this text the wrong way... I appreciate your time and effort.
I was hoping to get around building another form with text boxes that will contain the params to use the SMTPClient by using the outlook default new mail message (popped up).
hmmm
If you want to use the Outlook mail form, then you have to automate Outlook to do it.
It's easier, though, to just make your own form than to try and wrestle Outlook's security into letting you use it to send a message. Of course the more of Outlook's features you want, the more you might be tempted to to automate it...things like address completion, access to the Outlook address book, automatically parsing email addresses with display names, checking spelling, adding attachments, using Word as the editor...this stuff starts to add up after a while.
But if you want to just send a simple text email with an attachment, building your own form may give you fewer headaches. Or you could always search one or more of the many code repositories for some simple email forms that people have already written. Because you're right, this is a problem that's been solved a few thousand times.
I've totally have it down now and thought I'd post for someone else in need.
The call to open message with attachment:
'open message and send with attach......
Dim
MAPI As
New
SendFileTo.MAPIMAPI.SendMail(AttachMe.ToString,
"Quote number: "
& QuoteNumber & ""
) -
The path on the users hard drive as FileStream:
Dim
PDFExists As
String
= Mypath & frmMainMax.txtProposalNum.Text & "-"
& frmMainMax.ComboRevision.Text & "Proposal.pdf"
'
On
Error
Resume
Next
'
'trap for existing report
If
PDFExists = PDFExists Then
'append old pdf proposal
Dim
fs As
New
IO.FileStream(Mypath & frmMainMax.txtProposalNum.Text & "-"
& frmMainMax.ComboRevision.Text & "Proposal.pdf"
, IO.FileMode.Append)fs.Write(bytes, 0, bytes.Length)
fs.Close()
fs.Dispose()
Else
'
Dim
fs As
New
IO.FileStream(Mypath & frmMainMax.txtProposalNum.Text & "-"
& frmMainMax.ComboRevision.Text & "Proposal.pdf"
, IO.FileMode.OpenOrCreate)fs.Write(bytes, 0, bytes.Length)
fs.Close()
fs.Dispose()
End
If
'
' ''
' ''Attach PDF Document
Dim
AttachMe As
String
= Mypath & frmMainMax.txtProposalNum.Text & "-"
& frmMainMax.ComboRevision.Text & "Proposal.pdf"
Dim
CustomerAttach As
System.Net.Mail.AttachmentBase = New
System.Net.Mail.Attachment(AttachMe)
--
VB Module or Class File
Name: SendFileTox.vb
Imports System
Imports System.Runtime.InteropServices
Imports System.IO
Namespace SendFileTo
Class MAPI
Private Const MAPI_LOGON_UI As Integer = &H1
Private Const MAPI_DIALOG As Integer = &H8
Public Shared Function SendMail(ByVal strAttachmentFileName As String, ByVal strSubject As String) As Integer
Dim
session As
IntPtr = New
IntPtr(0)Dim
winhandle As
IntPtr = New
IntPtr(0)Dim
msg As
MapiMessage = New
MapiMessagemsg.subject = strSubject
Dim
sizeofMapiDesc As
Integer
= Marshal.SizeOf(GetType
(MapiFileDesc))Dim
pMapiDesc As
IntPtr = Marshal.AllocHGlobal(sizeofMapiDesc)Dim
fileDesc As
MapiFileDesc = New
MapiFileDescfileDesc.position = -1
Dim
ptr As
Integer
= CType
(pMapiDesc, Integer
)Dim
strPath As
String
= strAttachmentFileNamefileDesc.name = Path.GetFileName(strPath)
fileDesc.path = strPath
Marshal.StructureToPtr(fileDesc,
CType
(ptr, IntPtr), False
)msg.files = pMapiDesc
msg.fileCount = 1
Return
MAPISendMail(session, winhandle, msg, MAPI_LOGON_UI Or
MAPI_DIALOG, 0)End
Function
<DllImport(
"MAPI32.DLL"
)> _Private
Shared
Function
MAPISendMail(ByVal
sess As
IntPtr, ByVal
hwnd As
IntPtr, ByVal
message As
MapiMessage, ByVal
flg As
Integer
, ByVal
rsv As
Integer
) As
Integer
End
Function
End
Class
<StructLayout(LayoutKind.Sequential)> _
Public
Class
MapiMessagePublic
reserved As
Integer
Public
subject As
String
Public
noteText As
String
Public
messageType As
String
Public
dateReceived As
String
Public
conversationID As
String
Public
flags As
Integer
Public
originator As
IntPtrPublic
recipCount As
Integer
Public
recips As
IntPtrPublic
fileCount As
Integer
Public
files As
IntPtrEnd
Class
<StructLayout(LayoutKind.Sequential)> _
Public
Class
MapiFileDescPublic
reserved As
Integer
Public
flags As
Integer
Public
position As
Integer
Public
path As
String
Public
name As
String
Public
type As
IntPtrEnd
Class
End Namespace
-
DaveWasEre... you can easily attach multiple files with the above code and "concatenation"!
Enjoy and have a great weekend!
billb