send a email and attach file in VB | Member | | Join Date: Oct 2006
Posts: 64
| | |
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.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB
There are examples of this all over the net including MSDN.
What have you tried so far? Where is your code hanging up?
| | Member | | Join Date: Oct 2006
Posts: 64
| | | re: send a email and attach file in VB
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?
---------------------------------------------------------------- - Imports Microsoft.VisualBasic
-
Imports System.net.Mail
-
-
Public Class SendEmail
-
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
-
As String, ByVal strSubject _
-
As String, ByVal strMessage _
-
As String, ByVal fileList() As String)
-
'This procedure takes string array parameters for multiple recipients and files
-
Try
-
For Each item As String In strTo
-
'For each to address create a mail message
-
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
-
'MailMsg.BodyEncoding = Encoding.Default
-
MailMsg.Subject = strSubject.Trim()
-
MailMsg.Body = strMessage.Trim() & vbCrLf
-
MailMsg.Priority = MailPriority.High
-
MailMsg.IsBodyHtml = True
-
-
'attach each file attachment
-
For Each strfile As String In fileList
-
If Not strfile = "" Then
-
Dim MsgAttach As New Attachment(strfile)
-
MailMsg.Attachments.Add(MsgAttach)
-
End If
-
Next
-
-
'Smtpclient to send the mail message
-
Dim SmtpMail As New SmtpClient
-
SmtpMail.Host = "10.10.10.10"
-
SmtpMail.Send(MailMsg)
-
Next
-
'Message Successful
-
Catch ex As Exception
-
ex.ToString()
-
End Try
-
End Sub
-
-
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
-
As String, ByVal strSubject _
-
As String, ByVal strMessage _
-
As String, ByVal file As String)
-
'This procedure overrides the first procedure and accepts a single
-
'string for the recipient and file attachement
-
Try
-
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
-
'MailMsg.BodyEncoding = Encoding.Default
-
MailMsg.Subject = strSubject.Trim()
-
MailMsg.Body = strMessage.Trim() & vbCrLf
-
MailMsg.Priority = MailPriority.High
-
MailMsg.IsBodyHtml = True
-
-
If Not file = "" Then
-
Dim MsgAttach As New Attachment(file)
-
MailMsg.Attachments.Add(MsgAttach)
-
End If
-
-
'Smtpclient to send the mail message
-
Dim SmtpMail As New SmtpClient
-
SmtpMail.Host = "10.10.10.10"
-
SmtpMail.Send(MailMsg)
-
Catch ex As Exception
-
'Message Error
-
End Try
-
End Sub
-
End Class
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB 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. |  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB Quote:
Originally Posted by qfchen 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? 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.
| | Member | | Join Date: Oct 2006
Posts: 64
| | | re: send a email and attach file in VB
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"
| | Member | | Join Date: Oct 2006
Posts: 64
| | | re: send a email and attach file in VB
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. - Imports Microsoft.VisualBasic
-
Imports System.net.Mail
-
-
Public Class SendEmail
-
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
-
As String, ByVal strSubject _
-
As String, ByVal strMessage _
-
As String, ByVal fileList() As String)
-
'This procedure takes string array parameters for multiple recipients and files
-
Try
-
For Each item As String In strTo
-
'For each to address create a mail message
-
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
-
'MailMsg.BodyEncoding = Encoding.Default
-
MailMsg.Subject = strSubject.Trim()
-
MailMsg.Body = strMessage.Trim() & vbCrLf
-
MailMsg.Priority = MailPriority.High
-
MailMsg.IsBodyHtml = True
-
-
'attach each file attachment
-
For Each strfile As String In fileList
-
If Not strfile = "" Then
-
Dim MsgAttach As New Attachment(strfile)
-
MailMsg.Attachments.Add(MsgAttach)
-
End If
-
Next
-
-
'Smtpclient to send the mail message
-
Dim SmtpMail As New SmtpClient
-
-
SmtpMail.Host = frmSetting.txtSMTP.Text.ToString
-
SmtpMail.Send(MailMsg)
-
Next
-
'Message Successful
-
Catch ex As Exception
-
MsgBox("source:" & ex.Source & ", " & "message:" & ex.Message)
-
End Try
-
End Sub
-
End Class
-
-
Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
-
Dim SendEmail As New SendEmail
-
Dim SendTo(2) As String
-
Dim FileAttach(2) As String
-
Dim strSubject As String
-
Dim strMessage As String
-
Dim strFrom As String
-
SendTo(0) = txtEmailTo1.Text.ToString
-
SendTo(1) = txtEmailTo2.Text.ToString
-
-
FileAttach(0) = "c:\temp\888 success Story Jabil.pdf"
-
FileAttach(1) = "c:\temp\888 success Story Jabil.pdf"
-
-
strSubject = txtSubject.Text.ToString
-
-
strMessage = "Email Messge Text" 'The body encoding is set to HTML
-
strFrom = txtFrom.Text.ToString
-
SendEmail.SendEmailMessage(strFrom, SendTo, strSubject, strMessage, FileAttach)
-
End Sub
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB
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.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB Quote:
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.
| | Member | | Join Date: Oct 2006
Posts: 64
| | | re: send a email and attach file in VB
but I don't know why it show me this error. Quote:
:"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. - Catch ex As Exception
-
MsgBox("source:" & ex.Source & ", " & "message:" & ex.Message)
-
End Try
-
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB
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.
| | Member | | Join Date: Oct 2006
Posts: 64
| | | re: send a email and attach file in VB
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: Quote:
source:System, message:The parameter 'address' cannot be an empty string, Parameter name:address
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,791
| | | re: send a email and attach file in VB
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.
|  | Similar Visual Basic .NET bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,567 network members.
|