Solved

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

Posted on 2006-11-20
3
245 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now