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 MypathAsString ="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]
# 1
The mailto protocol doesn't support an attachment option. Check this MSDN topic for reference.
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Do you or anyone else no a workaround?

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Don't use the mailto protocol. Contact an SMTP server directly with the SmtpClient class.
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
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?

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Check the MSDN library for SmtpClient.Send(MailMessage), it's got a good example.
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

If you're suggesting this...

Dim [to] As String = "myemail@some.com"

Dim From As String = FromMe.ToString

Dim 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 Smile

Thanks for your posts,

billb

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
Okay, I'm beginning to guess what "popup an email message" means. You'll have to automate the installed email client. Such as through the Microsoft.Office.Interop.Outlook namespace. That's off topic for this forum, ask questions in an Outlook or Office developer's newsgroup at www.microsoft.com/communities
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

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

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9

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.

ChristopherPayne at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10

Christopher, I've all ready did the form thing for a simple send thing and found this article in the meantime:

http://www.thescripts.com/forum/thread538389.html

this link will take you to http://www.codeproject.com/cs/internet/simplemapidotnet.asp after some light reading

and should be the solution I was looking for... just imagined it would be easier to find!

Thanks,

billb

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11

You need the quotes around the filename so this should work :

<a href='mailto:b.gates@microsoft.com?subject=hello&Attach="c:\one.txt"'>mailto</a>

Outlook 2003 does the quotes differently.

So

MsgBuilder.Append("&Attachment=\"c:\one.txt\"'");

becomes

MsgBuilder.Append("&Attachment=\"\"c:\one.txt\"\"'");

Anyway is it possible to have 2 attachments?

Just can't seem to find any info on this.

DaveWasEre at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 12

hey DaveWasEre... i'm trippin or something.... I've tried:

MsgBuilder.Append("mailtoTongue Tiedomeone@one.com?subject=hello&Attach=""" & AttachMe.ToString & """'")

and get a Microsoft Office Outlook dialog message box that says:

The command line argument is not valid. Verify the switch you are using.

The icon is mailto with a colon... Anyone have any idea why I am not firing up the mail message?

Thanks,

billb

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 13

I've totally have it down now and thought I'd post for someone else in need.

  • VB.NET 2.0 source:

The call to open message with attachment:

'open message and send with attach......

Dim MAPI As New SendFileTo.MAPI

MAPI.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 MapiMessage

msg.subject = strSubject

Dim sizeofMapiDesc As Integer = Marshal.SizeOf(GetType(MapiFileDesc))

Dim pMapiDesc As IntPtr = Marshal.AllocHGlobal(sizeofMapiDesc)

Dim fileDesc As MapiFileDesc = New MapiFileDesc

fileDesc.position = -1

Dim ptr As Integer = CType(pMapiDesc, Integer)

Dim strPath As String = strAttachmentFileName

fileDesc.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 MapiMessage

Public 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 IntPtr

Public recipCount As Integer

Public recips As IntPtr

Public fileCount As Integer

Public files As IntPtr

End Class

<StructLayout(LayoutKind.Sequential)> _

Public Class MapiFileDesc

Public reserved As Integer

Public flags As Integer

Public position As Integer

Public path As String

Public name As String

Public type As IntPtr

End Class

End Namespace

-

DaveWasEre... you can easily attach multiple files with the above code and "concatenation"!

Enjoy and have a great weekend!

billb

billb59 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...