Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我试图在单击按钮时发送电子邮件时出错_C# - Fatal编程技术网

C# 当我试图在单击按钮时发送电子邮件时出错

C# 当我试图在单击按钮时发送电子邮件时出错,c#,C#,System.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理 其他信息:值不能为空 似乎无法找到错误的来源,错误出现在第88行 -这是我尝试使用的代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using

System.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理

其他信息:值不能为空

似乎无法找到错误的来源,错误出现在第88行

-这是我尝试使用的代码

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统配置;
使用系统文本;
使用System.Net.Mail;
Net系统;
公共部分类UserCreate:System.Web.UI.Page
{
字符串CustLoginID、CustName、CUSTSEXT、CustDOB、CustEmail、CustTelNo、CustAddress、CustPassword;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效btn\u返回\u单击(对象发送者,事件参数e)
{
重定向(“UserCorL.aspx”);
}
受保护的无效btn\u确定\u单击(对象发送方,事件参数e)
{
int结果=0;
客户客户=新客户(tb_CustLoginID.Text、tb_CustName.Text、rb_custness.SelectedItem.Text、tb_CustDOB.Text、tb_CustAddress.Text、tb_custelno.Text、tb_CustEmail.Text、tb_CustPassword.Text);
结果=cust.CustomerInsert();
sendMsg(CustLoginID、CustName、CustGender、CustDOB、CustEmail、CustTelNo、CustAddress、CustPassword);
会话[“id”]=客户;
回复。写(“警报(‘已发送电子邮件’);”;
}
public static void sendMsg(字符串CustLoginID、字符串CustName、字符串CustGender、字符串CustDOB、字符串CustAddress、字符串CustTelNo、字符串CustEmail、字符串CustPassword)
{
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.Credentials=新系统.Net.NetworkCredential(“projectwingsdrinks@gmail.com“,“WingsDrinks”);
smtp.EnableSsl=true;
MailMessage msg=新的MailMessage();
msg.Subject=“Hello”+CustName;
msg.Body=“Hello”+CustName+“感谢您的注册,您的帐户详细信息如下:”;
msg.Body+=“”;
msg.Body+=“LoginID:+CustLoginID+”;
msg.Body+=“”;
msg.Body+=“”;
msg.Body+=“密码:+CustPassword+”;
msg.Body+=“”;
msg.Body+=“”;
消息体+=“DOB:+CustDOB+”;
msg.Body+=“”;
msg.Body+=“”;
msg.Body+=“电子邮件:+CustEmail+”;
msg.Body+=“”;
msg.Body+=“”;
msg.Body+=“性别:”+CustGender+”;
msg.Body+=“”;
msg.Body+=“”;
消息正文+=“联系人号码:”+custelno+”;
msg.Body+=“”;
msg.Body+=“”;
msg.Body+=“地址:”+CustAddress+”;
msg.Body+=“”;
msg.Body+=“”;
msg.Body+=“谢谢”;
msg.Body+=“”;
msg.Body+=“来自团队WingsDrinks”;
msg.Body+=“”;
字符串toAddress=CustEmail;
msg.To.Add(toAddress);
string fromAddress=“\”WingsDrinks\”;
msg.From=新邮件地址(fromAddress);
msg.IsBodyHtml=true;
尝试
{
smtp.Send(msg);
}
抓住
{
投掷;
}
}
}
更改此行

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


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

检查此行
msg.To.Add(toAddress)
,您可以添加
null
值您好!我根据您的要求进行了更改,现在底部出现了一个错误-“thow;”在App_Web_kxcyocwt.dll中发生了类型为“System.Net.Mail.SmtpException”的异常,但未在用户代码中处理其他信息:SMTP服务器需要安全连接或客户端未经身份验证。服务器响应为:需要5.5.1身份验证。@现在您必须检查SmtpClient使用示例。如果需要,启用SSL,设置凭据等。。。“我对这个很陌生,我该怎么做呢?”见这里的例子
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);