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

 string emailmessage = @"<html xmlns='http://www.w3.org/1999/xhtml'>" + Emailmessage + "</html>";

        MailMessage Mailobj = new MailMessage(txtFrom.Text, EmailIds);
        MailBody = @"<html xmlns='http://www.w3.org/1999/xhtml'>" + 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 = "bulkemail.smtp.com";
        SMTPMail.Port = 2525;
        SMTPMail.EnableSsl = false;
        SMTPMail.Send(Mailobj);

this is my send email code..its work fine  but if i give wrong email like  aa@gg.m....no 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)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
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";
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
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
Hi...

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\-]+\" +
 @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
 Regex objRegex = new Regex(strRegex);
 if (objRegex.IsMatch(pstrEmailAddress))
 {
 return true;
 }
else
 {
 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:

<system.net>
 
<mailSettings>
   
<smtp deliveryMethod="Network" from="you@yourdomain.com">
     
<network host="localhost" port="25" userName="user" password="pass" />
   
</smtp>
 
</mailSettings>
</system.net>

UPDATE:

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 http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx on MSDN.