Sending Emails from Google Server

Some time we need to send mails to others from our application, and fro this we need to use some free email server like GMAIL. here is the code using that we can use the gmail mail server to send the mails. I am simply specifying the g mail SMTP server name and the Port number in the code.. take a look…



MailMessage mailMessage = new MailMessage();

mailMessage.To.Add(”test@test.com”);

mailMessage.Subject = “Test”;

mailMessage.Body = “This is a test mail”;

mailMessage.IsBodyHtml = true;

// Create the credentials to login to the gmail account associated with my custom domain

string sendEmailsFrom = “abc@xyz.com”;

string sendEmailsFromPassword = “password”;


NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);

SmtpClient mailClient = new SmtpClient(”smtp.gmail.com”, 587);

mailClient.EnableSsl = true;

mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

mailClient.UseDefaultCredentials = false;

mailClient.Timeout = 30000;

mailClient.Credentials = cred;

mailClient.Send(mailMessage);



Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA


Comments

Author: Dotnet Developer-201501 Jul 2009 Member Level: Gold   Points : 1

Dear Sir

pl change this code .vb

how to include imports web.net

pl change this error check jpg file here

Author: Anil Kumar Pandey01 Jul 2009 Member Level: Diamond   Points : 1

hi,

u can use the Code converter available in DNS. and for that error please import the System.Net.Mail; name space

like

Imports System.Net.Mail;

Thanks

Author: Dotnet Developer-201501 Jul 2009 Member Level: Gold   Points : 1

Dear Sir

thanks ur reply and ur help very thankful
advantages of the code

following error comes

A from address must be specified.

Author: Anil Kumar Pandey01 Jul 2009 Member Level: Diamond   Points : 1

hi,
please specify the form address there..
like this


Message.From.Add(address);

Author: Dotnet Developer-201501 Jul 2009 Member Level: Gold   Points : 2



Dim mailMessage As MailMessage = New MailMessage()

mailMessage.To.Add("test@test.com")

mailMessage.Subject = "Test"

mailMessage.Body = "This is a test mail"


mailMessage.IsBodyHtml = True

' Create the credentials to login to the gmail account associated with my custom domain

Dim sendEmailsFrom As String = "abc@xyz.com"

Dim sendEmailsFromPassword As String = "password"

Dim address1 As String = "Wesdsdfsdf"



Dim cred As Net.NetworkCredential = New Net.NetworkCredential(sendEmailsFrom, sendEmailsFromPassword)

Dim mailClient As SmtpClient = New SmtpClient("smtp.gmail.com", 587)

mailClient.EnableSsl = True

mailClient.DeliveryMethod = SmtpDeliveryMethod.Network

mailClient.UseDefaultCredentials = False

mailClient.Timeout = 30000

mailClient.Credentials = cred

Message.From.Add(address);

mailClient.Send(mailMessage)


message convert mailMessage ?
pl change this code

Author: Dadasaheb k. Chobhe22 Jul 2009 Member Level: Bronze   Points : 1

Greet Sir,
I was tried at my level. But it gives error remote name could not resolve : smtp.gmail.com.
pls give me reply.

Author: Abhay Kumar Raut14 Aug 2009 Member Level: Gold   Points : 1

Hi Anil,

good one .

keep it up.

contribute more.

Thanks and Regards
Abhay



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: