-
Apr 12th, 2005, 09:22 PM
#1
Thread Starter
Lively Member
VB.NET - Button_Click to send email
How do i code a button click to send email to a person based
on the txtName.Text, txtEmail.Text, txtSubject.Text and
txtMessage.Text input by the user?
How do i define by own from-name and from-email too?
-
Apr 12th, 2005, 09:56 PM
#2
Re: VB.NET - Button_Click to send email
you want to use the system.web.mail class.
more info on this is available at System.Web.Mail.
-
Apr 13th, 2005, 12:14 AM
#3
New Member
Re: VB.NET - Button_Click to send email
Hai,
This piece of code can be used to send mails with attachments. I'm sure it will help you a bit.
VB Code:
Dim email As New System.Web.Mail.MailMessage
' Setting the from,to, message and subject for the mail
email.To = "urfriend@sample.com"
email.From = "yourid@gg.com"
email.Body = "Sample message that to be send to your friend"
email.Subject = "Ha Guys"
' Adding the attachment to the mail
Dim Attach As String = "C:\02_Ranger.JPG"
Dim myAttachment As _
System.Web.mail.MailAttachment = _
New System.Web.mail.MailAttachment(Attach)
email.Attachments.Add(myAttachment)
email.BodyFormat = Web.Mail.MailFormat.Text
System.Web.Mail.SmtpMail.SmtpServer = "Put Mail Server Here"
Try
System.Web.Mail.SmtpMail.Send(email)
Catch ex As Exception
MessageBox.Show( _
ex.Message & vbNewLine & _
ex.Source & vbNewLine & _
ex.StackTrace, "Email Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Regards,
Brainstorming Guys
-
Apr 13th, 2005, 06:46 AM
#4
Hyperactive Member
Re: VB.NET - Button_Click to send email
This should do it for you.
VB Code:
Dim email As New MailMessage
Dim Attach As MailAttachment = _
New MailAttachment("C:\02_Ranger.JPG")
With email
.To = txtTo.Text
.From = txtFrom.Text
.Body = txtBody.Text
.Subject = txtSubject.Text
.Attachments.Add(Attach)
.BodyFormat = MailFormat.Text
End With
SmtpMail.SmtpServer() = txtMserver.Text
Try
SmtpMail.Send(email)
MessageBox.Show("Email was sent sucessfully", _
"Email Sent", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & _
ex.Source & vbNewLine & ex.StackTrace, _
"Send Mail Error", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End Try
Just remove the attachment lines if you don't need them. As an alternative you may want to use a Open File Dialog with the attachment so that it can be chosen dynamicaly/optionly instead of just being hard coded into your sub.
Oh yea for this to work as written you need to make this your first line of code (very top of the page).
Imports System.Web.Mail
Last edited by BukHix; Apr 13th, 2005 at 07:03 AM.
-
Apr 13th, 2005, 09:11 AM
#5
Member
Re: VB.NET - Button_Click to send email
Question:
How would you send a crystal report that your program generates? Can you do it on the fly, or would you have to save it then add it as your attachment?
-
May 26th, 2005, 08:40 PM
#6
Frenzied Member
Re: VB.NET - Button_Click to send email
myReport.ExportToDisk(CrystalDecisions.[Shared].ExportFormatType.WordForWindows, "c:\Temp\myDoc.doc")
Create a report object (myReport), export it to a word document ("c:\Temp\myDoc.doc") then email the path to the document as an attachment as previously explained in this thread. Works nicely.
Now I've got a question:
I have used this class to send mail. I can't work out how to CC and BCC to multiple addresses. I have had no problem CC'ing and BCC'ing to a single address but can't work out how to send to multiple addresses.
-
May 26th, 2005, 10:40 PM
#7
Frenzied Member
Re: VB.NET - Button_Click to send email
The answer was more obvious that what I thought - simply delimit multiple addresses with a semi-colon.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|