ASP.NET - emailsend find error mail - Asked By msakt on 31-May-12 04:08 AM

 string emailmessage = @"<html xmlns=''>" + Emailmessage + "</html>";

        MailMessage Mailobj = new MailMessage(txtFrom.Text, EmailIds);
        MailBody = @"<html xmlns=''>" + Emailmessage + "</html>";
        Mailobj.Subject = txtSubject.Text;
        Mailobj.Body = emailmessage;
        Mailobj.IsBodyHtml = true;
        SmtpClient SMTPMail = new SmtpClient();
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailUserName"].ToString(), ConfigurationManager.AppSettings["MailPassword"].ToString());
        SMTPMail.Credentials = nc;
        SMTPMail.Host = "";
        SMTPMail.Port = 2525;
        SMTPMail.EnableSsl = false;

this is my send email code..its work fine  but if i give wrong email like error display and reduce email count.....

now i want if give wrong email  dont send email for particular id and show display incorrect mailid
Jitendra Faye replied to msakt on 31-May-12 04:19 AM
Try this code-

For sending mail you have to use MailMessage Class.

you can include image in message body-

follow this code

protected void btnSubmit_Click(object sender, EventArgs e)
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body. 
myimage.ContentId = "companylogo";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
catch (Exception ex)


Neha Garg replied to msakt on 31-May-12 04:32 AM

before sending the mail just validate the Email Address by using the below code:

public static bool isValidEmail(string pstrEmailAddress)
 string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
 @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
 Regex objRegex = new Regex(strRegex);
 if (objRegex.IsMatch(pstrEmailAddress))
 return true;
 return false;

Somesh Yadav replied to msakt on 31-May-12 04:47 AM

Make sure you're pointing to an SMTP service on your production server, it may not work on "localhost" as it does on your development machine. And pinging the server doesn't really tell you if it has SMTP enabled.

I generally prefer setting up SMTP for my sites in web.config:

<smtp deliveryMethod="Network" from="">
<network host="localhost" port="25" userName="user" password="pass" />


If your code is working on your development machine, and it fails on the server with the same configuration, then there's probably something blocking. I would suggest trying to play around with a simple implementation that does nothing but test the servers SMTP configuration. You may want to try the <smtp deliveryMethod="SpecifiedPickupDirectory">, it's quite helpful when testing code that sends out emails. See the on MSDN.