2

Possible Duplicate:
How can I lower the spam score of my email message?

I have this c# code that can send lots of emails to people . but emails that I sent has classified as spam .what should I do .is there any change that I should apply to my code?I try to get email addresses from sql server data base .and My code can attach one file .

Entities context = new Entities();
var query = from c in context.Emails select c.EmailAddress;
MailMessage mail = new MailMessage();
Regex sample = new Regex(@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*
                                  @[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");
int total = 0;//number of all emails
int count = 0;//counter for putting interrupt between each 10 sending
int failed = 0;//number of failed sending
int success = 0;//number of successful sending

double totalsize = 0;//size of attachment file

if (FileUpload1.HasFile)
{
    mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
    foreach (Attachment attachment in mail.Attachments)
    {
        string size =attachment.ContentStream.Length.ToString ();
        totalsize=Convert .ToDouble (size);
    }
 }
foreach (var c in query)
{
    if (count == 10)
    {
        Thread.Sleep(10000);
        count = 0;
    }
    mail.From = new MailAddress("hadi@myhost.com");
    mail.Bcc.Add(new MailAddress(c.ToString()));
    mail.Subject = "hello";
    mail.IsBodyHtml = true;

    mail.Body = FCKeditor1.Value.ToString();

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "localhost";
    smtp.Port = 25; 
    if ((sample.IsMatch(c.ToString())) && (sample .IsMatch (mail .From .ToString ())) && (totalsize<1000000))
    {
      try
        {
            smtp.Send(mail);
            //Response.Write("email has sent to " + c.ToString());
            success++;

        }
        catch 
        {
            //Response.Write("email has not sent to " + c.ToString());
            failed++;
        }
        count++;
    }
    total++;
} 
Community
  • 1
  • 1
Hadi Nemati
  • 547
  • 3
  • 11
  • 23
  • 1. Are you spamming people? 2. I notice you're using localhost as an SMTP, but the from is Yahoo. If localhost is not delegating to Yahoo, that can cause problems. – Matthew Flaschen Apr 07 '12 at 05:57

1 Answers1

2

You don't have to do anything with your code to make it not spam. All you have to do is to make sure you are sending email from a host that should not have "Open Relay" means not everybody can send email from it.

Send your email from proper server and with proper email signature , so that when receiver checks back for authentication they should authenticate your email source server and signature validated from your email host.

Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101