Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 = "admin@i1tech.com";
				string strFromName = "Administrator";
				string strNewPassword = GeneratePassword().ToString(); //This is where you'd call the new password string.
				try
				{
					//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 += "admin@i1tech.com";
					//utilizing SMTP (simple mail transfer protocol)
					SmtpClient smtp = new SmtpClient();
					smtp.Host = "webmail.i1tech.com"; //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 = "smtpout.asia.secureserver.net ";//smtpout.asia.secureserver.net
					//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
Edited 4-Jan-13 2:29am
60.7K
v2
Comments
CHill60 at 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 at 4-Jan-13 11:03am
   
Can you please share across your error with us?
Slava Khristich at 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
0 OriginalGriff 293
1 Sergey Alexandrovich Kryukov 281
2 RyanDev 65
3 nv3 60
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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