Jump to content

How to send a simple mail in.net 2005


dhanjish

Recommended Posts

Here is an example that I wrote earlier in C# to send e-mails from Gmail accounts, I think that will be useful for someone :

// Using directivesusing System;using System.Net;// System.Net.Mail for SmtpClient and MailMessageusing System.Net.Mail;namespace gmailSender{ public class GmailSenderClass {		public static void Main(string[] args)		{			  try			  {					 // parameters for MailMessage are, FROM:, TO:, Subject: and Body:					 MailMessage message_ = new					 MailMessage("Example@gmail.com", "Example@hotmail.com", "Hi this is subject", 					 "Hi this is body");					 // This is to send text e-mails					message_.IsBodyHtml = false;					// parameters for NetworkCredential is username and password 					NetworkCredential mailAuthentication= new					NetworkCredential("Example@gmail.com", "YourPassword");					 // SmtpClient parameters are: host and port (you can see I have used the gmail ones)					SmtpClient smtp_ = new SmtpClient("smtp.gmail.com", 587);					// We don't want to use default credentials because we wrote ours above					smtp_.UseDefaultCredentials = false;					// Gmail requires SSL to be enabled so we set it to true					smtp_.EnableSsl = true;					// We pass the credentials that we have created to SmtpClient object					smtp_.Credentials = mailAuthentication;					// We happily send our message :D					smtp_.Send(message_);			  }	 			  catch(Exception e)			  {					Console.WriteLine("Message: {0}", e.Message + Environment.NewLine);			  }		}   }}

Link to comment
Share on other sites

Hi all,Thanks for ur reply.I followed the following code and it wrkd Dim str As String lab.Visible = False MailMessage = New System.Net.Mail.MailMessage Dim username As String = "name" Dim pswd As String = "pswd" Dim credentials As New NetworkCredential("hostaddress", "password") client = New SmtpClient MailMessage.To.Add("daa@rediffmail.com") MailMessage.From = New MailAddress("gg@gmail.com") MailMessage.Subject = "Test" MailMessage.Body = "Hai hw r u ? " client.Host = "host address" Try client.Credentials = credentials client.Send(MailMessage) Catch ex As Exception If (ex.Message = "The remote name could not be resolved:'mail@divisionalforesttcr.org' ") Then str = " Failed!! your mail has not been sent because of some technical reasons " ' Response.Write(str) Else str = ex.ToString() ' Response.Write(str) End If End TryThanks for your support

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...