Solved

VB.net Windows App Sending mail

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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