By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

send a email and attach file in VB

P: 91
Hi

I'd like to implement in VB2005 to send a email and attech a file inside the email. Anyone knows how to do it?

Thanks.
Nov 4 '09 #1
Share this Question
Share on Google+
12 Replies


tlhintoq
Expert 2.5K+
P: 3,525
There are examples of this all over the net including MSDN.
What have you tried so far? Where is your code hanging up?
Nov 4 '09 #2

P: 91
below are some code I got from internet. but I have problem with the statement,
MailMsg.BodyEncoding = Encoding.Default, which indicate that Encoding is not declared.
And How can I get SMTP host name or IP address?

----------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System.net.Mail
  3.  
  4. Public Class SendEmail
  5.     Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
  6.     As String, ByVal strSubject _
  7.     As String, ByVal strMessage _
  8.     As String, ByVal fileList() As String)
  9.         'This procedure takes string array parameters for multiple recipients and files
  10.         Try
  11.             For Each item As String In strTo
  12.                 'For each to address create a mail message
  13.                 Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
  14.                 'MailMsg.BodyEncoding = Encoding.Default
  15.                 MailMsg.Subject = strSubject.Trim()
  16.                 MailMsg.Body = strMessage.Trim() & vbCrLf
  17.                 MailMsg.Priority = MailPriority.High
  18.                 MailMsg.IsBodyHtml = True
  19.  
  20.                 'attach each file attachment
  21.                 For Each strfile As String In fileList
  22.                     If Not strfile = "" Then
  23.                         Dim MsgAttach As New Attachment(strfile)
  24.                         MailMsg.Attachments.Add(MsgAttach)
  25.                     End If
  26.                 Next
  27.  
  28.                 'Smtpclient to send the mail message
  29.                 Dim SmtpMail As New SmtpClient
  30.                 SmtpMail.Host = "10.10.10.10"
  31.                 SmtpMail.Send(MailMsg)
  32.             Next
  33.             'Message Successful
  34.         Catch ex As Exception
  35.             ex.ToString()
  36.         End Try
  37.     End Sub
  38.  
  39.     Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
  40.     As String, ByVal strSubject _
  41.     As String, ByVal strMessage _
  42.     As String, ByVal file As String)
  43.         'This procedure overrides the first procedure and accepts a single
  44.         'string for the recipient and file attachement 
  45.         Try
  46.             Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
  47.             'MailMsg.BodyEncoding = Encoding.Default
  48.             MailMsg.Subject = strSubject.Trim()
  49.             MailMsg.Body = strMessage.Trim() & vbCrLf
  50.             MailMsg.Priority = MailPriority.High
  51.             MailMsg.IsBodyHtml = True
  52.  
  53.             If Not file = "" Then
  54.                 Dim MsgAttach As New Attachment(file)
  55.                 MailMsg.Attachments.Add(MsgAttach)
  56.             End If
  57.  
  58.             'Smtpclient to send the mail message
  59.             Dim SmtpMail As New SmtpClient
  60.             SmtpMail.Host = "10.10.10.10"
  61.             SmtpMail.Send(MailMsg)
  62.         Catch ex As Exception
  63.             'Message Error
  64.         End Try
  65.     End Sub
  66. End Class
Nov 5 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Nov 5 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
@qfchen
Encoding is for text encoding. Default is probably fine as it should take the encoding scheme for your OS. Unless you want to specify an encoding for Japanese or Arabic. When in doubt about methods and properties: Check the MSDN.

I'm not sure what you are expecting for "get the SMTP host name".
I would guess you have email. Probably more than one account. You had to provide SMPT host, username and password to Outlook (or your preferred email application) for all those accounts. Same here. You have to provide the account information that you want to use for sending this email.
Nov 5 '09 #5

P: 91
Hi, I'm using company email server, I got the server name from our IT department and added to the code as below,

SmtpMail.Host = "mailserv.mam.com"

then run the application, it shows enter password, how should I pass the lotus password in the VB codes?

The email and attachment were sent out successfully, however got error message from lotus notes as below. and the application closed automatically. any idea? Thanks

"an error occurred in a lotus notes application"
"data about error is being collected for your company's help desk for use in error analysis"
"the lotus notes application will close when data collection is complete"
Nov 6 '09 #6

P: 91
Hi, below are the codes, a small modification and display error message. here is the message get from the exception catch:"source:System, message:The parameter 'address' cannot be an empty string, Parameter name:address"

