Solved

What is the best way to send an email with .net 2.0?

Posted on 2006-11-20
3
247 Views
Last Modified: 2010-04-23
I am rebuilding a project that I had made a few years ago and would like to start using .net 2.0.  What object would be the best to use?  On the old project I used CDO.Message.

Thanks
0
Comment
Question by:GabicusC
3 Comments
 
LVL 3

Assisted Solution

by:schott19
schott19 earned 170 total points
ID: 17982071
Hi there,

this is a great site that i used when i was asking the same question..

http://www.systemnetmail.com/

gives you basic information as well as more complex stuff so will cover all you need to know.

A simple example from the site to send an email is:

 'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is a sample body"

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)


Regards

0
 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 160 total points
ID: 17982319
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 170 total points
ID: 17982414
Hi GabicusC;

Here is some sample code to show how to use Net.Mail

Note that this line needs to be changed in the sample code below to the smtp server you are using:

        mailSender = New SmtpClient("Your smtp-server.com", 25)


Imports System.Net.Mail

    Private Sub SendEmail(ByVal sender As String, _
        ByVal recipient As String, ByVal subject As String, _
        ByVal body As String, Optional ByVal attachmentString As String = "")

        Dim fromAddress As New MailAddress(sender)
        Dim toAddress As New MailAddress(recipient)
        Dim message As New MailMessage(fromAddress, toAddress)

        Dim mailSender As SmtpClient
        mailSender = New SmtpClient("Your smtp-server.com", 25)

        message.Bcc.Add(fromAddress)
        message.Subject = subject
        message.IsBodyHtml = False
        message.Body = body

        If Not attachmentString = "" Then
            Dim msgAttach As New Attachment(attachmentString)
            message.Attachments.Add(msgAttach)
        End If

        Try
            mailSender.Send(message)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Mail Not Sent")
        End Try

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        SendEmail("Fernando-Soto@mydomain.com", "Margarita-Rada@mydomain.com", _
            "This is a test", "This is the Body", "c:\Temp\pp1.pdf")

    End Sub


Fernando
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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