Solved

Problem creating email message with attachment

Posted on 2009-06-29
13
349 Views
Last Modified: 2012-05-07
WHAT I AM TRYING TO DO:
Create an email with an attachment while my laptop is offline.  I need this message to sit in the outbox until the user connects to the company network to send it.

WHAT I HAVE:
I have attached the code I am using to create the message but I get an "SMTPExecption was caught/ Failure sending mail" error when the emailClient.Send(message) line is executed.  

The InnerExecption for this error = [System.Net.Webexecption] "The remote name could not be resolved: xxx.xx.xxx.com.
This makes some sense to me because the system is not connected to the company network.  So I tested the same code out while connected to the company network and got this error
"SMTPExecption was caught/ Failure sending mail"
The InnerExecption for this error = [System.IO.IOExecption] "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.


So the bottom line is how can I do this?  Thanks for any and all help.

'*********mail zipped file.****************************************************************************

        'find current user email address

        Dim from As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\User Location Service\Client", "Email Name", "Default Value")

        Dim Attachment As System.Net.Mail.Attachment

        Dim message As New System.Net.Mail.MailMessage(from, "xxx@xxx.com", "Some Subject", "")

        Dim emailClient As New System.Net.Mail.SmtpClient("xxx.xxx.xxx.com")
 

        Attachment = New Net.Mail.Attachment("some zip file.zip")

        message.Attachments.Add(Attachment)

        Try

            emailClient.Send(message)

        Catch ex As Exception

        End Try

Open in new window

0
Comment
Question by:NevSoFly
  • 6
  • 5
  • 2
13 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24740008
If you want the mail to sit in your outbox until your connected to the network your going to need top use MAPI not SMTP. SMTP mail will show in you sent box, it never really goes through your outbox.
But when connected your sample should work. What is "xxx.xxx.xxx.com"? Have you tried using just the IP of your SMTP server.
Further... if your using exchange I believe you need to configure it for SMTP.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 24740101
The smtp sendmail assembly tries to connect directly to the smtp server on send.  If it fails then the mail does not get moved to an outbox, it simply fails.  

You have two options here, if you still want to use smtp then creating some sort of caching system will be required, a small database table with the contents of the email and a polling program to detect when it's possible to send mails, this will become your 'outbox'.

The second option involves using outlook (ie. MAPI) instead.  If the mail fails to send then MAPI will move it to your outbox automatically and the mail will be sent for you when a connection to the MAPI server is made.  After the release of SP2 for outlook 2000 using MAPI to send mails directly would create popups asking the user if they were happy for an application to send emails on their behalf, which is no good for coding (but good security for virii).  However a set of libraries exists called Outlook Redemption which allows developers to get around the security restrictions.  One thing to keep in mind is that the free version of Outlook Redemption occasionally has the odd nag screen (like once every 60 odd emails, not sure of the exact count).  

Personally I would go with the first option.  If the software is running from a PC where you don't really want to install SQL Express etc. there are plenty of free standalone database options out there (my favourite being firebird) which do not require installs.  However (another caveat) the standalone firebird db can only have ONE concurrent user (although this may have changed, haven't used firebird for a couple years).

The first option involves a lot more coding but more control, the second a lot less coding but the occasional nag screen (unless you pay for the Outlook Redemption libraries).
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 24740781
Just thought of another option

You can install/enable the smtp proxy, the proxy just forwards smpt mails sent to it to another smtp server, any that are not sent get put into a bad mail folder as files.  When the main smtp server is available you can just copy the correct <mail>.bad files to the pickup folder for them to be resent.
0
 

Author Comment

by:NevSoFly
ID: 24747423
It may be sometime before the user connects to the company network after running this app (hours or even days).  I think it may be easier for me to go the mapi route.  Could you point me to a sample code or article as I have googled & binged this subject but have not yet found a good vb.net 2005 example.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 24747604
Here is a link to the redemption objects
http://www.dimastr.com/redemption/

and some sample code to show it's use


Imports Microsoft.Office.Interop.Outlook
 

Imports RedemptionNet
 

Class MyApp
 

Public Shared Sub Main()
 

Dim objApp As Microsoft.Office.Interop.Outlook.Application
 

Dim objNS As Microsoft.Office.Interop.Outlook.NameSpace
 

Dim objSafeItem As RedemptionNet.SafeMailItem
 

Dim objMailItem As Microsoft.Office.Interop.Outlook.MailItem
 

objApp = New Application
 

objNS = objApp.GetNamespace("MAPI")
 

objNS.Logon("Outlook", "", False, False)
 

objMailItem = objApp.CreateItem(OlItemType.olMailItem)
 

objSafeItem = New SafeMailItem
 

objSafeItem.Item = objMailItem
 

objSafeItem.Recipients.Add("x@x.x")
 

objMailItem.Subject = "TestMessage"
 

