Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

24 Experts available now in Live!

Get 1:1 Help Now