Solved

VB.net Windows App Sending mail

Posted on 2014-04-09
2
356 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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

7 Experts available now in Live!

Get 1:1 Help Now