在发送邮件时,它显示错误为system.indexoutofrangeexception:email [英] While sending mail it shows error as system.indexoutofrangeexception: email
本文介绍了在发送邮件时,它显示错误为system.indexoutofrangeexception:email的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
发送电子邮件时我收到错误
System.IndexOutOfRangeException:电子邮件
请任何人都可以告诉我为什么会这样。
我尝试过:
< pre lang = C#> string uniqueCode = < span class =code-keyword> string .Empty;
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager。 ConnectionStrings [ MatriModel]。ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// 获取与提供的用户名或电子邮件ID匹配的记录。
cmd = new SqlCommand( 从tblProfile中选择FullName,其中Email COLLATE Latin1_general_CS_AS = @ emailId,con);
cmd.Parameters.AddWithValue( @ emailId,Convert.ToString (txtEmail.Text.Trim()));
dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.HasRows)
{
string username = string .Empty;
dr.Read();
username = dr [ FullName]。ToString();
// 生成唯一代码
uniqueCode = Convert.ToString(System.Guid .NewGuid());
// 在数据库表的UniquCode字段中更新唯一的随机代码
cmd = new SqlCommand( update tblProfile set UniqueCode = @uniqueCode,其中Email = @ emailid,con);
cmd.Parameters.AddWithValue( @ uniqueCode,uniqueCode);
cmd.Parameters.AddWithValue( @ emailid,txtEmail.Text.Trim() );
StreamReader reader = new StreamReader(Server.MapPath( 〜/ ForgotPasswordTemp.html跨度>));
string readFile = reader.ReadToEnd();
string myString = ;
myString = readFile;
myString = myString.Replace( $$ User $$,username);
myString = myString.Replace( $$ EmailId $$,txtEmail.Text) ;
myString = myString.Replace( $$ Url $$, http:// localhost:18601 / ResetPassword.aspx?emailId = + txtEmail.Text + & uCode = + uniqueCode + 跨度>);
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage( abc@syssigma.com,dr [ 电子邮件]。ToString(), 您要求的更改密码。,myString.ToString());
System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential( abc@syssigma.com, < span class =code-string> MyPassword);
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient( relay-hosting.secureserver.net, 25 );
mailclient.EnableSsl = false ;
mailclient.UseDefaultCredentials = false ;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true ;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
con.Close();
lblMessage.ForeColor = Color.Green;
lblMessage.Font.Bold = true ;
lblMessage.Text = 密码已发送到您的电子邮件地址。;
txtEmail.Text = ;
我要发送的Html文件是
<!DOCTYPE html>
< html>
< head>
< title> Vivahik< / title>
< meta charset =utf-8/>
< / head>
< body class =aAU>
< table border =0cellpadding =0cellspacing =0width =520>
< tbody>
< tr>
< td style =padding:5px; font-family:Arial,MS Sans serif,Arial,Verdana,Helvetica; font-size:9px; font-style:normal; text-transform:none; color: #a6a6a6\" >
您将收到这封邮件作为Vivahik.com的注册会员。请添加
< a href =#target =_ blank> info@Vivahik.com< / a>到您的地址簿,以确保发送到您的收件箱。
< / td>
< / tr>
< / tbody>
< / table>
< table cellpadding =0cellspacing =0border =0width =520style =border:1px solid#6c6c6c>
< tbody>
< tr style =background:#c22143>
< td valign =topstyle =padding:15px 20px>
< img src =images / logo.pngwidth =110height =40style =background:#c22143border =0alt =class =CToWUd>
< / td>
< / tr>
< tr>
< td valign =middleheight =55style =vertical-align:middle; padding-left:15px; font:normal 20px Georgia; color:#ffffffbgcolor =#f89722> ;
忘记密码
< / td>
< / tr>
< tr>
< td valign =topstyle =padding:15px 20px; font:normal 12px arial; color:#363636; line-height:18px>
< span style =display:block; padding-bottom:5px>< b>亲爱的$$用户$$<! - (标签) - >,< / b> < /跨度>
您最近请求获取有关您的登录密码的帮助。更改密码非常简单。< br>< br>点击电子邮件ID更改密码。(这只是一次访问)< br>< br>
< / td>
< / tr>
< tr>
< td style =padding-left:20px>
< table width =200cellpadding =0cellspacing =0style =font:normal 12px / 15px verdana,tahoma; color:#9a440dborder =0>
< tbody>
< tr>
< td align =left>
注册电子邮件ID
< / td>
< / tr>
< tr>
< td align =leftheight =5>< / td>
< / tr>
< tr>
< td align =left>
< a style =text-decoration:none
href =$$ Url $$
target =_ blank>
$$ EmailId $$
< / a>
< / td>
< / tr>
< tr height =15>< / tr>
< / tbody>
< / table>
< / td>
< / tr>
< tr>
< td valign =topstyle =padding:10px 20px 15px; font:normal 12px arial; color:#363636>
< b>祝你成为最好的生活伴侣,< / b>< br>团队 - Vivahik
< / td>
< / tr>
< / tbody>
< / table>
< table cellpadding =0cellspacing =0width =520border =0>
< tbody>
< tr>
< td valign =topstyle =padding-left:5px; padding-right:3px; padding-top:3px; padding-bottom:3px; font:normal 9px arial; text-align:justify ;颜色:#a6a6a6bgcolor =#ffffff>
您是Vivahik.com的会员。此电子邮件是根据Vivahik.com的隐私政策发送给您的。
< a href =#style =text-decoration:underline; color:#a6a6a6target =_ blank>
点击这里
< / a>退订。 Vivahik.com不对其自身以外的内容负责,也不对产品
或广告服务做出任何保证或保证。
< / td>
< / tr>
< / tbody>
< / table>
< / body>
< / html>
解决方案
用户
,username);
myString = myString.Replace(
EMAILID
While Sending email i am getting error as
System.IndexOutOfRangeException: Email
Please can any one tell me why is this coming.
What I have tried:
<pre lang="C#">string uniqueCode = string.Empty;
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MatriModel"].ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// get the records matching the supplied username or email id.
cmd = new SqlCommand("select FullName from tblProfile where Email COLLATE Latin1_general_CS_AS=@emailId", con);
cmd.Parameters.AddWithValue("@emailId", Convert.ToString(txtEmail.Text.Trim()));
dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.HasRows)
{
string username = string.Empty;
dr.Read();
username = dr["FullName"].ToString();
//generate unique code
uniqueCode = Convert.ToString(System.Guid.NewGuid());
//Updating an unique random code in then UniquCode field of the database table
cmd = new SqlCommand("update tblProfile set UniqueCode=@uniqueCode where Email=@emailid", con);
cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode);
cmd.Parameters.AddWithValue("@emailid", txtEmail.Text.Trim());
StreamReader reader = new StreamReader(Server.MapPath("~/ForgotPasswordTemp.html"));
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;
myString = myString.Replace("$$User$$", username);
myString = myString.Replace("$$EmailId$$", txtEmail.Text);
myString = myString.Replace("$$Url$$", "http://localhost:18601/ResetPassword.aspx?emailId=" + txtEmail.Text + "&uCode=" + uniqueCode + "");
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("abc@syssigma.com", dr["Email"].ToString(), "You requested for change in Password.", myString.ToString());
System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("abc@syssigma.com", "MyPassword");
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("relay-hosting.secureserver.net", 25);
mailclient.EnableSsl = false;
mailclient.UseDefaultCredentials = false;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
con.Close();
lblMessage.ForeColor = Color.Green;
lblMessage.Font.Bold = true;
lblMessage.Text = "Password has been sent to your email address.";
txtEmail.Text = "";
My Html File i want to send is
<!DOCTYPE html> <html> <head> <title>Vivahik</title> <meta charset="utf-8" /> </head> <body class="aAU"> <table border="0" cellpadding="0" cellspacing="0" width="520"> <tbody> <tr> <td style="padding:5px;font-family:Arial,MS Sans serif,Arial,Verdana,Helvetica;font-size:9px;font-style:normal;text-transform:none;color:#a6a6a6"> You are receiving this mail as a registered member of Vivahik.com. Please add <a href="#" target="_blank">info@Vivahik.com</a> to your address book to ensure delivery into your inbox. </td> </tr> </tbody> </table> <table cellpadding="0" cellspacing="0" border="0" width="520" style="border:1px solid #6c6c6c"> <tbody> <tr style="background:#c22143"> <td valign="top" style="padding:15px 20px"> <img src="images/logo.png" width="110" height="40" style="background:#c22143" border="0" alt="" class="CToWUd"> </td> </tr> <tr> <td valign="middle" height="55" style="vertical-align:middle;padding-left:15px;font:normal 20px Georgia;color:#ffffff" bgcolor="#f89722"> Forgot password </td> </tr> <tr> <td valign="top" style="padding:15px 20px;font:normal 12px arial;color:#363636;line-height:18px"> <span style="display:block;padding-bottom:5px"><b>Dear $$User$$<!-- (Label) -->,</b></span> You have recently requested for help with your login password. Changing your password is quite simple.<br><br>Click on the Email ID to change your password.(It is only one time access)<br><br> </td> </tr> <tr> <td style="padding-left:20px"> <table width="200" cellpadding="0" cellspacing="0" style="font:normal 12px/15px verdana,tahoma;color:#9a440d" border="0"> <tbody> <tr> <td align="left"> Registered Email ID </td> </tr> <tr> <td align="left" height="5"></td> </tr> <tr> <td align="left"> <a style="text-decoration:none" href="$$Url$$" target="_blank"> $$EmailId$$ </a> </td> </tr> <tr height="15"></tr> </tbody> </table> </td> </tr> <tr> <td valign="top" style="padding:10px 20px 15px;font:normal 12px arial;color:#363636"> <b>Wishing you the best life partner,</b><br>Team - Vivahik </td> </tr> </tbody> </table> <table cellpadding="0" cellspacing="0" width="520" border="0"> <tbody> <tr> <td valign="top" style="padding-left:5px;padding-right:3px;padding-top:3px;padding-bottom:3px;font:normal 9px arial;text-align:justify;color:#a6a6a6" bgcolor="#ffffff"> You are a Vivahik.com member. This e-mail comes to you in accordance with Vivahik.com's Privacy Policy. <a href="#" style="text-decoration:underline;color:#a6a6a6" target="_blank"> Click here </a> to unsubscribe. Vivahik.com is not responsible for content other than its own and makes no warranties or guarantees about the products or services that are advertised. </td> </tr> </tbody> </table> </body> </html>
解决方案
User
", username); myString = myString.Replace("
EmailId
这篇关于在发送邮件时,它显示错误为system.indexoutofrangeexception:email的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文