How can send mail through VB.Net or C# ?

For a developer, e-mail is an effective way to allow an application to send files or reports to users and to notify users of problems or events.Here i am going to explain how can send mail through VB.net or C#.

To send an email you need two objects:

1.MailMessage object: with this object you can create your message that you want to send.

2.SmtpClient object: with this object you can  send your MailMessage to the recipients.

Before you create a function that send mail ,you need to import System.Net.Mail :

[VB.Net]

Imports System.Net.Mail

[C#]

using System.Net.Mail;

Now your code is ready for creating SendMail function

[VB.Net]

 Private Sub SendMail()
        Dim Message As MailMessage = New MailMessage()
        Message.From = New MailAddress("Arman@codingtips.net", "Arman")
        Message.To.Add(New MailAddress("Mehran@codingtips.net", "Mehran"))
        Message.To.Add(New MailAddress("Mike@codingtips.net", "Mike"))
        Message.To.Add(New MailAddress("JM@codingtips.net", "JM"))
        Message.Subject = "Quarterlydata report."
        Message.Body = "See the attached spreadsheet."
        Message.Attachments.Add(New Attachment("C:\Test.txt"))

        Dim sc As SmtpClient = New SmtpClient("smtp.gmail.com")
        sc.Port = 587
        sc.EnableSsl = True
        sc.Credentials = New NetworkCredential("Username", "Password")
        sc.Send(Message)

    End Sub

[C#]

private void SendMail()
{
    MailMessage Message = new MailMessage();
    Message.From = new MailAddress("Arman@codingtips.net", "Arman");
    Message.To.Add(new MailAddress("Mehran@codingtips.net", "Mehran"));
    Message.To.Add(new MailAddress("Mike@codingtips.net", "Mike"));
    Message.To.Add(new MailAddress("JM@codingtips.net", "JM"));
    Message.Subject = "Quarterlydata report.";
    Message.Body = "See the attached spreadsheet.";
    Message.Attachments.Add(new Attachment("C:\\Test.txt"));

    SmtpClient sc = new SmtpClient("smtp.gmail.com");
    sc.Port = 587;
    sc.EnableSsl = true;
    sc.Credentials = new NetworkCredential("Username", "Password");
    sc.Send(Message);

}

Caution:

SmtpClient.port: it depends on the server that you want to use it to send mail.

SmtpClient .Host: it depends on the server that you want to use it to send mail. (here I used “smtp.gmail.com”)

SmtpClient.EnableSsl: If your server is using the SSL you can user this option.

Advertisement

How can create Array in VB.Net & C# ?

ms_visual_studio    There are different ways that you can create array , here I am going to explain them for you
1.You can supply the array size when you are declaring it , as the following example shows.

[VB.NET]

        Dim cargoWeights(10) As Double 

        Dim atmospherePressures(2, 2, 4, 10) As Short 

        Dim inquiriesByYearMonthDay(20)()() As Byte

[C#]

        double[] cargoWeights = new double[11];

        short[,,,] atmospherePressures = new short[3, 3, 5, 11];

        byte[][][] inquiriesByYearMonthDay = new byte[21][][];

2.You can also use a New clause to supply the size of an array
 when it’s created, as the following example shows.

[VB.NET]

          cargoWeights = New Double(10) {}

          atmospherePressures = New Short(2, 2, 4, 10) {}

          inquiriesByYearMonthDay = New Byte(20)()() {}

[C#]

          cargoWeights = new double[11];

          atmospherePressures = new short[3, 3, 5, 11];

          inquiriesByYearMonthDay = new byte[21][][];