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.
SMTP Client
- Yahoo: smtp.mail.yahoo.com
- 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.

maharaja sPosted Sep 8, 2015, 6:03 AM
no it is not working and shows above msg
maharaja sPosted Sep 8, 2015, 6:03 AM
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required
maharaja sPosted Sep 8, 2015, 6:03 AM
no it is working
brgiht cbnPosted Mar 2, 2015, 10:18 AM
please i'm getting failure sending mail
Pratham JaiswalPosted Aug 31, 2014, 7:06 AM
I have got the following error for Yahoo login: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required".. Please help me..
Pratham JaiswalPosted Aug 31, 2014, 6:46 AM
I have got the following error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
yohanes sukardiPosted Jan 7, 2014, 6:24 AM
THANKS U very much....
Deeapk KaushikPosted May 16, 2013, 3:49 AM
Nice article, Thanks for sharing
shwetank suthareditedPosted Apr 3, 2013, 6:53 AMEdited Apr 3, 2013, 6:53 AM
Error in Yahoo . I can't send mail from Yahoo
Tina GargPosted Oct 22, 2012, 12:24 AM
Wow that a very nice article, Thanks for sharing.......
Nishant RoyPosted Oct 21, 2012, 10:57 PM
Thanks for sharing this nice article.