The mail still able to be delivered except above error message.

Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System.net.Mail
  3.  
  4. Public Class SendEmail
  5.     Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
  6.     As String, ByVal strSubject _
  7.     As String, ByVal strMessage _
  8.     As String, ByVal fileList() As String)
  9.         'This procedure takes string array parameters for multiple recipients and files
  10.         Try
  11.             For Each item As String In strTo
  12.                 'For each to address create a mail message
  13.                 Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
  14.                 'MailMsg.BodyEncoding = Encoding.Default
  15.                 MailMsg.Subject = strSubject.Trim()
  16.                 MailMsg.Body = strMessage.Trim() & vbCrLf
  17.                 MailMsg.Priority = MailPriority.High
  18.                 MailMsg.IsBodyHtml = True
  19.  
  20.                 'attach each file attachment
  21.                 For Each strfile As String In fileList
  22.                     If Not strfile = "" Then
  23.                         Dim MsgAttach As New Attachment(strfile)
  24.                         MailMsg.Attachments.Add(MsgAttach)
  25.                     End If
  26.                 Next
  27.  
  28.                 'Smtpclient to send the mail message
  29.                 Dim SmtpMail As New SmtpClient
  30.  
  31.                 SmtpMail.Host = frmSetting.txtSMTP.Text.ToString
  32.                 SmtpMail.Send(MailMsg)
  33.             Next
  34.             'Message Successful
  35.         Catch ex As Exception
  36.             MsgBox("source:" & ex.Source & ",  " & "message:" & ex.Message)
  37.         End Try
  38.     End Sub
  39. End Class
  40.  
  41. Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  42.         Dim SendEmail As New SendEmail
  43.         Dim SendTo(2) As String
  44.         Dim FileAttach(2) As String
  45.         Dim strSubject As String
  46.         Dim strMessage As String
  47.         Dim strFrom As String
  48.         SendTo(0) = txtEmailTo1.Text.ToString
  49.         SendTo(1) = txtEmailTo2.Text.ToString
  50.  
  51.         FileAttach(0) = "c:\temp\888 success Story Jabil.pdf"
  52.         FileAttach(1) = "c:\temp\888 success Story Jabil.pdf"
  53.  
  54.         strSubject = txtSubject.Text.ToString
  55.  
  56.         strMessage = "Email Messge Text" 'The body encoding is set to HTML 
  57.         strFrom = txtFrom.Text.ToString
  58.         SendEmail.SendEmailMessage(strFrom, SendTo, strSubject, strMessage, FileAttach)
  59. End Sub
Nov 6 '09 #7

tlhintoq
Expert 2.5K+
P: 3,525
When posting code please make a point of using the [code] tags as described in my tip earlier. The make the posts and the code much easier to read.
Nov 6 '09 #8

tlhintoq
Expert 2.5K+
P: 3,525
Hi, below are the codes, a small modification and display error message. here is the message get from the exception catch:"source:System, message:The parameter 'address' cannot be an empty string, Parameter name:address"
Glad you found your error.
Nov 6 '09 #9

P: 91
but I don't know why it show me this error.
:"source:System, message:The parameter 'address' cannot be an empty string, Parameter name:address"
The mail is sent successfully, however my application captured this exception.
Expand|Select|Wrap|Line Numbers
  1. Catch ex As Exception 
  2.             MsgBox("source:" & ex.Source & ",  " & "message:" & ex.Message) 
  3.         End Try 
  4.  
Nov 7 '09 #10

tlhintoq
Expert 2.5K+
P: 3,525
Maybe it's not the "to" address that's empty.
Maybe its the "from" address that empty.
Put in a breakpoint and walk through and check all your values - line by line.
Nov 7 '09 #11

P: 91
both 'to' and 'from' address are not empty. I had provided 5 parameters:strFrom, SendTo, strSubject, strMessage, FileAttach. all had checked with proper value. but still got the same error message as below:
source:System, message:The parameter 'address' cannot be an empty string, Parameter name:address
Nov 7 '09 #12

tlhintoq
Expert 2.5K+
P: 3,525
Your btnEmailSend_Click method bothers me.
There is not validation of the addresses. No confirmation that those textboxes actually contain data.

It is conceivable that either TextEmailTo1 or TextEmailTo2 or TextEmailFrom could be empty and the method will still try to send using 2 "to" addresses and a from address even if they are blank.

You really should check that all the fields contain information before allowing the method to call the SendEmail function. Just to be safe.

I would recommend a breakpoint at line 11 and walk through the function both times it goes through the loop (for both of the "to" addresses just to confirm all your values are coming through as expected.
Nov 7 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.