使用System; 使用System.Collections; 使用System.Configuration; 使用System.Data; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.HtmlControls; 使用System.Web.UI.WebControls; 使用System.Web.UI.WebControls.WebParts; 使用System.Xml.Linq; 使用System.Net.Mail;
public partial class adminEmail : System.Web.UI.Page
{
string address = null;
string to = null;
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (address == null)
address = GridView1.Rows[0].Cells[1].Text;
else
address = address + "," + GridView1.Rows[i].Cells[1].Text;
Label2.Text = address;
string to = address;
txtTo.Text = to;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string from = ".com"; //Replace this with your own correct Gmail Address
//Replace this with the Email Address to whom you want to send the mail
Label1.Text = "Mail sent successfully";
Label1.Visible = true;
MailMessage mail = new System.Net.Mail.MailMessage();
Label2.Text = address;
/*gettting error here*/ mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(from, "Check My Ads", System.Text.Encoding.UTF8);
mail.Subject = txtSubject.Text;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = Editor1.ScriptPath;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "password");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
}
}
和我的.aspx代码
<%@ Page Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeFile="adminEmail.aspx.cs" Inherits="adminEmail" Title="Untitled Page" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor"
TagPrefix="cc1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<center>
<table style="width: 652px; height: 504px">
<tr>
<td colspan="2">
<span style="color: activecaption"><strong><em><span style="font-size: 16pt">
Mail Sender</span></em></strong>
</span>
</td>
</tr>
<tr>
<td style="height: 26px" >
<strong><span style="color: activecaption">
Enter UserName</span></strong>
</td>
<td align=left style="height: 26px">
<asp:TextBox ID="txtUserName" runat="server" Width="169px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong><span style="color:Gray"><span style="color: activecaption">
Enter Password</span> </span></strong>
</td>
<td align=left>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="168px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong><span style="color: activecaption">
To:</span></strong>
</td>
<td align=left>
<asp:TextBox ID="txtTo" runat="server" Width="169px" Height="22px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong><span style="color: activecaption">
Subject:</span></strong>
</td>
<td align=left>
<asp:TextBox ID="txtSubject" runat="server" Width="168px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong><span style="color: activecaption">
Body: </span></strong>
</td>
<td align="left">
<cc1:Editor ID="Editor1" runat="server" ButtonMouseOverBorderColor="GradientInactiveCaption" ButtonMouseOverColor="ActiveCaption" DialogButtonBarColor="ActiveCaption" DialogHeadingColor="ActiveCaption" DialogSelectedTabColor="ActiveCaption" DialogUnselectedTabColor="ActiveCaption" EditorBorderColor="ActiveCaption" SelectedTabBackColor="ActiveCaption" SelectedTabTextColor="InactiveCaptionText" TabBackColor="ActiveCaption" TabMouseOverColor="GradientActiveCaption" ToolbarColor="GradientActiveCaption" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="SUBMIT" Font-Bold="True" ForeColor="ActiveCaption" OnClick="btnSubmit_Click" />
<asp:Button ID="btnCancel" runat="server" Text="RESET" CausesValidation="False" Font-Bold="True" ForeColor="ActiveCaption" OnClick="btnCancel_Click" UseSubmitBehavior="False" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label" Visible=false></asp:Label>
<br />
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="username" DataSourceID="SqlDataSource1" Width="223px">
<Columns>
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True"
SortExpression="username" />
<asp:BoundField DataField="emailid" HeaderText="emailid"
SortExpression="emailid" />
<asp:BoundField DataField="gender" HeaderText="gender"
SortExpression="gender" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:eclassConnectionString2 %>"
SelectCommand="SELECT [username], [emailid], [gender] FROM [userdetails] WHERE ([status_user] = @status_user)">
<SelectParameters>
<asp:Parameter DefaultValue="new" Name="status_user" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
</center>
</div>
</asp:Content>
请告诉我我的错是什么
答案 0 :(得分:1)
/*gettting error here*/ mail.To.Add(new MailAddress(to));
您从未在任何地方设置to
的值。在Page_Load
方法中,您有一个名为to
的局部变量,它隐藏了类成员。将代码更改为具有适当的值,并查看获得的内容。
答案 1 :(得分:0)
您尚未定义to
。
/*gettting error here*/ mail.To.Add(new MailAddress(to));
试试这个:
mail.To.Add(new MailAddress(txtTo.Text));
或
foreach(string toAddr in txtTo.Text.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries))
{
mail.To.Add(new MailAddress(toAddr));
}
将“发送”代码替换为:
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "testSubject", "testbody");