Library code snippets
Sending Email With an Attachment
By James Crowley, published on 12 Jan 2004
This code demonstrates how to send an email in VB.NET with an attachment. It currently attaches the file "C:\myattachment.zip" - in order to work, this file must obviously exist!
Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
oMsg.From = "noone@nobody.com"
oMsg.To = "someone@somewhere.com"
oMsg.Subject = "Email with Attachment Demo"
oMsg.Body = "This is the main body of the email"
Dim oAttch As MailAttachment = New MailAttachment("C:\myattachment.zip")
oMsg.Attachments.Add(oAttch)
SmtpMail.Send(oMsg)
Related articles
Related discussion
-
Why choose c# when there is Java
by Thaj06 (0 replies)
-
Change color while typing in rich text box
by Akhtar Hussain (0 replies)
-
Third party components to enhance User Interface of Windows based application
by Shaila14041981 (0 replies)
-
VB.NET DataGridView Sort
by bradhen (0 replies)
-
About Comparison in Asp.Net
by S_Rahul (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
this is the vb.net coding .......
note:- this is the coding which uses the gmail smtp server, u need to enter the valid email id and password of gmail, then only this will work...
if u have any pblm then ask me any time...
my id is:- k.ashish237@gmail.com
Imports System.Net.Mail
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress("sender email")
MyMailMessage.To.Add(TextBox1.Text)
MyMailMessage.Subject = "subject title"
MyMailMessage.Body = "enter the body text message that u want to send"
For counter = 0 To ListBox1.Items.Count - 1
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(ListBox1.Items(counter))
MyMailMessage.Attachments.Add(Attach)
Next
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("yourid@gmail.com", "id password")
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
MessageBox.Show("Email Sent")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try
End Sub
same coding with the tools of vb.net......
hope this will help u....
Imports System.Net.Mail
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress(TextBox3.Text)
MyMailMessage.To.Add(TextBox1.Text)
MyMailMessage.Subject = TextBox2.Text
MyMailMessage.Body = RichTextBox1.Text
For counter = 0 To ListBox1.Items.Count - 1
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(ListBox1.Items(counter))
MyMailMessage.Attachments.Add(Attach)
Next
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential(TextBox4.Text, TextBox5.Text)
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
MessageBox.Show("Email Sent")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try
End Sub
to krishna reddy..
hi krishna how r u...
i m making an application in vb.net to send email using smtp server... but it dosnt work... even it does not send mail to spam...
if it is possible, then plz send me the coding of ur application and the procedure... to me by my email id... this is my colg project and it didnt work, help me plz....
plzzzzzzzzzzzzzzzzzzzzzzzzzzzz
thank u....
i have to develop an application that can send emails to our clients through a vb.net application...
but da problm is dat i cant use a SMTP server... oderwise ther wudnt b ne problm @ all..!!!
help...![Sad [:(]](/emoticons/emotion-6.gif)
all u hav to do is add reference "System.web.dll" in ur project...
that u can do by :----
1) rite - clickin on ur project in da solution explorer and click on 'add reference'...
2) got to da 'COM' tab and add the dll i mentioned before...![Smiley Face [:)]](/emoticons/emotion-1a.gif)
njoi...!!!![Smiley Face [:)]](/emoticons/emotion-1a.gif)
Great Thanks
hi
you can keep that code any where its not a problem whether a webpage calls it or a windows app.
whats important is that it works on the .net framework.
do you mean by client side, that you use javascript, if yes, you can use AJAX to call a function from the server side, without any trip to the server.
Regards
I have seen this format before and I believe that format is for VB System not a Web App - I am not sure if I stated my question clearly enough.
I am using .NET
from the client side I want to be able to send an email and specify the email server username and password.
I will retry that code just to be sure - I posted the message a while back and cant remember much..
Dim objSender As SmtpMail
objSender.SmtpServer = "SMTPMAILSERVER"
Dim objMsg As New MailMessage
objMsg.From = "email@server.com"
objMsg.To = "email@server.com"
objMsg.Subject = "Test HTML"
objMsg.BodyFormat = MailFormat.Html
objMsg.Body = "<html><h1>hello</h1></html>"
''
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtsperver") = "SMTPMAILSERVER"
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "SMTPUSERNAME"
objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "SMTPPASSWORD"
''
objSender.Send(objMsg)
The code is simple and works fine with me!!!
I have tried to attach to oMsg.Body a richtexbox text.
I found that it doesn't work this way. I presume is because I have to send html formated text or plain text.
But still, I would like to send formated texts (from my app) in order to have well designed e-mails to my receivers.
How I amm supposed to accomplished that?
<An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll>
<Additional information: Could not access 'CDO.Message' object.>
Please Help!!!
Kinghenery87@aol.com
I can't get System.Web.Mail name space on my system. Is there any installation problem??
Ram Kinkar Pandey
PRamkinkar@pisoftek.com
Priya International Softek Limited
India
One thing about this code - I am trying to send with a specific SMTP server and Need to utilize the appropriate Authentication to use that SMTP server..
Any Ideas on how to do that ?
I know I can specify the SMTP server but how do I Specify the Authentication (username and password) obviously I might not want to send this in the clear..Can anyone help ..
First Add Reference to System.Web.Dll
Go to top Menu
-Project
-Add References
Select System.Web.dll
Use this Coding
oMsg.From = Me@Here.com"
oMsg.To = "He@There.com"
oMsg.Subject = "Dit is my Afrikaanse Boodskap"
oMsg.Body = "Ek wil dit bysit"
Dim oAttch As System.Web.Mail.MailAttachment = New System.Web.Mail.MailAttachment("C:\ftpfile.txt")
oMsg.Attachments.Add(oAttch)
System.Web.Mail.SmtpMail.Send(oMsg)
MessageBox.Show("Mail Message Send")
I am using XML Text reader in PDA application to read large XML file, its working in Emulator but not in the PDA device. SO if any body can give suggetion, I will be thankful.
Ramakrishna Tulluri
India
Do I double click the forum to enter this code?
')
')
AND PLEASE KEEP THE TUTORIALS ON THIS COMING!!
javascript:smilie('
javascript:smilie('
I am pretty new to VB.net programming and I seem to be missing a critical element to understanding how to use these code samples. For example the code below taken from this site does not work for me. VB underlines the two items surrounded by ** in the sample code below and says that "type Mail Attachement is not defined" also says that "name smtpMail is not declared"
What am I missing - each time I try to download sample code I run into the same types of issues????
Thanks!
Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
oMsg.From = "noone@nobody.com"
oMsg.To = "someone@somewhere.com"
oMsg.Subject = "Email with Attachment Demo"
oMsg.Body = "This is the main body of the email"
Dim oAttch As *MailAttachment = New MailAttachment("C:\myattachment.zip")
oMsg.Attachments.Add(oAttch)
SmtpMail.Send*(oMsg)
This thread is for discussions of Sending Email With an Attachment.