Solved

VB.net Windows App Sending mail

Posted on 2014-04-09
2
358 Views
Last Modified: 2014-04-13
Hi
I use the VB.net code further down to send emails in my ASP.net web application.
I put the following in the web.config file. I want to now use this code to send mails
from Windows Forms app but there is obviously no web.config file. How do I set this
up in my Windows forms app

  <!-- Email **** Add the email settings to the <system.net> element -->
  <system.net>
    <mailSettings>
      <smtp from="info@online-excel.com">
        <network host="41xx" port="25" userName="info@online-excel.com" password="x"/>

      </smtp>
    </mailSettings>
  </system.net>
  <!-- Email **** Add the email settings to the <system.net> element -->

                    Dim sBody As String = oBuild_Email_Body_From_Query() & "<br />" & oBuild_Email_Body_From_Query2()
                    Dim objScheduleMail As New clsScheduleMail()
                    objScheduleMail.oSendScheduleMail("info@online-excel.com", "info@online-excel.com", "murbro9@yahoo.com", "", "GREAT Entries", sBody)

Imports System.Net.Mail

Public Class clsScheduleMail

    Public Function oSendScheduleMail(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String) As String
        Try

            ' Instantiate a new instance of MailMessage
            Dim mMailMessage As New MailMessage()

            ' Set the sender address of the mail message
            mMailMessage.From = New MailAddress(from)
            ' Set the recepient address of the mail message
            mMailMessage.To.Add(New MailAddress(recepient))

            ' Check if the bcc value is nothing or an empty string
            If Not bcc Is Nothing And bcc <> String.Empty Then
                ' Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(New MailAddress(bcc))
            End If

            ' Check if the cc value is nothing or an empty value
            If Not cc Is Nothing And cc <> String.Empty Then
                ' Set the CC address of the mail message
                mMailMessage.CC.Add(New MailAddress(cc))
            End If

            ' Set the subject of the mail message
            mMailMessage.Subject = subject
            ' Set the body of the mail message
            mMailMessage.Body = body

            ' Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = True
            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()
            ' Send the mail message
            mSmtpClient.Send(mMailMessage)

            oSendScheduleMail = "Mail Sent"
        Catch ex As Exception
            oSendScheduleMail = ex.Message
        End Try
    End Function

End Class
0
Comment
Question by:murbro
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39990107
Don't put settings in config file and change following line

Dim mMailMessage As New MailMessage()

to

Dim mMailMessage As New MailMessage(From, Recipient)
0
 

Author Closing Comment

by:murbro
ID: 39997610
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DataGridView does not show data rows correctly in vb.net 2013 3 31
Resolve Dependency Issues 4 47
What .NET URL re-routing tool did I use? 2 37
Exit the loop 4 37
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now