objSafeItem.Body = "TestMessage"
 

objSafeItem.Send()
 

objSafeItem = Nothing
 

objMailItem = Nothing
 

objNS.Logoff()
 

objNS = Nothing
 

objApp = Nothing
 
 
 

End Sub
 

End Class

Open in new window

0
 

Author Comment

by:NevSoFly
ID: 24765416
ok, I tried your sample and added 'Microsoft Office 11.0 Object Library' and the Redemption OUtlook library references to the project.    I got the following errors:
1      Type 'Microsoft.Office.Interop.Outlook.Application' is not defined.
2      Type 'Microsoft.Office.Interop.Outlook.NameSpace' is not defined.      
3      Type 'RedemptionNet.SafeMailItem' is not defined.      
4      Type 'Microsoft.Office.Interop.Outlook.MailItem' is not defined.
5      Type 'System.Windows.Forms.Application' has no constructors.
6      Name 'OlItemType' is not declared.


I also noticed that Imports Microsoft.office.Interop.Outlook was not available and Imports RedemptionNet did not work but Imports Redemption did.

I've had problems in the past getting Microsoft.Office.Interop to work.  Can you see what I am doing wrong?
Imports Microsoft.Office.Interop.Outlook

Imports RedemptionNet
 

Public Class Form1
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objApp As Microsoft.Office.Interop.Outlook.Application

        Dim objNS As Microsoft.Office.Interop.Outlook.NameSpace

        Dim objSafeItem As RedemptionNet.SafeMailItem

        Dim objMailItem As Microsoft.Office.Interop.Outlook.MailItem
 

        objApp = New Application

        objNS = objApp.GetNamespace("MAPI")

        objNS.Logon("Outlook", "", False, False)

        objMailItem = objApp.CreateItem(OlItemType.olMailItem)

        objSafeItem = New SafeMailItem

        objSafeItem.Item = objMailItem

        objSafeItem.Recipients.Add("x@x.x")

        objMailItem.Subject = "TestMessage"

        objSafeItem.Body = "TestMessage"

        objSafeItem.Send()
 

        objSafeItem = Nothing

        objMailItem = Nothing

        objNS.Logoff()

        objNS = Nothing

        objApp = Nothing
 

    End Sub

End Class

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24765479
you need to add the references to your project.
0
 

Author Comment

by:NevSoFly
ID: 24766210
they were already added.
0
 
LVL 11

Accepted Solution

by:
LordWabbit earned 500 total points
ID: 24770410
Not sure where you got the "Imports RedemptionNet" from but if I remove the Net part everything works fine.  What version of office are you using btw, the namespaces change so you might have to make some adjustments for that.
Imports Microsoft.Office.Interop.Outlook

Imports Redemption
 

Public Class Form1
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objApp As Microsoft.Office.Interop.Outlook.Application

        Dim objNS As Microsoft.Office.Interop.Outlook.NameSpace

        Dim objSafeItem As Redemption.SafeMailItem

        Dim objMailItem As Microsoft.Office.Interop.Outlook.MailItem
 

        objApp = New Application

        objNS = objApp.GetNamespace("MAPI")

        objNS.Logon("Outlook", "", False, False)

        objMailItem = objApp.CreateItem(OlItemType.olMailItem)

        objSafeItem = New Redemption.SafeMailItem

        objSafeItem.Item = objMailItem

        objSafeItem.Recipients.Add("x@x.x")

        objMailItem.Subject = "TestMessage"

        objSafeItem.Body = "TestMessage"

        objSafeItem.Send()
 

        objSafeItem = Nothing

        objMailItem = Nothing

        objNS.Logoff()

        objNS = Nothing

        objApp = Nothing
 

    End Sub

End Class

Open in new window

0
 

Author Comment

by:NevSoFly
ID: 24772701
Imports Redemptionam was in the original code sample that you posted.  I am using outlook 2003.  How do I find out what the namespace is if it has changed?
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 24779111
by trial and error, in your using statement hit ctrl+space to force the intellisense menu to come up and then start looking through the Microsoft namespace - usually something like whats in the examples, the difference is slight but should be something very similar (sorry I cant check for you cos I dont have  a pc with office 2003 installed).
0
 

Author Comment

by:NevSoFly
ID: 24798924
I did as you suggested but all I get is MIcrosoft.Office.Core not Interop.  I have added the 'reference 'Microsoft Office 11.0 Object Library' to my project.  I have also tried 9,10 & 12.  This is really starting to bug me.  Is there anything else you can think of to try?
0
 

Author Closing Comment

by:NevSoFly
ID: 31604834
OK I found the problem.  I was referencing Microsoft Office 11.0 Object Library when I should have been referencing Microsoft Outlook 11.0 Object Library.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 69
Hide Tab Page 3 34
Change Format on Data when exporting to Excel 4 23
Get Client IP on RDS - VB.NET 15 27
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now