-2

An exception of type 'System.ArgumentNullException' occurred in System.dll but was not handled in user code

Additional information: Value cannot be null.

can't seem to find where the error is coming from, error appears on line 88

https://github.com/SubhanjanBasu/MailServices/blob/master/SIGNUP1.aspx.cs - this is the codes that i tried using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Net.Mail;
using System.Net;

public partial class UserCreate : System.Web.UI.Page
{
    string CustLoginID, CustName, CustGender, CustDOB, CustEmail, CustTelNo, CustAddress, CustPassword;
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserCorL.aspx");
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        int result = 0;

        Customer cust = new Customer(tb_CustLoginID.Text, tb_CustName.Text, rb_CustGender.SelectedItem.Text, tb_CustDOB.Text, tb_CustAddress.Text, tb_CustTelNo.Text, tb_CustEmail.Text, tb_CustPassword.Text);
        result = cust.CustomerInsert();
        sendMsg(CustLoginID, CustName, CustGender, CustDOB, CustEmail, CustTelNo, CustAddress, CustPassword);
        Session["id"] = cust;
        Response.Write("<script>alert('Email Sent');</script>");

    }

    public static void sendMsg(string CustLoginID, string CustName, string CustGender, string CustDOB, string CustAddress, string CustTelNo, string CustEmail, string CustPassword)
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("projectwingsdrinks@gmail.com", "WingsDrinks");
        smtp.EnableSsl = true;
        MailMessage msg = new MailMessage();
        msg.Subject = "Hello" + CustName;
        msg.Body = "Hello" + CustName + "Thank You for Registering, Your Account Details are given below:";
        msg.Body += "<tr>";
        msg.Body += "<td>LoginID: " + CustLoginID + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Password: " + CustPassword + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>DOB:" + CustDOB + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Email: " + CustEmail + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Gender: " + CustGender + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Contact No.: " + CustTelNo + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Address: " + CustAddress + "</td>";
        msg.Body += "</tr>";
        msg.Body += "<tr>";
        msg.Body += "<td>Thank you</td>";
        msg.Body += "</tr>";
        msg.Body += "<td>From Team WingsDrinks </td>";
        msg.Body += "<tr>";

        string toAddress = CustEmail;
        msg.To.Add(toAddress);

        string fromAddress = "\"WingsDrinks\"<projectwingsdrinks@gmail.com>";
        msg.From = new MailAddress(fromAddress);
        msg.IsBodyHtml = true;

        try
        {
            smtp.Send(msg);
        }
        catch
        {
            throw;
        }
    }
}
George Alexandria
  • 2,841
  • 2
  • 16
  • 24
Ash
  • 1
  • 3

1 Answers1

1

Change this line

sendMsg(CustLoginID, CustName, CustGender, CustDOB, CustEmail, CustTelNo, CustAddress, CustPassword);

to:

sendMsg(tb_CustLoginID.Text, tb_CustName.Text, rb_CustGender.SelectedItem.Text, tb_CustDOB.Text, tb_CustAddress.Text, tb_CustTelNo.Text, tb_CustEmail.Text, tb_CustPassword.Text);

You're probably passing null value to CustEmail parameter. In result it adds null value here msg.To.Add(toAddress)

opewix
  • 4,993
  • 1
  • 20
  • 42
  • hello! i changed according to what u asked and now i have an error at the bottom - "thow;" An exception of type 'System.Net.Mail.SmtpException' occurred in App_Web_kxcyocwt.dll but was not handled in user code Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – Ash Jul 22 '17 at 11:07
  • @Ash now you have to check SmtpClient usage examples. Enable SSL if required, set your credentials and so on – opewix Jul 22 '17 at 11:09
  • oh... im pretty new to this , how do i go about doing that? – Ash Jul 22 '17 at 11:12
  • @Ash see examples here https://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp – opewix Jul 22 '17 at 11:24