Proposed Answer asp.net send mail using C#

  • Wednesday, December 28, 2011 5:48 AM
     
      Has Code

     

    Can u plz help me, what is the meaning of this error and help me how to retify.

    Parameter name: addresses

     

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: addresses

    Source Error:

    Line 83:         MailMessage msgMail = new MailMessage();
    Line 84:         msgMail.From = new MailAddress(_mailFrom);
    Line 85:         msgMail.To.Add(_mailTo); // Change this 
    Line 86:         msgMail.Subject = _mailSubject;
    Line 87:         msgMail.Body = _mailBody;


    Source File: e:\wwwRoot\7hats\App_Code\Email.cs    Line: 85

    Stack Trace:

    [ArgumentNullException: Value cannot be null.
    Parameter name: addresses]
       System.Net.Mail.MailAddressCollection.Add(String addresses) +1105799
       Email.SendEmail() in e:\wwwRoot\7hats\App_Code\Email.cs:85
       _Default.btnSignup_Click(Object sender, EventArgs e) in e:\wwwRoot\7hats\Default.aspx.cs:38
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    



    Version Information: Microsoft .NET Framework Version:2.0.50727.4963; ASP.NET Version:2.0.50727.4955

     

    • Edited by praveen08 Wednesday, December 28, 2011 5:49 AM
    •  

