Click here to Skip to main content
12,953,014 members (38,405 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Getting smtp exception so that mail is not sending to reciever by means of below code

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace randompwdgeneration
	public partial class _Default : System.Web.UI.Page
		protected void Page_Load(object sender, EventArgs e)
		public string GeneratePassword()
			//Since I'm big on security, I wanted to generate passwords that contained numbers, letters and special
			//characters - and not easily hacked.
			//I started with creating three string variables.
			//This one tells you how many characters the string will contain.
			string PasswordLength = "12";
			//This one, is empty for now - but will ultimately hold the finised randomly generated password
			string NewPassword = "";
			//This one tells you which characters are allowed in this new password
			string allowedChars = "";
			allowedChars = "1,2,3,4,5,6,7,8,9,0";
			allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
			allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
			allowedChars += "~,!,@,#,$,%,^,&,*,+,?";
			//Then working with an array...
			char[] sep = { ',' };
			string[] arr = allowedChars.Split(sep);
			string IDString = "";
			string temp = "";
			//utilize the "random" class
			Random rand = new Random();
			//and lastly - loop through the generation process...
			for (int i = 0; i < Convert.ToInt32(PasswordLength); i++)
				temp = arr[rand.Next(0, arr.Length)];
				IDString += temp;
				NewPassword = IDString;
				//For Testing purposes, I used a label on the front end to show me the generated password.
				//lblProduct.Text = IDString;
			return NewPassword;
		protected void Button1_Click(object sender, EventArgs e)
				//create your variables
				string strSubject = "Your Password Has Been Reset";
				string strFromEmail = "";
				string strFromName = "Administrator";
				string strNewPassword = GeneratePassword().ToString(); //This is where you'd call the new password string.
					//create the new mail message[/color]
					MailMessage MailMsg = new MailMessage();
					//cretate the FROM[/color]
					MailMsg.From = new MailAddress(strFromEmail);
					//create the subject[/color]
					MailMsg.Subject = strSubject;
					//We obviously need to create the TO - otherwise it goes nowhere.
					MailMsg.To.Add(TextBox1.Text); //assuming there was a form to fill out and they put the right email address in.
					MailMsg.IsBodyHtml = true; //I decided to make it html - so I could format the text.
					MailMsg.Body = "The following email was sent to you by " + strFromName + ".";
					MailMsg.Body += "Apparently, you needed your password reset - So here it is: ";
					MailMsg.Body += "New Password " + strNewPassword + "";
					MailMsg.Body += "If you didn't request this, then - oops, you might wanna think about changing it, now. ";
					MailMsg.Body += "Click the following link to change your password:";
					MailMsg.Body += "";
					//utilizing SMTP (simple mail transfer protocol)
					SmtpClient smtp = new SmtpClient();
					smtp.Host = ""; //if my domain had a way of sending out emails.
					SmtpClient client = new SmtpClient();
					client.Port = 465;
					//client.EnableSsl = true;
					//client.Credentials = new System.Net.NetworkCredential
					//("username", "password");
					//client.EnableSsl = true;
					client.Host = " ";//
					//System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
					////This object stores the authentication values
					System.Net.NetworkCredential basicCrenntial =
					new System.Net.NetworkCredential("username", "password");
					//mailClient.UseDefaultCredentials = false;
					//mailClient.Credentials = basicCrenntial;
					client.Send(MailMsg); //send it
					Label1.Text = "Message Sent Successfully"; //tell the person that the email was sent.
				catch (Exception ex)
					Label1.Text = "Error: " + ex.ToString(); //or tell the person that they screwed up and it's all their fault.... or what the actual error was.
kindly help me
Posted 4-Jan-13 2:20am
Updated 4-Jan-13 2:29am
CHill60 4-Jan-13 8:32am
Please use the Improve question widget to post the exception details and let us know which line is generating the exception
Sandeep Mewara 4-Jan-13 11:03am
Can you please share across your error with us?
Slava Khristich 7-Jan-13 2:38am
Did you configure your SMTP server in web.config? Bad idea to do it in the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,389
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100