当我单击按钮时尝试发送电子邮件时发生错误

时间:2017-07-22 10:32:45

标签: c#

System.dll中出现'System.ArgumentNullException'类型的异常,但未在用户代码中处理

附加信息:值不能为空。

似乎找不到错误的来源,第88行出现错误

https://github.com/SubhanjanBasu/MailServices/blob/master/SIGNUP1.aspx.cs - 这是我尝试使用的代码

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

更改此行

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

为:

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);

您可能会将空值传递给CustEmail参数。结果,它在msg.To.Add(toAddress)

添加了空值