All Replies

  • Wednesday, December 28, 2011 6:53 AM
     
     Proposed Answer

    Hi Praveen,

    The value of Address has to set, This is the reason you are getting a null value exception here,you have to replace _mailto and _mail from with the email ID's and make sure that you have cofigured your SMTP host name .

    MailMessage msgMail = new MailMessage();
    msgMail.From="from e-mail";//give the from email ID here
    msgMail.To="to e-mail";//Give the To email ID here
    msgMail.Subject="Message Subject";
    msgMail.Body="Message Body";

    Another way of doing the same is

    System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
    System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText");

    Have a nice Day!

    Jayesh Patel

     

     

    • Proposed As Answer by jayesh1990 Wednesday, December 28, 2011 8:33 AM
    •  
  • Wednesday, December 28, 2011 9:37 AM
     
     

    How to give i can't understand  pls tell me clearly, actually i am newly stated coding. i don't have server management,

     


    Default.aspx.cs

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Net.Mail;
    using System.Net;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblMsg.Text = string.Empty;
        }
        protected void btnSignup_Click(object sender, EventArgs e)
        {
            Email lEmail = new Email();
            //Set mail parameters
            lEmail.MailFrom = txtEmail.Text;
            lEmail.MailBody = "The user " + txtEmail.Text + " has requested to signup";
            lEmail.MailSubject = "SignUp";
            //Send Email
            lEmail.SendEmail();
            lblMsg.Text = "Thank You for Signup";
            txtEmail.Text = string.Empty;
        }
    }

     

    Default.aspx

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Welcome to 7Hats </title>
    <link href="css/style.css" type="text/css" rel="stylesheet"  />

    <script type="text/JavaScript">
    <!--
        function validateForm() {

            var x = document.getElementById("txtEmail").value;
            var atpos = x.indexOf("@");
            var dotpos = x.lastIndexOf(".");
            if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
                alert("Please enter a valid e-mail address");
                var obj = document.getElementById("lblMsg");
                obj.innerHTML = "";
                return false;
            }
        }


    //-->
    </script>
    <script src="js_new/iepng.js" type="text/javascript"></script>

    <script src="js_new/jquery.bxSlider.min.js" type="text/javascript"></script>
    <script src="js_new/superfish.js" type="text/javascript"></script>
    <style>
            .msg_button_class
                {
                   background-image: url(msg_button_1.jpg);
                   font-family:Tahoma;
                   font-size:11px;
                   font-weight:bold;
                   color:#FFFFFF;
                   border:1px solid #DDDDDD;
                   height:22px;
                }
        </style>
    </head>

    <body>
    <div class="wrapper">
    <div class="header">
    <div class="logo">
      <img src="images/logo.png" alt="logo" />
      </div>
      <div class="navi">
        <ul>
          <li ><a href="index.html" class="active" >Home</a></li>
          <li class="line">&nbsp;</li>
          <li><a  href="aboutus.html">About us</a></li>
          <li class="line">&nbsp;</li>
          <li><a href="whyhats.html">Why 7 Hats?</a></li>
          <li class="line">&nbsp;</li>
          <li><a href="ourservices.html"  >Our Services</a></li>
          <li class="line">&nbsp;</li>
          <li><a href="group.html">Our Process</a></li>
          <li class="line">&nbsp;</li>
          <li><a href="reports.html">Reports</a></li>
          <li class="line">&nbsp;</li>
          <li><a href="contact.html">Contact Us</a></li>
        </ul>
        </div>
    </div><!-- header ends here-->
    <div class="banner">
    <div class="banner_left">
    <div class="banner_heading">Employer Initiated<br />
    Verification Services</div>
    </div>
    <div class="banner_right">
    <img src="images/banner_img.png" class="banner_img" />
    </div>

    </div><!-- banner ends here-->

    <div class="page_content">
    <div class="divider"></div>
    <div class="blue_bar">
    </div>
    <div class="main_content">
    <div class="page_content_left">
    <div class="top_box"></div>
    <div class="left_txt_content">
    <div class="left_txt_heading">7 HATS Services  – an ISO 9001:2008</div>
    <p>certified organization was established to meet the HR related requirements for every corporate spread on a Pan India Level. We proudly present our Background Verification Services.&nbsp;<a href="#">More...</a></p>
    <img src="images/scanpast_logo.png" />
    <p>ScanPast is a single-window Background Authentication platform through which HR can initiate, execute, record and manage Background Authentication as a process.&nbsp;<a href="#">More...</a></p>
    </div>
    </div><!-- end page_content_left -->
    <div class="page_content_right">
    <div class="flow_diagram">
    <div class="left_flow">
    <ol>
    <li><span class="bold">Employment </span> Verification</li>
    <li><span class="bold">Educational </span>Verification</li>
    <li><span class="bold">Address </span>Verification</li>
    <li><span class="bold">Database  </span>Checks</li>
    </ol>
    </div>
    <div class="right_flow">
    <ol>
    <li><span class="bold">Site </span> Visits</li>
    <li><span class="bold">Police/Court </span>Verification</li>
    <li><span class="bold">Drug Abuse </span>Checks </li>
    <li><span class="bold">Passport/Licence  </span>Verification</li>
    </ol>
    </div>
    </div>
    </div><!-- page_content_right -->
    </div>
    </div><!-- end page_content -->
    <div class="footer">
    <div class="footer_content">
    <div class="news_subscribe">
    <div class="heading_news">
    <span>Subscribe</span> 7Hats Newsletter
    </div>
    <div id="contact_form" class="newsletter">
                   <form method="post" name="myForm" id="myForm" runat="server">
                       
                        <asp:TextBox ID ="txtEmail" class="ipt" runat ="server"/>
                        <asp:Button ID="btnSignup" class="btn" runat="server" Text=""
                            onclick="btnSignup_Click" OnClientClick ="javascript: return validateForm();"/>
                        <asp:Label ID="lblMsg" class="thankslabel" runat="server" Text="Label"></asp:Label>
                   </form>                         
            </div>
    </div>
    <div class="copy_right">
    Copyright © 2011 All rights reserved 7Hats Services
    </div>
    <div class="solical_links">
    <a href="#" class="fb"><img src="images/facebook.jpg" /></a>
    <a href="#" class="twi"><img src="images/twitter.png" /></a>
    </div>
    </div>
    </div><!-- footer ends here-->
    </div><!-- wrapper ends here-->
    </body>
    </html>

     


    Email.cs

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net.Mail;
    using System.Net;
    using System.Configuration;

    /// <summary>
    /// Summary description for Email
    /// </summary>
    public class Email
    {
        public Email()
        {
            _mailTo = ConfigurationSettings.AppSettings["EmailTo"];
            _mailDisplayName = ConfigurationSettings.AppSettings["EmailFrom"];
        }
        private string _mailTo;
        private string _mailFrom;
        private string _mailBody;
        private string _mailSubject;
        private string _mailDisplayName;


        public string MailTo
        {
            get
            {
                return _mailTo;
            }
            set
            {
                _mailTo = value;
            }
        }
        public string MailFrom
        {
            get
            {
                return _mailFrom;
            }
            set
            {
                _mailFrom = value;
            }
        }
        public string MailSubject
        {
            get
            {
                return _mailSubject;
            }
            set
            {
                _mailSubject = value;
            }
        }
        public string MailBody
        {
            get
            {
                return _mailBody;
            }
            set
            {
                _mailBody = value;
            }
        }
        public string MailDisplayName
        {
            get
            {
                return _mailDisplayName;
            }
            set
            {
                _mailDisplayName = value;
            }
        }
        public void SendEmail()
        {
            MailMessage msgMail = new MailMessage();
            msgMail.From = new MailAddress(_mailFrom);
            msgMail.To.Add(_mailTo); // Change this
            msgMail.Subject = _mailSubject;
            msgMail.Body = _mailBody;
            msgMail.IsBodyHtml = true;
            msgMail.Priority = MailPriority.High;

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "";  // For Yahoo Mail  ... Change this According to your requirement
            smtpClient.Port = ;  // and this also
            smtpClient.Credentials = new NetworkCredential();
            smtpClient.EnableSsl = false;

            try
            {
                smtpClient.Send(msgMail);
            }
            catch (Exception ex)
            {
            }
        }

    }

    this page shown error pls tell me the step by step error how to retify

    • Edited by praveen08 Wednesday, December 28, 2011 9:43 AM
    •  
  • Friday, December 30, 2011 11:29 AM
     
     Proposed Answer

    Hi praveen ,

    The code looks all fine try and check the following things

    Put a Breakpoint and Check whether you are getting the values of "_mailto" and "_mailDisplayname" from Configuration file if not change the appsettings and add the EmailTo and EmailFrom in appsettings within configurations

    e.g..

     <appSettings>
        <add key="EmailTo" value=" Toemailaddress"/>
        <add key="EmailFrom" value="FromEmailaddress"/>
      </appSettings>
     

     

    • Proposed As Answer by jayesh1990 Friday, December 30, 2011 11:29 AM
    •  
  • Monday, January 02, 2012 11:57 AM
     
     

    HI

    msgMail.From = new MailAddress(_mailFrom);

    •         msgMail.To.Add(_mailTo); // Change this

            msgMail.Subject = _mailSubject;
            msgMail.Body = _mailBody;
            msgMail.IsBodyHtml = true;
            msgMail.Priority = MailPriority.

     

    there is no condition for that line wat i do

  • Thursday, January 05, 2012 5:08 AM
     
     
    hi

    i am setting appsettings mail was sending but it will not come to mail id.wat is the problem,can u plz tell me.
  • Monday, April 09, 2012 3:58 AM
     
     

    Hi Praveen

    Try the following............

     mailMSG.BodyEncoding = System.Text.Encoding.ASCII;
            mailMSG.IsBodyHtml = true;
            mailMSG.Priority = MailPriority.Normal;
            SmtpClient myMail = new SmtpClient("-------------");//Change accordingly
            myMail.UseDefaultCredentials = true;
            
       myMail.Host = "localhost"; //Change accordingly
       //myMail.SmtpServer="--------";//Change accordingly
            myMail.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            //myMail.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            myMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            myMail.Send(mailMSG);

    I am using same.. and it works fine for me.


    • Edited by Sunil Raina Monday, April 09, 2012 6:29 AM
    •