Results 1 to 11 of 11

Thread: [02/03] Send email from form?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    [02/03] Send email from form?

    Hi every one

    Im hoping someone out there could offer me some help please.

    I have a program which is linked to a database, I have the program working perfectly as it saves and derives data from the database. The question I want to ask is simple would it be possible when the user clicks on 'Save' and the data gets saved in to the database, outlook opens and the information is taken from the textboxes and placed in body text field (where you normally write out your email), with the address bar automatically flled in ready for an email to be sent (If it sends it straight away that would be better.)

    Any help please? if its too much then please tell me.

    *It doesnt have to be outlook if I could send it from my form that would be just as good.

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Send email from form?

    Hi,

    Here you could already find some good information.

    http://support.microsoft.com/kb/314201

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Send email from form?

    Thanks for your help but it keeps breaking on the

    SmtpMail.Send(oMsg)

    line with the following error:

    An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

    Additional information: Could not create 'CDO.Message' object.

    Any ideas to maybe why? Thanks

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Send email from form?

    Hi,

    I think it's only for file attachments who have a path like in the example:

    vb Code:
    1. ' TODO: Replace with path to attachment.
    2.         Dim sFile As String = "C:\temp\Hello.txt"
    3.         Dim oAttch As MailAttachment = New MailAttachment(sFile, MailEncoding.Base64)
    4.  
    5.         oMsg.Attachments.Add(oAttch)
    6.  
    7.         ' TODO: Replace with the name of your remote SMTP server.
    8.         SmtpMail.SmtpServer = "MySMTPServer"
    9.         SmtpMail.Send(oMsg)

    So, what you can try is to sent a file located on your disk and sent it.

    Just to see if the error still exist.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Send email from form?

    Hi Sparrow I did create the text file in the C: temp directory, I think the problem might be on the SMTP server side however I did enter the correct name for that.

    Thanks

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Send email from form?

    Hi,

    What outgoing server did you used:

    vb Code:
    1. relay. ?????? . com ' this is the one like your email account!

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Send email from form?

    Huh sorry I dont understand, would it possible for you to create me a quick example so I can have a look at it please?


    I would appreciate it very much.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Send email from form?

    Any one

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [02/03] Send email from form?

    Regarding the error message you were getting you might want to have a read of this and do some research

    http://support.microsoft.com/kb/910360
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  10. #10
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Send email from form?

    Quote Originally Posted by frankwhite
    Huh sorry I dont understand, would it possible for you to create me a quick example so I can have a look at it please?


    I would appreciate it very much.
    Hi,

    Here's an example how to use System.Web.Mail.

    .Net Code:
    1. Imports System.Web.Mail
    2.  
    3. Public Class Form2
    4.     Inherits System.Windows.Forms.Form
    5.     Dim obj As _
    6.     System.Web.Mail.SmtpMail ' Variable which will send the mail
    7.     Dim Attachment As System.Web.Mail.MailAttachment    'Variable to store the attachments
    8.     Dim Mailmsg As New System.Web.Mail.MailMessage()    'Variable to create the message to send
    9.  
    10.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.  
    12.     End Sub
    13.  
    14.     Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    15.         'Show open dialogue box to select the files to attach
    16.         Dim Counter As Integer
    17.         OpenFileDialog.CheckFileExists = True
    18.         OpenFileDialog.Title = "Select file(s) to attach"
    19.         OpenFileDialog.ShowDialog()
    20.         For Counter = 0 To UBound(OpenFileDialog.FileNames)
    21.             lstAttachment.Items.Add(OpenFileDialog.FileNames(Counter))
    22.         Next
    23.     End Sub
    24.  
    25.     Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    26.         'Remove the attachments
    27.         If lstAttachment.SelectedIndex > -1 Then
    28.             lstAttachment.Items.Remove(lstAttachment.SelectedIndex)
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    33.         Dim Counter As Integer
    34.  
    35.         'Validate the data
    36.         If txtSMTPServer.Text = "" Then
    37.             MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
    38.             Exit Sub
    39.         End If
    40.  
    41.         If txtFrom.Text = "" Then
    42.             MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
    43.             Exit Sub
    44.         End If
    45.  
    46.         If txtTo.Text = "" Then
    47.             MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
    48.             Exit Sub
    49.         End If
    50.  
    51.         If txtSubject.Text = "" Then
    52.             MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
    53.             Exit Sub
    54.         End If
    55.  
    56.         'Set the properties
    57.  
    58.         obj.SmtpServer = txtSMTPServer.Text ' your local outgoing SMTP server
    59.         'Multiple recepients can be specified using ; as the delimeter
    60.         Mailmsg.To = txtTo.Text
    61.         Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
    62.  
    63.         'Specify the body format
    64.         If chkFormat.Checked = True Then
    65.             Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    66.         Else
    67.             Mailmsg.BodyFormat = MailFormat.Text
    68.         End If
    69.  
    70.         Mailmsg.Subject = txtSubject.Text
    71.         For Counter = 0 To lstAttachment.Items.Count - 1
    72.             Attachment = New MailAttachment(lstAttachment.Items(Counter))
    73.             Mailmsg.Attachments.Add(Attachment)
    74.         Next
    75.  
    76.         Mailmsg.Body = txtMessage.Text
    77.         obj.Send(Mailmsg)
    78.     End Sub
    79. End Class
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  11. #11
    New Member
    Join Date
    Oct 2008
    Posts
    2

    Re: [02/03] Send email from form?

    Quote Originally Posted by sparrow1
    Hi,

    Here's an example how to use System.Web.Mail.
    .Net Code:
    1. Imports System.Web.Mail
    2.  
    3. Public Class Form2
    4.     Inherits System.Windows.Forms.Form
    5.     Dim obj As _
    6.     System.Web.Mail.SmtpMail ' Variable which will send the mail
    7.     Dim Attachment As System.Web.Mail.MailAttachment    'Variable to store the attachments
    8.     Dim Mailmsg As New System.Web.Mail.MailMessage()    'Variable to create the message to send
    9.  
    10.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.  
    12.     End Sub
    13.  
    14.     Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    15.         'Show open dialogue box to select the files to attach
    16.         Dim Counter As Integer
    17.         OpenFileDialog.CheckFileExists = True
    18.         OpenFileDialog.Title = "Select file(s) to attach"
    19.         OpenFileDialog.ShowDialog()
    20.         For Counter = 0 To UBound(OpenFileDialog.FileNames)
    21.             lstAttachment.Items.Add(OpenFileDialog.FileNames(Counter))
    22.         Next
    23.     End Sub
    24.  
    25.     Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    26.         'Remove the attachments
    27.         If lstAttachment.SelectedIndex > -1 Then
    28.             lstAttachment.Items.Remove(lstAttachment.SelectedIndex)
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    33.         Dim Counter As Integer
    34.  
    35.         'Validate the data
    36.         If txtSMTPServer.Text = "" Then
    37.             MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
    38.             Exit Sub
    39.         End If
    40.  
    41.         If txtFrom.Text = "" Then
    42.             MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
    43.             Exit Sub
    44.         End If
    45.  
    46.         If txtTo.Text = "" Then
    47.             MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
    48.             Exit Sub
    49.         End If
    50.  
    51.         If txtSubject.Text = "" Then
    52.             MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
    53.             Exit Sub
    54.         End If
    55.  
    56.         'Set the properties
    57.  
    58.         obj.SmtpServer = txtSMTPServer.Text ' your local outgoing SMTP server
    59.         'Multiple recepients can be specified using ; as the delimeter
    60.         Mailmsg.To = txtTo.Text
    61.         Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
    62.  
    63.         'Specify the body format
    64.         If chkFormat.Checked = True Then
    65.             Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    66.         Else
    67.             Mailmsg.BodyFormat = MailFormat.Text
    68.         End If
    69.  
    70.         Mailmsg.Subject = txtSubject.Text
    71.         For Counter = 0 To lstAttachment.Items.Count - 1
    72.             Attachment = New MailAttachment(lstAttachment.Items(Counter))
    73.             Mailmsg.Attachments.Add(Attachment)
    74.         Next
    75.  
    76.         Mailmsg.Body = txtMessage.Text
    77.         obj.Send(Mailmsg)
    78.     End Sub
    79. End Class
    good job, thanks for good share

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width