C# 使用按钮发送电子邮件时出错
我想做的是发送一封电子邮件,通过一个按钮点击一个附加的图像与它。我的aspx页面上有以下代码:C# 使用按钮发送电子邮件时出错,c#,email,button,C#,Email,Button,我想做的是发送一封电子邮件,通过一个按钮点击一个附加的图像与它。我的aspx页面上有以下代码: <div> <table style=" border:1px solid" align="center"> <tr> <td colspan="2" align="center"> <b>Send Mail using gmail credentials in asp.net</b> </td> </tr>
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using gmail credentials in asp.net</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
在asp.net中使用gmail凭据发送邮件
Gmail用户名:
Gmail密码:
主题:
致:
附上一份文件:
正文:
下面是背后的代码:
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body.
myimage.ContentId = "companylogo";
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
试试看
{
MailMessage Msg=新的MailMessage();
//发件人电子邮件地址。
Msg.From=新邮件地址(txtUsername.Text);
//收件人电子邮件地址。
Msg.To.Add(txtTo.Text);
Msg.Subject=txtSubject.Text;
//文件上载路径
字符串文件名=fileUpload1.PostedFile.FileName;
字符串mailbody=txtBody.Text+“
”;
LinkedResource myimage=新LinkedResource(文件名);
//创建HTML视图
AlternateView htmlMail=AlternateView.createAlternateView-FromString(邮件体,null,“text/html”);
//设置ContentId属性。ContentId属性的值必须与相同
//电子邮件正文中图像标记的src属性。
myimage.ContentId=“companylogo”;
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
//您的远程SMTP服务器IP。
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.Credentials=new System.Net.NetworkCredential(txtexerName.Text,txtpwd.Text);
smtp.EnableSsl=true;
smtp.Send(Msg);
Msg=null;
RegisterStartupScript(“UserMsg”,“alert('Mail sented thanky…');if(alert){window.location='SendMail.aspx';}”);
}
捕获(例外情况除外)
{
WriteLine(“{0}捕获异常。”,ex);
}
然而这条线
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
Page.RegisterStartupScript(“UserMsg”,“警报('Mail send thanky…');如果(警报){window.location='SendMail.aspx';}”);
给我一个错误,说:
RegisterStartupScript(字符串,字符串)已过时
你知道这是什么,怎么解决吗?谢谢大家! 您的电子邮件的安全性应该较低。
试试看
{
MailMessage Msg=新的MailMessage();
//发件人电子邮件地址。
Msg.From=新邮件地址(txtUsername.Text);
//收件人电子邮件地址。
Msg.To.Add(txtTo.Text);
Msg.Subject=txtSubject.Text;
Msg.IsBodyHtml=true;
//文件上载路径
if(fileUpload1.HasFile)
{
//创建HTML视图
字符串mailbody=txtBody.Text+“
”;
AlternateView htmlMail=AlternateView.createAlternateView-FromString(邮件体,null,“text/html”);
字符串路径=Server.MapPath(“~/images/”);
字符串FileName=path+(fileUpload1.PostedFile.FileName);
//Msg.Attachments.Add(新附件(fileUpload1.PostedFile.InputStream,FileName));
LinkedResource myimage=新LinkedResource(文件名);
myimage.ContentId=“companylogo”;
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
}
//您的远程SMTP服务器IP。
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.Credentials=new System.Net.NetworkCredential(txtexerName.Text,txtpwd.Text);
smtp.EnableSsl=true;
smtp.Send(Msg);
Msg=null;
Page.ClientScript.RegisterStartupScript(GetType(),“msgbox”,“警报('Mail sent thanky…');如果(警报){window.location='WebForm1.aspx';}”,则为true);
}
捕获(例外情况除外)
{
WriteLine(“{0}捕获异常。”,ex);
}`
试试这个
using System;
using System.Net.Mail;
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
string fileName = Path.GetFileName(FileName);
Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
//LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body.
//myimage.ContentId = "companylogo";
// htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
//ClientScript.RegisterStartupScript( "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
Response.Write("<script>alert('Email Sent');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('Unable To Send Email');</script>");
Console.WriteLine("{0} Exception caught.", ex);
}
}
使用系统;
使用System.Net.Mail;
使用System.IO;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
尝试
{
MailMessage Msg=新的MailMessage();
//发件人电子邮件地址。
Msg.From=新邮件地址(txtUsername.Text);
//收件人电子邮件地址。
Msg.To.Add(txtTo.Text);
Msg.Subject=txtSubject.Text;
//文件上载路径
字符串文件名=fileUpload1.PostedFile.FileName;
字符串mailbody=txtBody.Text+“
”;
字符串文件名=Path.GetFileName(文件名);
Msg.Attachments.Add(新附件(fileUpload1.PostedFile.InputStream,fileName));
//LinkedResource myimage=新LinkedResource(文件名);
//创建HTML视图
AlternateView htmlMail=AlternateView.createAlternateView-FromString(邮件体,null,“text/html”);
//设置ContentId属性。ContentId属性的值必须与相同
//电子邮件正文中图像标记的src属性。
//myimage.ContentId=“companylogo”;
//htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
//您的远程SMTP服务器IP。
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.Credentials=new System.Net.NetworkCredential(txtexerName.Text,txtpwd.Text);
smtp.EnableSsl=true;
smtp.Send(Msg);
Msg=null;
//RegisterStartupScript(“警报('Mail sented thanky…');if(警报){window.location='SendMail.aspx';}”);
回复。写(“警报(‘已发送电子邮件’);”;
}
捕获(例外情况除外)
{
回复。写(“警报(‘无法发送电子邮件’);”;
WriteLine(“{0}捕获异常。”,ex);
}
}
我想把你介绍给我的一位朋友。。。我试过这个,但它没有屈服。我的意思是,点击按钮后什么也没发生。你用调试器点测试过吗?另一件事是,你的电子邮件应该设置较少的安全性。对什么也没发生。如果我是c
using System;
using System.Net.Mail;
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
string fileName = Path.GetFileName(FileName);
Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
//LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body.
//myimage.ContentId = "companylogo";
// htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
//ClientScript.RegisterStartupScript( "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
Response.Write("<script>alert('Email Sent');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('Unable To Send Email');</script>");
Console.WriteLine("{0} Exception caught.", ex);
}
}