Solved

VB.net Windows App Sending mail

Posted on 2014-04-09
2
365 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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