Sending email with attachments

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using VB.net 2005, I am trying to send an email with attachments in Outlook.
I have tried using this article from Microsoft,
(http://support.microsoft.com/?kbid=313803), but I can't even get the dim
statements to work. It says it needs to be declared and I have used the
reference 0utlook 11.0. Also, I used Imports
Microsoft.Office.Interop.Outlook. Any ideas?
 
Add a reference too System.Net.Mail and use this code:

Private Sub SendEmail(ByVal Path As String, ByVal Too As String,
Optional ByVal Body As String = "")

Dim SendMessage As New SmtpClient
Dim Message As MailMessage = New
MailMessage(My.Settings.EmailFrom, Too, "EDI Transfer Report", Body)
Dim App As Attachment

If Path.Length > 0 And File.Exists(Path) Then
App = New Attachment(Path)
Message.Attachments.Add(App)
End If

SendMessage.Host = My.Settings.EmailServer
SendMessage.Send(Message)

End Sub
 
Thanks for your response, but I guess I failed to tell you how new I am at
this. When somebody sends me a piece of code, I just copy and paste it.
When I did this on yours after I added the reference as Imports
System.Net.Mail, I got numerous errors in the code. I am assuming that &quot
means ". But still, the My.Settings does not work. Anything you can help
with?
 
Try this:

Private Sub SendEmail(ByVal Path As String, ByVal Too As String,
Optional ByVal Body As String = "")

Dim SendMessage As New SmtpClient
Dim Message As MailMessage = New
MailMessage("(e-mail address removed)", Too, "Subject goes here", Body)
Dim App As Attachment

If Path.Length > 0 And File.Exists(Path) Then
App = New Attachment(Path)
Message.Attachments.Add(App)
End If

SendMessage.Host = "ServerName or IP Address" 'Name Of Your
SMTP Server Goes Here
SendMessage.Send(Message)

End Sub
 
I think I am getting it, but now it give me an error message andd asks me for
smtp authentication, which I guess is username and password. I don't see
where that goes. any ideas?
 
I use it with Exchange 2003 and don't ever have to supply a username a
password. Just a valid "from" address.

Try inserting this line of code:
SendMessage.Credentials = New Net.NetworkCredential("UserName",
"Password")

Give it a valid username and password.

Izzy
 
Mark,

As a prior newbie I also did the same thing. I would cut and paste code
into my projects and see if it would work. I know it is frustrating,
but one way to learn is to try to get the code to work after you paste
it into your project. I know it is frustrating as hell, but it does
help with learning code.

Scott Moore
 
You should also highlight items and press F1 then read about them an
see how else they can be used.
 
Back
Top