Send Mail Using Gmail, Yahoo Account in VB.Net and C#.Net

In this article I am going to show how to send mail from Gmail or Yahoo using VB.Net & C# without logging into your Gmail account.

Send-Mail-using-Gmail-Yahoo.jpg

SMTP Client

  1. Yahoo: smtp.mail.yahoo.com
  2. Gmail: smtp.gmail.com

VB.NET Code

  Imports System.Net

 

Public Class Form1

 

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

        OpenFileDialog1.ShowDialog()

    End Sub

 

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        txtatt.Text = OpenFileDialog1.FileName.ToString

    End Sub

 

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

        Dim str As String = ""

        Dim state As Boolean

 

        If Microsoft.VisualBasic.Right(txtfrom.Text, 9) = "gmail.com" Then

            str = "smtp.gmail.com"

            state = True

        ElseIf Microsoft.VisualBasic.Right(txtfrom.Text, 9) = "yahoo.com" Then

            str = "smtp.mail.yahoo.com"

            state = False

        End If

        Dim From As Mail.MailAddress = New Mail.MailAddress(txtfrom.Text)

        Dim [To] As Mail.MailAddress = New Mail.MailAddress(txtto.Text)

        Dim Message As Mail.MailMessage = New Mail.MailMessage(From, [To])

        Message.Subject = txtsub.Text

        Message.Body = txtmess.Text

        Message.Attachments.Add(New Mail.Attachment(OpenFileDialog1.FileName.ToString))

        Dim client = New Mail.SmtpClient(str)

        client.EnableSsl = state

        client.Credentials = New System.Net.NetworkCredential(txtfrom.Text, txtpass.Text)

        client.Send(Message)

        MsgBox("Send Email Successfully")

    End Sub

 

End Class

C#.NET Code
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net;

 

namespace SendEmailCS

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

                     

         public void Button1_Click(System.Object sender, System.EventArgs e)

        {

              string str="";

            Boolean  state=false;

 

            if (right (txtfrom .Text ,9)=="gmail.com")

            {

                 str = "smtp.gmail.com";

                 state =true ;

            }

            else if (right (txtfrom .Text ,9)=="yahoo.com")

            {

                 str = "smtp.mail.yahoo.com";

                  state = false ;

            }

                               

            System.Net.Mail.MailAddress From = new System.Net.Mail.MailAddress(txtfrom.Text);

            System.Net.Mail.MailAddress To = new System.Net.Mail.MailAddress(txtto.Text);

            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(From, To);

            Message.Subject = txtsub.Text;

            Message.Body = txtmess.Text;

            Message.Attachments.Add(new System.Net.Mail.Attachment(openFileDialog1.FileName.ToString()));

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(str);

            client.EnableSsl = Convert .ToBoolean (state );

            client.Credentials = new System.Net.NetworkCredential(txtfrom.Text, txtpass.Text);

            client.Send(Message);

            MessageBox.Show("Send Email Successfully");

                  }

      

        public  string right(string param, int length)

        {

            string result = param.Substring(param.Length - length, length);

            return result;

        }

 

        private void Label5_Click_1(object sender, EventArgs e)

        {

            openFileDialog1.ShowDialog();

        }

 

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)

        {

            txtatt.Text = openFileDialog1.FileName.ToString();

        }

       

    }

}


This article helps you to send an Email using a Gmail or Yahoo acount.

Up Next
    Ebook Download
    View all
    Learn
    View all