Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am sending mail through C# but it goes to junk folder.

Following is the code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;
using System.Net;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class wwwbeta_Default : System.Web.UI.Page
{
    string EmailTo = string.Empty;
    string EmailFrom = string.Empty;
    string Body = string.Empty;
    string BCC = string.Empty;
    string Subject = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsend_Click(object sender, EventArgs e)
    {
        EmailFrom = hdnEmail.Value;
        EmailTo = "reciever mail";
        Subject = "test";
        Body = hdnName.Value + " with EmailID " + hdnEmail.Value + " sends a message to you.<br /> The Message is following:" + hdnMessage.Value;
     

        String Result = SendMail(EmailFrom, EmailTo, BCC, Subject, Body, true);
        if (Result == "MailSent")
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", "showpopupAfterEmail();", true);
        }
    }
    public String SendMail(String MailFrom, String MailTo, String BCC, String Subject, String Body, Boolean IsHtmlBody)
    {
        MyMail.From = new MailAddress(MailFrom);
       
        String[] Tos = MailTo.Split(';');
        foreach (String To in Tos)
        {
            MyMail.To.Add(To);
        }
        if (BCC != "")
        {
            MyMail.Bcc.Add(BCC);
        }
        MyMail.Subject = Subject;
        MyMail.Body = Body;
       
      

        return SendMail();
    }
    String Output;
    MailMessage MyMail = new MailMessage();

    #region enum EnmMailResult
    public enum EnmMailResult
    {
        MailSent
    }
    #endregion
    #region String SendMail()
    String SendMail()
    {
        MyMail.Priority = MailPriority.Normal;
        try
        {
            SetSMTPCredentials(MyMail);
            Output = EnmMailResult.MailSent.ToString();
        }
        catch (Exception ex)
        {
            Output = ex.Message;
        }
        return Output;
    }
    #endregion

    public static void SetSMTPCredentials(MailMessage mailMsg)
    {
        String SMTPAddress = "";
        String SMTPUserName = "";
        String SMTPPassword = "";
        Int32 SMTPPort = 0;

        //Change Email format here for all emails
        StringBuilder sb = new StringBuilder();
        if (mailMsg.IsBodyHtml)
        
       
            sb.Append(mailMsg.Body);
            mailMsg.Body = sb.ToString();
      
        //End



        try
        {

            SMTPAddress = "address";
            SMTPUserName = "username";
            SMTPPassword = "password";
            SMTPPort = portno;



            SmtpClient smtpObj = new SmtpClient();
            smtpObj.Host = SMTPAddress;
            smtpObj.Port = SMTPPort;
            NetworkCredential netCred = new NetworkCredential(SMTPUserName, SMTPPassword);
            smtpObj.Credentials = netCred;
            // Execute stored procedure



            try
            {
                smtpObj.Send(mailMsg);

            }
            catch (Exception ex)
            {
                SMTPAddress = "address";
                SMTPUserName = "username";
                SMTPPassword = "password";
                SMTPPort = portno;
                smtpObj.Host = SMTPAddress;
                smtpObj.Port = SMTPPort;
                NetworkCredential netCred1 = new NetworkCredential(SMTPUserName, SMTPPassword);
                smtpObj.Credentials = netCred1;
                smtpObj.Send(mailMsg);
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {

        }
    }
}
Posted
Updated 16-Mar-12 17:34pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Mar-12 23:40pm    
As far as I know, this is not related to your code. This is about Outlook system administration. A system administrator usually can fix it by looking at your message and filter settings. Or the user receiving your e-mail messed up something in the personal e-mail filtering/rules.

If someone receives your generated e-mail, your code is already working.
--SA
RicardoArriaga 29-Jul-13 13:10pm    
I had the same problem, it was related with the size of the subject, have you tested sending emails with a small subject? Try it...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900